Security Demo
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 16 Sep 2009 14:06:11 +0000 (14:06 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 16 Sep 2009 14:06:11 +0000 (14:06 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@2937 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

24 files changed:
security/demo/org.argeo.security.demo.log4j/META-INF/MANIFEST.MF [new file with mode: 0644]
security/demo/org.argeo.security.demo.log4j/log4j.properties [new file with mode: 0644]
security/demo/pom.xml
security/modules/org.argeo.security.webapp/META-INF/MANIFEST.MF
security/modules/org.argeo.security.webapp/WEB-INF/security-servlet.xml [new file with mode: 0644]
security/modules/pom.xml
security/runtime/org.argeo.security.core/pom.xml
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/ArgeoUserDetails.java
security/runtime/org.argeo.security.mvc/pom.xml
security/runtime/org.argeo.security.mvc/src/main/java/org/argeo/security/mvc/UsersRolesController.java [new file with mode: 0644]
security/runtime/pom.xml
server/modules/org.argeo.server.catalina/META-INF/MANIFEST.MF
server/pom.xml
server/runtime/org.argeo.server.core/.classpath [new file with mode: 0644]
server/runtime/org.argeo.server.core/.project [new file with mode: 0644]
server/runtime/org.argeo.server.core/.settings/org.eclipse.jdt.core.prefs [new file with mode: 0644]
server/runtime/org.argeo.server.core/.settings/org.maven.ide.eclipse.prefs [new file with mode: 0644]
server/runtime/org.argeo.server.core/pom.xml [new file with mode: 0644]
server/runtime/org.argeo.server.core/src/main/java/org/argeo/server/ArgeoServerException.java [new file with mode: 0644]
server/runtime/org.argeo.server.core/src/main/java/org/argeo/server/ServerAnswer.java [new file with mode: 0644]
server/runtime/org.argeo.server.json/pom.xml
server/runtime/org.argeo.server.json/src/main/java/org/argeo/server/json/mvc/JsonView.java
server/runtime/org.argeo.server.json/src/main/java/org/argeo/server/json/mvc/JsonViewResolver.java [new file with mode: 0644]
server/runtime/pom.xml

diff --git a/security/demo/org.argeo.security.demo.log4j/META-INF/MANIFEST.MF b/security/demo/org.argeo.security.demo.log4j/META-INF/MANIFEST.MF
new file mode 100644 (file)
index 0000000..b96ebfe
--- /dev/null
@@ -0,0 +1,3 @@
+Bundle-SymbolicName: org.argeo.server.demo.log4j
+Bundle-Version: 0.1.1.SNAPSHOT
+Fragment-Host: com.springsource.org.apache.log4j
diff --git a/security/demo/org.argeo.security.demo.log4j/log4j.properties b/security/demo/org.argeo.security.demo.log4j/log4j.properties
new file mode 100644 (file)
index 0000000..dd02654
--- /dev/null
@@ -0,0 +1,28 @@
+log4j.rootLogger=WARN, console
+
+## Levels
+log4j.logger.org.argeo=DEBUG
+
+log4j.logger.org.hibernate=WARN
+
+log4j.logger.org.springframework=WARN
+log4j.logger.org.springframework.web=DEBUG
+#log4j.logger.org.springframework.jms=WARN
+#log4j.logger.org.springframework.security=WARN
+
+log4j.logger.org.apache.activemq=WARN
+log4j.logger.org.apache.activemq.transport=WARN
+log4j.logger.org.apache.activemq.ActiveMQMessageConsumer=INFO
+log4j.logger.org.apache.activemq.ActiveMQMessageProducer=INFO
+
+log4j.logger.org.apache.catalina=INFO
+log4j.logger.org.apache.coyote=INFO
+log4j.logger.org.apache.tomcat=INFO
+
+## Appenders
+# console is set to be a ConsoleAppender.
+log4j.appender.console=org.apache.log4j.ConsoleAppender
+
+# console uses PatternLayout.
+log4j.appender.console.layout=org.apache.log4j.PatternLayout
+log4j.appender.console.layout.ConversionPattern= %-5p %d{ISO8601} %m - %c%n
index 10fbcb526d5d52e8f8ffa6009363908b5856fb71..7f52d9631fa513d295208e5234f0983c41fcc5d9 100644 (file)
                                <groupId>org.argeo.maven.plugins</groupId>
                                <artifactId>maven-argeo-osgi-plugin</artifactId>
                                <configuration>
-                                       <execDir>target/exec/server</execDir>
+                                       <execDir>target/exec</execDir>
                                        <osgiBootArtifactId>org.argeo.osgi.boot</osgiBootArtifactId>
+                                       <argsToAppend>
+                                               <arg>-clean</arg>
+                                       </argsToAppend>
+                                       <fork>false</fork>
                                        <systemProperties>
                                                <slc.osgi.start>
                                                        org.springframework.osgi.extender,
                                                        org.springframework.osgi.web.extender,
                                                        org.argeo.dep.osgi.catalina.start,
-                                                       org.argeo.server.security.webapp
+                                                       org.argeo.security.webapp
                                                </slc.osgi.start>
                                                <slc.osgi.bundles>
                                                        ${basedir};in=*;ex=pom.xml;ex=target;ex=.*,
                                                        ${basedir}/../modules;in=*;ex=pom.xml;ex=target;ex=.*,
+                                                       ${basedir}/../../server/modules;in=*;ex=pom.xml;ex=target;ex=.*,
                                                </slc.osgi.bundles>
                                        </systemProperties>
                                </configuration>
                </plugins>
        </build>
        <dependencies>
+               <!-- Argeo -->
+               <dependency>
+                       <groupId>org.argeo.commons.security</groupId>
+                       <artifactId>org.argeo.security.mvc</artifactId>
+                       <version>0.1.1-SNAPSHOT</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.argeo.commons.server</groupId>
+                       <artifactId>org.argeo.server.json</artifactId>
+                       <version>0.1.1-SNAPSHOT</version>
+               </dependency>
+
                <!--  Third Parties -->
 
                <!-- OSGi -->
                        <artifactId>com.springsource.javax.xml.bind</artifactId>
                </dependency>
 
-               <!-- Taglibs
-               <dependency>
-                       <groupId>org.apache.taglibs</groupId>
+               <!--
+                       Taglibs <dependency> <groupId>org.apache.taglibs</groupId>
                        <artifactId>com.springsource.org.apache.taglibs.standard</artifactId>
-               </dependency> -->
+                       </dependency>
+               -->
 
 
                <!-- Tomcat -->
index e17384c655f61db3b47d6c5014a6b2998b1c1b39..8116c1e4f3179d0303950ef59a7da645a139e156 100644 (file)
@@ -3,4 +3,12 @@ Bundle-SymbolicName: org.argeo.security.webapp
 Bundle-Version: 0.1.1.SNAPSHOT
 Import-Package: javax.servlet,
  javax.servlet.http,
- javax.servlet.resources
+ javax.servlet.resources,
+ org.argeo.security.mvc,
+ org.argeo.server.json.mvc,
+ org.springframework.osgi.web.context.support,
+ org.springframework.security.userdetails.memory,
+ org.springframework.web.context,
+ org.springframework.web.context.support,
+ org.springframework.web.filter,
+ org.springframework.web.servlet
diff --git a/security/modules/org.argeo.security.webapp/WEB-INF/security-servlet.xml b/security/modules/org.argeo.security.webapp/WEB-INF/security-servlet.xml
new file mode 100644 (file)
index 0000000..21f9561
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
+       xmlns:context="http://www.springframework.org/schema/context"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
+       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
+       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
+
+       <context:component-scan base-package="org.argeo.security.mvc" />
+
+       <bean id="controller" class="org.argeo.security.mvc.UsersRolesController"></bean>
+
+       <bean id="viewResolver" class="org.argeo.server.json.mvc.JsonViewResolver">
+       </bean>
+
+</beans>
\ No newline at end of file
index a435223a1bfafef4a447866cdbea960298b49b62..fb9ed57590218189b148d10a54d0d0eaa41c0da5 100644 (file)
@@ -9,7 +9,7 @@
        <groupId>org.argeo.security</groupId>
        <artifactId>modules</artifactId>
        <packaging>bundles</packaging>
-       <name>Commons Server Modules</name>
+       <name>Commons Security Modules</name>
        <build>
                <plugins>
                        <plugin>
index 51aca0eac1c3f166bb5503fcce1e7ba298437f61..340e08fc9a9c4b7994b04a99f683ab54cef8d8ca 100644 (file)
        <dependencies>
                <!-- Spring LDAP -->
                <dependency>
-                       <groupId>org.springframework.ldap</groupId>
-                       <artifactId>org.springframework.ldap</artifactId>
-                       <version>1.3.0.RELEASE</version>
-               </dependency>
-               <dependency>
-                       <groupId>org.springframework.ldap</groupId>
-                       <artifactId>org.springframework.ldap.core.simple</artifactId>
-                       <version>1.3.0.RELEASE</version>
+                       <groupId>org.argeo.dep.osgi</groupId>
+                       <artifactId>org.argeo.dep.osgi.springframework.ldap</artifactId>
+                       <version>1.3.0.0002</version>
                </dependency>
 
                <!-- Security -->
                        <artifactId>org.springframework.security</artifactId>
                </dependency>
 
+               <!-- Logging -->
+               <dependency>
+                       <groupId>org.slf4j</groupId>
+                       <artifactId>com.springsource.slf4j.org.apache.commons.logging</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.log4j</groupId>
+                       <artifactId>com.springsource.org.apache.log4j</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.slf4j</groupId>
+                       <artifactId>com.springsource.slf4j.log4j</artifactId>
+               </dependency>
+
                <!-- JSON  -->
                <dependency>
                        <groupId>org.codehaus.jackson</groupId>
index 1f82ee949ae639ac837a95472f1227f1de90b3d7..b20bac447eaf9b5c3e4a6554498b0bfb668af5fb 100644 (file)
@@ -5,9 +5,12 @@ import java.util.Collections;
 import java.util.List;
 
 import org.argeo.security.ArgeoUser;
+import org.argeo.security.BasicArgeoUser;
 import org.argeo.security.UserNature;
+import org.springframework.security.Authentication;
 import org.springframework.security.GrantedAuthority;
 import org.springframework.security.userdetails.User;
+import org.springframework.security.userdetails.UserDetails;
 
 public class ArgeoUserDetails extends User implements ArgeoUser {
        private static final long serialVersionUID = 1L;
@@ -20,13 +23,10 @@ public class ArgeoUserDetails extends User implements ArgeoUser {
                        throws IllegalArgumentException {
                super(username, password, true, true, true, true, authorities);
                this.userNatures = Collections.unmodifiableList(userNatures);
-               
+
                // Roles
-               List<String> roles = new ArrayList<String>();
-               for (GrantedAuthority authority : getAuthorities()) {
-                       roles.add(authority.getAuthority());
-               }
-               this.roles = Collections.unmodifiableList(roles);
+               this.roles = Collections.unmodifiableList(addAuthoritiesToRoles(
+                               getAuthorities(), new ArrayList<String>()));
        }
 
        public List<UserNature> getUserNatures() {
@@ -36,4 +36,30 @@ public class ArgeoUserDetails extends User implements ArgeoUser {
        public List<String> getRoles() {
                return roles;
        }
+
+       /** The provided list, for chaining using {@link Collections} */
+       protected static List<String> addAuthoritiesToRoles(
+                       GrantedAuthority[] authorities, List<String> roles) {
+               for (GrantedAuthority authority : authorities) {
+                       roles.add(authority.getAuthority());
+               }
+               return roles;
+       }
+
+       public static BasicArgeoUser createBasicArgeoUser(UserDetails userDetails) {
+               BasicArgeoUser argeoUser = new BasicArgeoUser();
+               argeoUser.setUsername(userDetails.getUsername());
+               addAuthoritiesToRoles(userDetails.getAuthorities(), argeoUser
+                               .getRoles());
+               return argeoUser;
+       }
+
+       public static BasicArgeoUser createBasicArgeoUser(
+                       Authentication authentication) {
+               BasicArgeoUser argeoUser = new BasicArgeoUser();
+               argeoUser.setUsername(authentication.getName());
+               addAuthoritiesToRoles(authentication.getAuthorities(), argeoUser
+                               .getRoles());
+               return argeoUser;
+       }
 }
index e6644fc83a8235221646c7f231cf77c38c15e073..0fe4c2f516774c90bc9f5613a17c2771dcf3d8b2 100644 (file)
@@ -30,7 +30,7 @@
                                <configuration>
                                        <instructions>
                                                <Export-Package>
-                                                       org.argeo.security.*
+                                                       org.argeo.security.mvc.*
                                                </Export-Package>
                                        </instructions>
                                </configuration>
                </plugins>
        </build>
        <dependencies>
+               <!-- Argeo Security -->
+               <dependency>
+                       <groupId>org.argeo.commons.security</groupId>
+                       <artifactId>org.argeo.security.core</artifactId>
+                       <version>0.1.1-SNAPSHOT</version>
+               </dependency>
+       
                <!-- Spring -->
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>com.springsource.javax.servlet</artifactId>
                </dependency>
 
-               <!-- Security -->
-               <dependency>
-                       <groupId>org.springframework.security</groupId>
-                       <artifactId>org.springframework.security</artifactId>
-               </dependency>
-
-               <dependency>
-                       <groupId>com.springsource.json</groupId>
-                       <artifactId>com.springsource.json</artifactId>
-                       <version>1.0.1.RELEASE</version>
-               </dependency>
-               <dependency>
-                       <groupId>org.antlr</groupId>
-                       <artifactId>com.springsource.org.antlr</artifactId>
-               </dependency>
-
-               <!-- LDAP -->
-               <dependency>
-                       <groupId>org.springframework.ldap</groupId>
-                       <artifactId>org.springframework.ldap</artifactId>
-                       <version>1.3.0.RELEASE</version>
-               </dependency>
-               <dependency>
-                       <groupId>org.springframework.ldap</groupId>
-                       <artifactId>org.springframework.ldap.core.simple</artifactId>
-                       <version>1.3.0.RELEASE</version>
-               </dependency>
        </dependencies>
 </project>
diff --git a/security/runtime/org.argeo.security.mvc/src/main/java/org/argeo/security/mvc/UsersRolesController.java b/security/runtime/org.argeo.security.mvc/src/main/java/org/argeo/security/mvc/UsersRolesController.java
new file mode 100644 (file)
index 0000000..7f53345
--- /dev/null
@@ -0,0 +1,22 @@
+package org.argeo.security.mvc;
+
+import org.argeo.security.ArgeoUser;
+import org.argeo.security.core.ArgeoUserDetails;
+import org.springframework.security.Authentication;
+import org.springframework.security.context.SecurityContextHolder;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+@Controller
+public class UsersRolesController {
+
+       @RequestMapping("/getCredentials.security")
+       @ModelAttribute("getCredentials")
+       public ArgeoUser getCredentials() {
+               Authentication authentication = SecurityContextHolder.getContext()
+                               .getAuthentication();
+
+               return ArgeoUserDetails.createBasicArgeoUser(authentication);
+       }
+}
index 960812cfeca4db6253ab959fcc36ff95b2a716be..ca24e5b3a51281ac3e087e5dc1ab97d0af6d7a66 100644 (file)
@@ -13,6 +13,7 @@
        <name>Commons Security Runtime</name>
        <packaging>pom</packaging>
        <modules>
-               <module>org.argeo.security.ria</module>
+               <module>org.argeo.security.core</module>
+               <module>org.argeo.security.mvc</module>
        </modules>
 </project>
\ No newline at end of file
index 01d0fcca005e2565290b0ce40ab85e7faa3a4536..78701c1b72838e7d59bad6a80b4f56ca88bd849c 100644 (file)
@@ -7,4 +7,5 @@ Import-Package: org.springframework.security;resolution:=optional,
  org.springframework.security.providers;resolution:=optional,
  org.springframework.security.ui;resolution:=optional,
  org.springframework.security.ui.savedrequest;resolution:=optional,
- org.springframework.security.userdetails;resolution:=optional
+ org.springframework.security.userdetails;resolution:=optional,
+ org.springframework.security.context;resolution:=optional
index 57d13d1df562c49938620dc1cc7446a40797f75a..4392f9cfbdc0177335e97acb00b5fb60dcd8366f 100644 (file)
@@ -12,6 +12,7 @@
        <name>Commons Server</name>
        <packaging>pom</packaging>
        <modules>
+               <module>runtime</module>
                <module>modules</module>
        </modules>
 </project>
\ No newline at end of file
diff --git a/server/runtime/org.argeo.server.core/.classpath b/server/runtime/org.argeo.server.core/.classpath
new file mode 100644 (file)
index 0000000..16f01e2
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+       <classpathentry kind="src" output="target/classes" path="src/main/java"/>
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+       <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
+       <classpathentry kind="output" path="target/classes"/>
+</classpath>
diff --git a/server/runtime/org.argeo.server.core/.project b/server/runtime/org.argeo.server.core/.project
new file mode 100644 (file)
index 0000000..1856520
--- /dev/null
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>org.argeo.server.core</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>org.eclipse.jdt.core.javabuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.maven.ide.eclipse.maven2Builder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>org.maven.ide.eclipse.maven2Nature</nature>
+               <nature>org.eclipse.jdt.core.javanature</nature>
+       </natures>
+</projectDescription>
diff --git a/server/runtime/org.argeo.server.core/.settings/org.eclipse.jdt.core.prefs b/server/runtime/org.argeo.server.core/.settings/org.eclipse.jdt.core.prefs
new file mode 100644 (file)
index 0000000..2a55b73
--- /dev/null
@@ -0,0 +1,5 @@
+#Wed Sep 16 10:02:19 CEST 2009
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.source=1.5
diff --git a/server/runtime/org.argeo.server.core/.settings/org.maven.ide.eclipse.prefs b/server/runtime/org.argeo.server.core/.settings/org.maven.ide.eclipse.prefs
new file mode 100644 (file)
index 0000000..fb7d9a8
--- /dev/null
@@ -0,0 +1,9 @@
+#Wed Sep 16 10:02:18 CEST 2009
+activeProfiles=
+eclipse.preferences.version=1
+fullBuildGoals=process-test-resources
+includeModules=false
+resolveWorkspaceProjects=true
+resourceFilterGoals=process-resources resources\:testResources
+skipCompilerPlugin=true
+version=1
diff --git a/server/runtime/org.argeo.server.core/pom.xml b/server/runtime/org.argeo.server.core/pom.xml
new file mode 100644 (file)
index 0000000..51d1267
--- /dev/null
@@ -0,0 +1,60 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+       <modelVersion>4.0.0</modelVersion>
+       <parent>
+               <groupId>org.argeo.commons.server</groupId>
+               <artifactId>runtime</artifactId>
+               <version>0.1.1-SNAPSHOT</version>
+               <relativePath>..</relativePath>
+       </parent>
+       <artifactId>org.argeo.server.core</artifactId>
+       <name>Commons Server Core</name>
+       <build>
+               <plugins>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-compiler-plugin</artifactId>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-source-plugin</artifactId>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-jar-plugin</artifactId>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.apache.felix</groupId>
+                               <artifactId>maven-bundle-plugin</artifactId>
+                               <version>${version.maven-bundle-plugin}</version>
+                               <configuration>
+                                       <instructions>
+                                               <Export-Package>
+                                                       org.argeo.server.*
+                                               </Export-Package>
+                                       </instructions>
+                               </configuration>
+                       </plugin>
+               </plugins>
+       </build>
+       <dependencies>
+               <!-- Apache Commons -->
+               <dependency>
+                       <groupId>org.apache.commons</groupId>
+                       <artifactId>com.springsource.org.apache.commons.io</artifactId>
+               </dependency>
+
+               <!-- Spring -->
+               <dependency>
+                       <groupId>org.springframework</groupId>
+                       <artifactId>org.springframework.web.servlet</artifactId>
+               </dependency>
+
+               <!-- J2EE -->
+               <dependency>
+                       <groupId>javax.servlet</groupId>
+                       <artifactId>com.springsource.javax.servlet</artifactId>
+               </dependency>
+
+       </dependencies>
+</project>
diff --git a/server/runtime/org.argeo.server.core/src/main/java/org/argeo/server/ArgeoServerException.java b/server/runtime/org.argeo.server.core/src/main/java/org/argeo/server/ArgeoServerException.java
new file mode 100644 (file)
index 0000000..f8e3fc4
--- /dev/null
@@ -0,0 +1,21 @@
+package org.argeo.server;
+
+public class ArgeoServerException extends RuntimeException {
+       private static final long serialVersionUID = 1L;
+
+       public ArgeoServerException() {
+       }
+
+       public ArgeoServerException(String message) {
+               super(message);
+       }
+
+       public ArgeoServerException(Throwable cause) {
+               super(cause);
+       }
+
+       public ArgeoServerException(String message, Throwable cause) {
+               super(message, cause);
+       }
+
+}
diff --git a/server/runtime/org.argeo.server.core/src/main/java/org/argeo/server/ServerAnswer.java b/server/runtime/org.argeo.server.core/src/main/java/org/argeo/server/ServerAnswer.java
new file mode 100644 (file)
index 0000000..c3e7476
--- /dev/null
@@ -0,0 +1,72 @@
+package org.argeo.server;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import org.apache.commons.io.IOUtils;
+
+/** Answer to an execution of a remote service which performed changes. */
+public class ServerAnswer {
+       public final static String OK = "OK";
+       public final static String ERROR = "ERROR";
+
+       private String status = OK;
+       private String message = "";
+
+       /** Canonical constructor */
+       public ServerAnswer(String status, String message) {
+               setStatus(status);
+               if (message == null)
+                       throw new ArgeoServerException("Message cannot be null");
+               this.message = message;
+       }
+
+       /** Empty constructor */
+       public ServerAnswer() {
+       }
+
+       public String getStatus() {
+               return status;
+       }
+
+       public void setStatus(String status) {
+               if (status == null || (!status.equals(OK) && !status.equals(ERROR)))
+                       throw new ArgeoServerException("Bad status format: " + status);
+               this.status = status;
+       }
+
+       public String getMessage() {
+               return message;
+       }
+
+       public void setMessage(String message) {
+               this.message = message;
+       }
+
+       public Boolean isOk() {
+               return status.equals(OK);
+       }
+
+       public Boolean isError() {
+               return status.equals(ERROR);
+       }
+
+       public static ServerAnswer error(String message) {
+               return new ServerAnswer(ERROR, message);
+       }
+
+       public static ServerAnswer error(Throwable e) {
+               StringWriter writer = new StringWriter();
+               try {
+                       e.printStackTrace(new PrintWriter(writer));
+                       return new ServerAnswer(ERROR, writer.toString());
+               } finally {
+                       IOUtils.closeQuietly(writer);
+               }
+       }
+
+       public static ServerAnswer ok(String message) {
+               return new ServerAnswer(OK, message);
+       }
+
+}
index 5b7991ba72a8a271ecc4d7c448548462b72f35d1..182f74d4cef52d6d1a727a456a9552bede28dd17 100644 (file)
                </plugins>
        </build>
        <dependencies>
+               <!-- Argeo Commons -->
+               <dependency>
+                       <groupId>org.argeo.commons.server</groupId>
+                       <artifactId>org.argeo.server.core</artifactId>
+                       <version>0.1.1-SNAPSHOT</version>
+               </dependency>
+
                <!-- JSON  -->
                <dependency>
                        <groupId>org.codehaus.jackson</groupId>
index fcd070a4aa780622b899ceab9507f6207c89c864..2d342e9b3eaf2da1840a1c9ba8501be89b8fe80c 100644 (file)
@@ -1,17 +1,25 @@
-package org.argeo.slc.web.mvc;
+package org.argeo.server.json.mvc;
 
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.argeo.server.ArgeoServerException;
+import org.codehaus.jackson.JsonFactory;
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.map.ObjectMapper;
 import org.springframework.web.servlet.view.AbstractView;
 
 /** Marshal one of the object of the map to the output. */
 public class JsonView extends AbstractView {
+       private JsonFactory jsonFactory = new JsonFactory();
+       private ObjectMapper objectMapper = new ObjectMapper();
+
        private String modelKey = null;
 
-       public JsonView() {
+       public JsonView(String modelKey) {
+               this.modelKey = modelKey;
        }
 
        @Override
@@ -20,24 +28,26 @@ public class JsonView extends AbstractView {
                        HttpServletRequest request, HttpServletResponse response)
                        throws Exception {
                final Object answer;
-               if (modelKey != null) {
+               if (model.size() == 1)
+                       answer = model.values().iterator().next();
+               else if (modelKey != null) {
                        if (!model.containsKey(modelKey))
-                               throw new SlcException("Key " + modelKey
+                               throw new ArgeoServerException("Key " + modelKey
                                                + " not found in model.");
                        answer = model.get(modelKey);
-               } else {
-                       if (model.size() != 1)
-                               throw new SlcException(
-                                               "Model has a size different from 1. Specify a modelKey.");
-                       answer = model.values().iterator().next();
+               } else {// models.size!=1 and no modelKey
+                       throw new ArgeoServerException(
+                                       "Model has a size different from 1. Specify a modelKey.");
                }
 
-               if (answer instanceof JSONObject) {
-                       ((JSONObject) answer).write(response.getWriter());
-               } else {
-                       JSONObject jsonObject = new JSONObject(answer);
-                       jsonObject.write(response.getWriter());
-               }
+               response.setContentType("application/json");
+
+               JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(response
+                               .getWriter());
+               jsonGenerator.useDefaultPrettyPrinter();
+
+               objectMapper.writeValue(jsonGenerator, answer);
+
        }
 
        public void setModelKey(String modelKey) {
diff --git a/server/runtime/org.argeo.server.json/src/main/java/org/argeo/server/json/mvc/JsonViewResolver.java b/server/runtime/org.argeo.server.json/src/main/java/org/argeo/server/json/mvc/JsonViewResolver.java
new file mode 100644 (file)
index 0000000..a3b5e0d
--- /dev/null
@@ -0,0 +1,18 @@
+package org.argeo.server.json.mvc;
+
+import java.util.Locale;
+
+import org.springframework.web.servlet.View;
+import org.springframework.web.servlet.view.AbstractCachingViewResolver;
+
+/**
+ * Returns a {@link JsonView} based on the underlying. View name is the model
+ * key of the JSON view.
+ */
+public class JsonViewResolver extends AbstractCachingViewResolver {
+       @Override
+       protected View loadView(String viewName, Locale locale) throws Exception {
+               return new JsonView(viewName);
+       }
+
+}
index 73717be9d7fba2d7db3f5ebdb26356ca684e455a..b380102131c624085bae06ddde9a331a35ffe09c 100644 (file)
@@ -13,6 +13,7 @@
        <name>Commons Security Runtime</name>
        <packaging>pom</packaging>
        <modules>
+               <module>org.argeo.server.core</module>
                <module>org.argeo.server.json</module>
        </modules>
 </project>
\ No newline at end of file