Author: [email protected]
Date: Tue Aug 16 10:20:05 2011
New Revision: 1322

Log:


Added:
   
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/SurveyStorageProvider.java
   
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/NTLMFilterRegistrationServiceImpl.java
   
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/SurveyRESTServiceImpl.java
   sandbox/ivol/ntlm/src/main/resources/conf/
   sandbox/ivol/ntlm/src/main/resources/conf/org.amdatu.auth.ntlm.cfg
Removed:
   sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/NTLMService.java
   sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/SurveyService.java
   sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/SurveyStore.java
   
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/NTLMServiceImpl.java
   
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/SurveyServiceImpl.java
   sandbox/ivol/ntlm/src/main/resources/jsp/register.jsp
Modified:
   sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/Answer.java
   sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/AnswerResult.java
   sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/Question.java
   sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/Survey.java
   sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/SurveyResult.java
   sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/osgi/Activator.java
   
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/SurveyColumnFamilyProvider.java
   
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/SurveyStoreImpl.java
   sandbox/ivol/ntlm/src/main/resources/jsp/enquete.jsp

Added: 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/SurveyStorageProvider.java
==============================================================================
--- (empty file)
+++ 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/SurveyStorageProvider.java 
    Tue Aug 16 10:20:05 2011
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.amdatu.auth.ntlm;
+
+import java.util.List;
+
+import org.amdatu.auth.ntlm.domain.Answer;
+import org.amdatu.auth.ntlm.domain.Survey;
+import org.amdatu.auth.ntlm.domain.SurveyResult;
+import org.json.JSONException;
+
+/**
+ * This interface represents a storage provider for survey questions and 
answers.
+ * 
+ * @author ivol
+ */
+public interface SurveyStorageProvider {
+    /**
+     * Retrieves a survey by its id.
+     * @param surveyId the id of the survey to retrieve
+     * @return the survey
+     */
+    Survey getSurvey(String surveyId);
+    
+    void addAnswers(String surveyId, List<Answer> answers, String username);
+    
+    SurveyResult loadAnswers(String surveyId) throws JSONException;
+    
+    boolean hasSubmitted(String surveyId, String username);
+}

Modified: 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/Answer.java
==============================================================================
--- sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/Answer.java     
(original)
+++ sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/Answer.java     
Tue Aug 16 10:20:05 2011
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.amdatu.auth.ntlm.domain;
 
 import javax.xml.bind.annotation.XmlAccessType;
