--- /dev/null
+#
+# Sonar, open source software quality management tool.
+# Copyright (C) 2008-2012 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# Sonar is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 3 of the License, or (at your option) any later version.
+#
+# Sonar is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with Sonar; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+#
+class Api::AuthenticationController < Api::ApiController
+ skip_before_filter :check_authentication
+
+ # prevent HTTP proxies from caching authentication status
+ before_filter :set_cache_buster, :only => 'index'
+
+ #
+ # GET /api/authentication/index
+ # curl http://localhost:9000/api/authentication/index -v -u admin:admin
+ #
+ def index
+ hash={:valid => valid?}
+
+ respond_to do |format|
+ format.json { render :json => jsonp(hash) }
+ format.xml { render :xml => hash.to_xml(:skip_types => true, :root => 'authentication') }
+ format.text { render :text => text_not_supported }
+ end
+ end
+
+ private
+
+ def valid?
+ logged_in? || (!force_authentication? && anonymous?)
+ end
+
+ def force_authentication?
+ property = Property.find(:first, :conditions => {:prop_key => org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY, :resource_id => nil, :user_id => nil})
+ property ? property.value == 'true' : false
+ end
+
+ def anonymous?
+ !session.has_key?(:user_id)
+ end
+
+ def set_cache_buster
+ response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
+ response.headers["Pragma"] = "no-cache"
+ response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
+ end
+
+end
<version>6.1.6</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.easytesting</groupId>
+ <artifactId>fest-assert</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.wsclient.services;
+
+public class Authentication extends Model {
+ private boolean valid;
+
+ public boolean isValid() {
+ return valid;
+ }
+
+ public Authentication setValid(boolean valid) {
+ this.valid = valid;
+ return this;
+ }
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.wsclient.services;
+
+public class AuthenticationQuery extends Query<Authentication> {
+ public static final String BASE_URL = "/api/authentication/index";
+
+ @Override
+ public String getUrl() {
+ return BASE_URL;
+ }
+
+ @Override
+ public Class<Authentication> getModelClass() {
+ return Authentication.class;
+ }
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.wsclient.unmarshallers;
+
+import org.sonar.wsclient.services.Authentication;
+import org.sonar.wsclient.services.WSUtils;
+
+import java.util.List;
+
+public class AuthenticationUnmarshaller implements Unmarshaller<Authentication> {
+ public Authentication toModel(String json) {
+ WSUtils utils = WSUtils.getINSTANCE();
+ Object map = utils.parse(json);
+
+ return new Authentication()
+ .setValid(utils.getBoolean(map, "valid"));
+ }
+
+ public List<Authentication> toModels(String json) {
+ throw new UnsupportedOperationException();
+ }
+}
*/
package org.sonar.wsclient.unmarshallers;
-import org.sonar.wsclient.services.*;
+import org.sonar.wsclient.services.Authentication;
+import org.sonar.wsclient.services.Dependency;
+import org.sonar.wsclient.services.DependencyTree;
+import org.sonar.wsclient.services.Event;
+import org.sonar.wsclient.services.Favourite;
+import org.sonar.wsclient.services.ManualMeasure;
+import org.sonar.wsclient.services.Metric;
+import org.sonar.wsclient.services.Model;
+import org.sonar.wsclient.services.Plugin;
+import org.sonar.wsclient.services.Profile;
+import org.sonar.wsclient.services.Property;
+import org.sonar.wsclient.services.Resource;
+import org.sonar.wsclient.services.Review;
+import org.sonar.wsclient.services.Rule;
+import org.sonar.wsclient.services.Server;
+import org.sonar.wsclient.services.ServerSetup;
+import org.sonar.wsclient.services.Source;
+import org.sonar.wsclient.services.TimeMachine;
+import org.sonar.wsclient.services.Violation;
import java.util.HashMap;
import java.util.Map;
unmarshallers.put(Profile.class, new ProfileUnmarshaller());
unmarshallers.put(Review.class, new ReviewUnmarshaller());
unmarshallers.put(ManualMeasure.class, new ManualMeasureUnmarshaller());
+ unmarshallers.put(Authentication.class, new AuthenticationUnmarshaller());
}
public static <MODEL extends Model> Unmarshaller<MODEL> forModel(Class<MODEL> modelClass) {
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.wsclient.services;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class AuthenticationQueryTest extends QueryTestCase {
+ @Test
+ public void should_query_right_url() {
+ AuthenticationQuery query = new AuthenticationQuery();
+
+ assertThat(query.getUrl()).isEqualTo("/api/authentication/index");
+ assertThat(query.getModelClass().getName()).isEqualTo(Authentication.class.getName());
+ }
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.wsclient.services;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class AuthenticationTest {
+ @Test
+ public void should_set_valid_state() {
+ assertThat(new Authentication().isValid()).isFalse();
+ assertThat(new Authentication().setValid(false).isValid()).isFalse();
+ assertThat(new Authentication().setValid(true).isValid()).isTrue();
+ }
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.wsclient.unmarshallers;
+
+import org.junit.Test;
+import org.sonar.wsclient.services.Authentication;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class AuthenticationUnmarshallerTest extends UnmarshallerTestCase {
+ @Test
+ public void should_unmarshall_valid_authentication() {
+ Authentication authentication = new AuthenticationUnmarshaller().toModel(loadFile("/authentication/authentication_valid.json"));
+
+ assertThat(authentication.isValid()).isTrue();
+ }
+
+ @Test
+ public void should_unmarshall_invalid_authentication() {
+ Authentication authentication = new AuthenticationUnmarshaller().toModel(loadFile("/authentication/authentication_invalid.json"));
+
+ assertThat(authentication.isValid()).isFalse();
+ }
+}
--- /dev/null
+{"valid":false}
\ No newline at end of file
--- /dev/null
+{"valid":true}
\ No newline at end of file