@@ -5,6 +20,7 @@
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 
+@SuppressWarnings("restriction")
 @XmlRootElement(name = "answer")
 @XmlAccessorType(XmlAccessType.NONE)
 public class Answer {

Modified: 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/AnswerResult.java
==============================================================================
--- 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/AnswerResult.java   
    (original)
+++ 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/AnswerResult.java   
    Tue Aug 16 10:20:05 2011
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.amdatu.auth.ntlm.domain;
 
 import java.util.List;
@@ -8,6 +23,7 @@
 import javax.xml.bind.annotation.XmlElements;
 import javax.xml.bind.annotation.XmlRootElement;
 
+@SuppressWarnings("restriction")
 @XmlRootElement(name = "answerResult")
 public class AnswerResult {
     @XmlAttribute(name = "id")

Modified: 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/Question.java
==============================================================================
--- sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/Question.java   
(original)
+++ sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/Question.java   
Tue Aug 16 10:20:05 2011
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.amdatu.auth.ntlm.domain;
 
 import java.util.ArrayList;
@@ -15,6 +30,12 @@
 import org.json.JSONException;
 import org.json.JSONObject;
 
+/**
+ * Represents a single question in a survey.
+ * @author ivol
+ *
+ */
+@SuppressWarnings("restriction")
 @XmlRootElement(name = "question")
 @XmlAccessorType(XmlAccessType.NONE)
 public class Question {

Modified: 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/Survey.java
==============================================================================
--- sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/Survey.java     
(original)
+++ sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/Survey.java     
Tue Aug 16 10:20:05 2011
@@ -1,6 +1,19 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.amdatu.auth.ntlm.domain;
-
-import java.util.ArrayList;
 import java.util.List;
 
 import javax.xml.bind.annotation.XmlAccessType;
@@ -9,19 +22,20 @@
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
-import me.prettyprint.hector.api.beans.HColumn;
-import me.prettyprint.hector.api.beans.HSuperColumn;
-
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-
+/**
+ * This class represents a survey. A survey is not much more then a list of 
questions.
+ * Answers are not contained by this survey.
+ * @author ivol
+ */
+@SuppressWarnings("restriction")
 @XmlRootElement(name = "survey")
 @XmlAccessorType(XmlAccessType.NONE)
 public class Survey {
+    // Identifier of the survey
     @XmlAttribute(name = "id")
     private String m_id;
 
+    // List of questions
     @XmlElement(name = "question")
     private List<Question> m_questions;
 
@@ -52,21 +66,4 @@
         }
         return null;
     }
-
-    public void loadQuestions(HSuperColumn<String, String, String> questionSC) 
throws JSONException {
-        List<HColumn<String, String>> questions = questionSC.getColumns();
-        m_questions = new ArrayList<Question>();
-        for (HColumn<String, String> question : questions) {
-            m_questions.add(Question.load(question));
-        }
-    }
-
-    public String toJSON() throws JSONException {
-        JSONObject survey = new JSONObject();
-        survey.put("id", getId());
-        JSONArray questions = new JSONArray(getQuestions());
-
-        survey.put("questions", questions);
-        return new JSONObject().put("survey", survey).toString();
-    }
 }

Modified: 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/SurveyResult.java
==============================================================================
--- 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/SurveyResult.java   
    (original)
+++ 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/domain/SurveyResult.java   
    Tue Aug 16 10:20:05 2011
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.amdatu.auth.ntlm.domain;
 
 import java.util.ArrayList;
@@ -9,7 +24,7 @@
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
-
+@SuppressWarnings("restriction")
 @XmlRootElement(name = "surveyResult")
 @XmlAccessorType(XmlAccessType.NONE)
 public class SurveyResult {

Modified: 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/osgi/Activator.java
==============================================================================
--- sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/osgi/Activator.java    
(original)
+++ sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/osgi/Activator.java    
Tue Aug 16 10:20:05 2011
@@ -18,16 +18,10 @@
 import java.util.Dictionary;
 import java.util.Hashtable;
 
-import javax.servlet.Filter;
-
-import jcifs.http.NtlmHttpFilter;
-
-import org.amdatu.auth.ntlm.NTLMService;
-import org.amdatu.auth.ntlm.SurveyService;
-import org.amdatu.auth.ntlm.SurveyStore;
-import org.amdatu.auth.ntlm.service.NTLMServiceImpl;
+import org.amdatu.auth.ntlm.SurveyStorageProvider;
+import org.amdatu.auth.ntlm.service.NTLMFilterRegistrationServiceImpl;
 import org.amdatu.auth.ntlm.service.SurveyColumnFamilyProvider;
-import org.amdatu.auth.ntlm.service.SurveyServiceImpl;
+import org.amdatu.auth.ntlm.service.SurveyRESTServiceImpl;
 import org.amdatu.auth.ntlm.service.SurveyStoreImpl;
 import org.amdatu.auth.ntlm.service.WebResourceProvider;
 import org.amdatu.cassandra.listener.ColumnFamilyProvider;
@@ -36,11 +30,10 @@
 import org.amdatu.web.httpcontext.ResourceProvider;
 import org.amdatu.web.jsp.JspSupport;
 import org.amdatu.web.resource.ResourceSupport;
+import org.amdatu.web.rest.jaxrs.RESTService;
 import org.apache.felix.dm.DependencyActivatorBase;
 import org.apache.felix.dm.DependencyManager;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.log.LogService;
 
 /**
@@ -69,56 +62,44 @@
      */
     public static final String CONTEXTID = "amdatu-ntlm";
     
-    private ServiceRegistration m_registration;
-    
     @Override
     public void init(final BundleContext context, final DependencyManager 
manager) throws Exception {
-        // Register the tweet gadget
+        // Register the Web resource provider
         Dictionary<String, Object> properties = new Hashtable<String, 
Object>();
         properties.put(DispatcherService.CONTEXT_ID_KEY, CONTEXTID);
         properties.put(JspSupport.JSP_ALIAS_KEY, JSP_ALIAS);
         properties.put(ResourceSupport.RESOURCE_ALIAS_KEY, RES_ALIAS);
-
         
manager.add(createComponent().setInterface(ResourceProvider.class.getName(), 
properties)
             .setImplementation(WebResourceProvider.class)
             
.add(createServiceDependency().setService(LogService.class).setRequired(true)));
    
         
-        // Register the NTLM HTTP filter, used to resolve tenants     
-        Dictionary<String, Object> filterProps = new Hashtable<String, 
Object>();
-        filterProps.put(Constants.SERVICE_RANKING, 0);
-        filterProps.put(DispatcherService.PATTERN_KEY, ".*");
-        filterProps.put(DispatcherService.CONTEXT_ID_KEY, Activator.CONTEXTID);
-        filterProps.put("init.jcifs.http.domainController", "bail.gx.local");
-
-        m_registration = context.registerService(Filter.class.getName(), new 
NtlmHttpFilter(), filterProps);
-       /* manager.add(
-            createComponent()
-                .setInterface(Filter.class.getName(), filterProps)
-                .setImplementation(jcifs.http.NtlmHttpFilter.class));*/
-        
+        // Register the NTLM filter
         manager.add(
             createComponent()
-                .setInterface(NTLMService.class.getName(), null)
-                .setImplementation(NTLMServiceImpl.class));
+                .setImplementation(NTLMFilterRegistrationServiceImpl.class)
+                
.add(createServiceDependency().setService(LogService.class).setRequired(true))
+                
.add(createConfigurationDependency().setPid(NTLMFilterRegistrationServiceImpl.PID)));
         
+        // Register the survey REST service
         manager.add(
             createComponent()
-                .setInterface(SurveyService.class.getName(), null)
-                .setImplementation(SurveyServiceImpl.class)
-                
.add(createServiceDependency().setService(SurveyStore.class).setRequired(true)));
        
+                .setInterface(RESTService.class.getName(), null)
+                .setImplementation(SurveyRESTServiceImpl.class)
+                
.add(createServiceDependency().setService(SurveyStorageProvider.class).setRequired(true)));
        
         
+        // Register the Survey ColumnFamilies in Cassandra
         manager.add(createComponent()
             .setInterface(new String[]{ColumnFamilyProvider.class.getName()}, 
null)
             .setImplementation(SurveyColumnFamilyProvider.class));
         
+     // Register the survey store
         manager.add(createComponent()
-            .setInterface(new String[]{SurveyStore.class.getName()}, null)
+            .setInterface(new String[]{SurveyStorageProvider.class.getName()}, 
null)
             .setImplementation(SurveyStoreImpl.class)
             
.add(createServiceDependency().setService(CassandraPersistenceManager.class).setRequired(true)));
     }
 
     @Override
     public void destroy(final BundleContext context, final DependencyManager 
manager) throws Exception {
-        m_registration.unregister();
     }
 }

Added: 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/NTLMFilterRegistrationServiceImpl.java
==============================================================================
--- (empty file)
+++ 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/NTLMFilterRegistrationServiceImpl.java
 Tue Aug 16 10:20:05 2011
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.amdatu.auth.ntlm.service;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.List;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+import jcifs.http.NtlmHttpFilter;
+
+import org.amdatu.auth.ntlm.osgi.Activator;
+import org.amdatu.web.dispatcher.DispatcherService;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedService;
+import org.osgi.service.log.LogService;
+
+public class NTLMFilterRegistrationServiceImpl implements ManagedService {
+    public final static String PID = "org.amdatu.auth.ntlm";
+    public final static String NTLM_SESION_KEY = "NtlmHttpAuth";
+
+    private volatile BundleContext m_bundleContext;
+    private volatile LogService m_logService;
+    private ServiceRegistration m_filterRegistration;
+
+    @SuppressWarnings("rawtypes")
+    private volatile Dictionary m_filterProperties = null;
+    
+    private List<HttpSession> m_sessions = new ArrayList<HttpSession>();
+
+    public void start() {
+        // Register the NTLM HTTP filter, used to resolve tenants
+        registerFilter();
+        
+        m_logService.log(LogService.LOG_INFO, "JCIFS NTLM filter registered.");
+    }
+
+    public void stop() {
+        unregisterFilter();
+        
+        m_logService.log(LogService.LOG_INFO, "JCIFS NTLM filter 
unregistered.");
+    }
+
+    private void registerFilter() {
+        setExtraFilterProperties();
+        m_filterRegistration = 
m_bundleContext.registerService(Filter.class.getName(), new CustomNtlmFilter(), 
m_filterProperties);
+    }
+    
+    private void unregisterFilter() {
+        for (HttpSession session : m_sessions) {
+            session.setAttribute(NTLM_SESION_KEY, null);
+        }
+        m_filterRegistration.unregister();
+    }
+
+    @SuppressWarnings("unchecked")
+    private void setExtraFilterProperties() {
+        m_filterProperties.put(Constants.SERVICE_RANKING, 0);
+        m_filterProperties.put(DispatcherService.PATTERN_KEY, Activator.ALIAS 
+ "/.*");
+        m_filterProperties.put(DispatcherService.CONTEXT_ID_KEY, 
Activator.CONTEXTID);
+    }
+
+    @SuppressWarnings("rawtypes")
+    public void updated(final Dictionary dictionary) throws 
ConfigurationException {
+        if (dictionary != null) {
+            if (m_filterProperties == null) {
+                m_filterProperties = dictionary;
+            }
+            else {
+                m_filterProperties = dictionary;
+
+                // TODO: restart the filter
+                unregisterFilter();
+                registerFilter();
+
+            }
+        }
+    }
+    
+    class CustomNtlmFilter extends NtlmHttpFilter {
+        public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain) throws IOException,
+            ServletException {
+            super.doFilter(request, response, chain);
+            
+            // We keep all Http sessions that passed the NTLM filter. This is 
necessary since
+            // we will need to remove the NtlmPasswordAuthentication attribute 
from the session
+            // when this bundle is updated in order to prevent 
ClassCastExceptions in the NtlmHttpFilter,
+            // reading the attribute from the session which still is loaded by 
the old bundle classloader.
+            HttpSession session = ((HttpServletRequest) request).getSession();
+            if (!m_sessions.contains(session)) {
+                m_sessions.add(session);
+            }
+        }
+    }
+}

Modified: 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/SurveyColumnFamilyProvider.java
==============================================================================
--- 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/SurveyColumnFamilyProvider.java
        (original)
+++ 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/SurveyColumnFamilyProvider.java
        Tue Aug 16 10:20:05 2011
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.amdatu.auth.ntlm.service;
 
 import org.amdatu.cassandra.listener.ColumnFamilyDefinition;

Added: 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/SurveyRESTServiceImpl.java
==============================================================================
--- (empty file)
+++ 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/SurveyRESTServiceImpl.java
     Tue Aug 16 10:20:05 2011
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.amdatu.auth.ntlm.service;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.CacheControl;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+
+import jcifs.smb.NtlmPasswordAuthentication;
+
+import org.amdatu.auth.ntlm.SurveyStorageProvider;
+import org.amdatu.auth.ntlm.domain.Answer;
+import org.amdatu.auth.ntlm.domain.Survey;
+import org.amdatu.auth.ntlm.domain.SurveyResult;
+import org.amdatu.web.rest.jaxrs.RESTService;
+
+@Path("survey")
+public class SurveyRESTServiceImpl implements RESTService {
+    // HTTP caching policy for this REST interface
+    private static CacheControl NO_CACHE_CONTROL;
+    static {
+        NO_CACHE_CONTROL = new CacheControl();
+        NO_CACHE_CONTROL.setNoCache(true);
+    }
+    private volatile SurveyStorageProvider m_surveyStore;
+
+    @GET
+    @Path("submitted/{id}")
+    @Produces({MediaType.TEXT_PLAIN})
+    public Response hasSubmitted(@Context final HttpServletRequest request, 
@PathParam("id") final String surveyId) {
+        NtlmPasswordAuthentication session =
+            (NtlmPasswordAuthentication) 
request.getSession().getAttribute(NTLMFilterRegistrationServiceImpl.NTLM_SESION_KEY);
+        if (session != null) {
+            String userName = session.getUsername();
+            if (userName != null) {
+                String result;
+                if (m_surveyStore.hasSubmitted(surveyId, userName)) {
+                    result = "1";
+                }
+                else {
+                    result = "0";
+                }
+                return 
Response.ok(result).cacheControl(NO_CACHE_CONTROL).build();
+            }
+        }
+        return 
Response.status(Status.UNAUTHORIZED).cacheControl(NO_CACHE_CONTROL).build();
+    }
+
+    @GET
+    @Path("username")
+    @Produces({MediaType.TEXT_PLAIN})
+    public Response status(@Context final HttpServletRequest request) {
+        NtlmPasswordAuthentication session =
+            (NtlmPasswordAuthentication) 
request.getSession().getAttribute(NTLMFilterRegistrationServiceImpl.NTLM_SESION_KEY);
+        if (session != null) {
+            String username = session.getUsername();
+            if (username != null) {
+                return 
Response.ok(username).cacheControl(NO_CACHE_CONTROL).build();
+            }
+        }
+        return 
Response.status(Status.UNAUTHORIZED).cacheControl(NO_CACHE_CONTROL).build();
+    }
+
+    @POST
+    @Consumes("application/x-www-form-urlencoded")
+    public void addSurvey(@Context final HttpServletRequest request) {
+        // Get the username from NTLM authentication
+        NtlmPasswordAuthentication session =
+            (NtlmPasswordAuthentication) 
request.getSession().getAttribute("NtlmHttpAuth");
+        String userName = session.getUsername();
+        if (userName != null && !"".equals(userName)) {
+            try {
+                Map parameters = request.getParameterMap();
+                String surveyId = ((String[]) parameters.get("surveyId"))[0];
+                List<Answer> answers = new ArrayList<Answer>();
+                for (Object key : parameters.keySet()) {
+                    if (!"surveyId".equals(key.toString())) {
+                        // Answers can be multi-valued
+                        String[] value = ((String[]) parameters.get(key));
+                        answers.add(new Answer(key.toString(), value));
+                    }
+                }
+
+                m_surveyStore.addAnswers(surveyId, answers, userName);
+            }
+            catch (Exception e) {
+                throw new WebApplicationException(e, 
Response.Status.INTERNAL_SERVER_ERROR);
+            }
+        }
+    }
+
+    @GET
+    @Path("answers/{id}")
+    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+    public Response getSurveyAnswers(@Context final HttpServletRequest 
request, @PathParam("id") final String surveyId) {
+        try {
+            SurveyResult survey = m_surveyStore.loadAnswers(surveyId);
+            return Response.ok(survey).cacheControl(NO_CACHE_CONTROL).build();
+        }
+        catch (Exception e) {
+            throw new WebApplicationException(e, 
Response.Status.INTERNAL_SERVER_ERROR);
+        }
+    }
+
+    @GET
+    @Path("questions/{id}")
+    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+    public Response getSurvey(@Context final HttpServletRequest request, 
@PathParam("id") final String surveyId) {
+        try {
+            Survey survey = m_surveyStore.getSurvey(surveyId);
+            return Response.ok(survey).cacheControl(NO_CACHE_CONTROL).build();
+        }
+        catch (Exception e) {
+            throw new WebApplicationException(e, 
Response.Status.INTERNAL_SERVER_ERROR);
+        }
+    }
+
+}

Modified: 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/SurveyStoreImpl.java
==============================================================================
--- 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/SurveyStoreImpl.java
   (original)
+++ 
sandbox/ivol/ntlm/src/main/java/org/amdatu/auth/ntlm/service/SurveyStoreImpl.java
   Tue Aug 16 10:20:05 2011
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.amdatu.auth.ntlm.service;
 
 import java.util.ArrayList;
@@ -8,7 +23,7 @@
 import me.prettyprint.hector.api.beans.HColumn;
 import me.prettyprint.hector.api.beans.HSuperColumn;
 
-import org.amdatu.auth.ntlm.SurveyStore;
+import org.amdatu.auth.ntlm.SurveyStorageProvider;
 import org.amdatu.auth.ntlm.domain.Answer;
 import org.amdatu.auth.ntlm.domain.AnswerResult;
 import org.amdatu.auth.ntlm.domain.Question;
@@ -16,37 +31,59 @@
 import org.amdatu.auth.ntlm.domain.SurveyResult;
 import org.amdatu.cassandra.persistencemanager.CassandraPersistenceManager;
 import org.json.JSONException;
+import org.osgi.service.log.LogService;
 
-public class SurveyStoreImpl implements SurveyStore {
+/**
+ * Cassandra implementation of a survey store.
+ * 
+ * @author ivol
+ */
+public class SurveyStoreImpl implements SurveyStorageProvider {
     public final static String SC_QUESTIONS = "Questions";
     public final static String SC_ANSWERS = "Answers";
 
     private volatile CassandraPersistenceManager m_pm;
+    private volatile LogService m_logService;
 
     public void start() throws JSONException {
         // Add a test survey
         addTestSurvey();
 
-        // Retieve the survey
-        Survey survey = loadSurvey("1");
+        // Retrieve the survey
+        Survey survey = getSurvey("1");
 
         List<Question> questions = survey.getQuestions();
         questions.get(0);
     }
 
-    public Survey loadSurvey(String surveyId) throws JSONException {
+    public Survey getSurvey(String surveyId) {
         Survey survey = new Survey();
         survey.setId(surveyId);
         List<HSuperColumn<String, String, String>> superColumns =
             m_pm.getSuperColumns(SurveyColumnFamilyProvider.CF_SURVEY, 
surveyId, String.class);
         for (HSuperColumn<String, String, String> superColumn : superColumns) {
             if (superColumn.getName().equals(SurveyStoreImpl.SC_QUESTIONS)) {
-                survey.loadQuestions(superColumn);
+                try {
+                    loadQuestions(survey, superColumn);
+                }
+                catch (JSONException e) {
+                    m_logService.log(LogService.LOG_ERROR, "Could not load 
questions for survey '" + surveyId + "'", e);
+                    return null;
+                }
             }
         }
         return survey;
     }
 
+    private void loadQuestions(Survey survey, HSuperColumn<String, String, 
String> questionSC) throws JSONException {
+        List<HColumn<String, String>> questions = questionSC.getColumns();
+        List<Question> questionList = new ArrayList<Question>();
+        for (HColumn<String, String> question : questions) {
+            questionList.add(Question.load(question));
+        }
+        survey.setQuestions(questionList);
+    }
+
     public boolean hasSubmitted(String surveyId, String username) {
         String submitted =
             m_pm.getValue(SurveyColumnFamilyProvider.CF_SURVEY_USERS, 
surveyId, null, username, String.class);
@@ -82,7 +119,7 @@
     public SurveyResult loadAnswers(String surveyId) throws JSONException {
         List<HSuperColumn<String, String, String[]>> superColumns =
             m_pm.getSuperColumns(SurveyColumnFamilyProvider.CF_SURVEY_ANSWERS, 
surveyId, String[].class);
-        Survey survey = loadSurvey(surveyId);
+        Survey survey = getSurvey(surveyId);
 
         // Each SuperColumn represents a submitted survey including answers
         Map<Question, List<String[]>> results = new HashMap<Question, 
List<String[]>>();
@@ -121,7 +158,7 @@
         }
         return surveyResult;
     }
-    
+
     private List<String> toSingleList(List<String[]> list) {
         List<String> values = new ArrayList<String>();
         for (String[] v : list) {
@@ -129,7 +166,7 @@
         }
         return values;
     }
-    
+
     private List<String> getStats(List<String> values, List<String[]> answers) 
{
         List<String> stats = new ArrayList<String>();
         for (String value : values) {

Added: sandbox/ivol/ntlm/src/main/resources/conf/org.amdatu.auth.ntlm.cfg
==============================================================================
--- (empty file)
+++ sandbox/ivol/ntlm/src/main/resources/conf/org.amdatu.auth.ntlm.cfg  Tue Aug 
16 10:20:05 2011
@@ -0,0 +1,5 @@
+init.jcifs.http.domainController=bail.gx.local
+init.jcifs.util.loglevel=4
+init.jcifs.smb.client.domain=GXLOCAL
+init.jcifs.smb.client.username=ivol
+init.jcifs.smb.client.password=***
\ No newline at end of file

Modified: sandbox/ivol/ntlm/src/main/resources/jsp/enquete.jsp
==============================================================================
--- sandbox/ivol/ntlm/src/main/resources/jsp/enquete.jsp        (original)
+++ sandbox/ivol/ntlm/src/main/resources/jsp/enquete.jsp        Tue Aug 16 
10:20:05 2011
@@ -130,7 +130,7 @@
 
   function questionToHtml(id, des, type, values) {
     var html = "<li>";
-    html += des + <br/>;
+    html += des + "<br/>";
     if (type == "radio") {
       for (j=0; j<values.length; j++) {
         html += "<br/><input type='radio' name='" + id + "' value='" + 
values[j] + "'>" + values[j];
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to