You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fm...@apache.org on 2014/06/20 21:42:43 UTC

svn commit: r1604259 - in /chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src: main/java/org/apache/chemistry/opencmis/server/impl/atompub/ main/java/org/apache/chemistry/opencmis/server/impl/browser/ main/java/or...

Author: fmui
Date: Fri Jun 20 19:42:43 2014
New Revision: 1604259

URL: http://svn.apache.org/r1604259
Log:
CMIS-811: Server: added HTTP HEAD support to AtomPub and Browser binding

Added:
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/HEADHttpServletRequestWrapper.java   (with props)
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/NoBodyHttpServletResponseWrapper.java   (with props)
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/HttpRequestHeadMethodTest.java   (with props)
Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/Dispatcher.java

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java?rev=1604259&r1=1604258&r2=1604259&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/CmisAtomPubServlet.java Fri Jun 20 19:42:43 2014
@@ -41,6 +41,7 @@ import static org.apache.chemistry.openc
 import static org.apache.chemistry.opencmis.server.impl.atompub.AbstractAtomPubServiceCall.RESOURCE_VERSIONS;
 import static org.apache.chemistry.opencmis.server.shared.Dispatcher.METHOD_DELETE;
 import static org.apache.chemistry.opencmis.server.shared.Dispatcher.METHOD_GET;
+import static org.apache.chemistry.opencmis.server.shared.Dispatcher.METHOD_HEAD;
 import static org.apache.chemistry.opencmis.server.shared.Dispatcher.METHOD_POST;
 import static org.apache.chemistry.opencmis.server.shared.Dispatcher.METHOD_PUT;
 
@@ -75,7 +76,9 @@ import org.apache.chemistry.opencmis.ser
 import org.apache.chemistry.opencmis.server.shared.AbstractCmisHttpServlet;
 import org.apache.chemistry.opencmis.server.shared.Dispatcher;
 import org.apache.chemistry.opencmis.server.shared.ExceptionHelper;
+import org.apache.chemistry.opencmis.server.shared.HEADHttpServletRequestWrapper;
 import org.apache.chemistry.opencmis.server.shared.HttpUtils;
+import org.apache.chemistry.opencmis.server.shared.NoBodyHttpServletResponseWrapper;
 import org.apache.chemistry.opencmis.server.shared.QueryStringHttpServletRequestWrapper;
 import org.apache.chemistry.opencmis.server.shared.ServiceCall;
 import org.apache.commons.lang.StringEscapeUtils;
@@ -160,7 +163,13 @@ public class CmisAtomPubServlet extends 
     @Override
     protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
             IOException {
-        QueryStringHttpServletRequestWrapper qsRequest = new QueryStringHttpServletRequestWrapper(request);
+
+        if (METHOD_HEAD.equals(request.getMethod())) {
+            request = new HEADHttpServletRequestWrapper(request);
+            response = new NoBodyHttpServletResponseWrapper(response);
+        } else {
+            request = new QueryStringHttpServletRequestWrapper(request);
+        }
 
         // set default headers
         response.addHeader("Cache-Control", "private, max-age=0");
@@ -169,8 +178,8 @@ public class CmisAtomPubServlet extends 
         // create a context object, dispatch and handle exceptions
         CallContext context = null;
         try {
-            context = createContext(getServletContext(), qsRequest, response);
-            dispatch(context, qsRequest, response);
+            context = createContext(getServletContext(), request, response);
+            dispatch(context, request, response);
         } catch (Exception e) {
             if (e instanceof CmisUnauthorizedException) {
                 response.setHeader("WWW-Authenticate", "Basic realm=\"CMIS\"");

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java?rev=1604259&r1=1604258&r2=1604259&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java Fri Jun 20 19:42:43 2014
@@ -71,6 +71,7 @@ import static org.apache.chemistry.openc
 import static org.apache.chemistry.opencmis.commons.impl.JSONConstants.ERROR_MESSAGE;
 import static org.apache.chemistry.opencmis.commons.impl.JSONConstants.ERROR_STACKTRACE;
 import static org.apache.chemistry.opencmis.server.shared.Dispatcher.METHOD_GET;
+import static org.apache.chemistry.opencmis.server.shared.Dispatcher.METHOD_HEAD;
 import static org.apache.chemistry.opencmis.server.shared.Dispatcher.METHOD_POST;
 
 import java.io.IOException;
@@ -106,7 +107,9 @@ import org.apache.chemistry.opencmis.ser
 import org.apache.chemistry.opencmis.server.shared.AbstractCmisHttpServlet;
 import org.apache.chemistry.opencmis.server.shared.Dispatcher;
 import org.apache.chemistry.opencmis.server.shared.ExceptionHelper;
+import org.apache.chemistry.opencmis.server.shared.HEADHttpServletRequestWrapper;
 import org.apache.chemistry.opencmis.server.shared.HttpUtils;
+import org.apache.chemistry.opencmis.server.shared.NoBodyHttpServletResponseWrapper;
 import org.apache.chemistry.opencmis.server.shared.QueryStringHttpServletRequestWrapper;
 import org.apache.chemistry.opencmis.server.shared.ServiceCall;
 import org.slf4j.Logger;
@@ -220,6 +223,9 @@ public class CmisBrowserBindingServlet e
                 request = new QueryStringHttpServletRequestWrapper(request);
             } else if (METHOD_POST.equals(method)) {
                 request = new POSTHttpServletRequestWrapper(request, getThresholdOutputStreamFactory());
+            } else if (METHOD_HEAD.equals(method)) {
+                request = new HEADHttpServletRequestWrapper(request);
+                response = new NoBodyHttpServletResponseWrapper(response);
             } else {
                 throw new CmisNotSupportedException("Unsupported method");
             }

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/Dispatcher.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/Dispatcher.java?rev=1604259&r1=1604258&r2=1604259&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/Dispatcher.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/Dispatcher.java Fri Jun 20 19:42:43 2014
@@ -45,6 +45,7 @@ public class Dispatcher implements Seria
     public static final String BASE_URL_ATTRIBUTE = "org.apache.chemistry.opencmis.baseurl";
 
     public static final String METHOD_GET = "GET";
+    public static final String METHOD_HEAD = "HEAD";
     public static final String METHOD_POST = "POST";
     public static final String METHOD_PUT = "PUT";
     public static final String METHOD_DELETE = "DELETE";

Added: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/HEADHttpServletRequestWrapper.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/HEADHttpServletRequestWrapper.java?rev=1604259&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/HEADHttpServletRequestWrapper.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/HEADHttpServletRequestWrapper.java Fri Jun 20 19:42:43 2014
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.chemistry.opencmis.server.shared;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletRequest;
+
+public class HEADHttpServletRequestWrapper extends QueryStringHttpServletRequestWrapper {
+
+    public HEADHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
+        super(request);
+    }
+
+    @Override
+    public String getMethod() {
+        return Dispatcher.METHOD_GET;
+    }
+}

Propchange: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/HEADHttpServletRequestWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/NoBodyHttpServletResponseWrapper.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/NoBodyHttpServletResponseWrapper.java?rev=1604259&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/NoBodyHttpServletResponseWrapper.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/NoBodyHttpServletResponseWrapper.java Fri Jun 20 19:42:43 2014
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.chemistry.opencmis.server.shared;
+
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
+public class NoBodyHttpServletResponseWrapper extends HttpServletResponseWrapper {
+
+    private final NoBodyOutputStream noBodyStream;
+    private PrintWriter writer;
+
+    public NoBodyHttpServletResponseWrapper(HttpServletResponse response) throws IOException {
+        super(response);
+        noBodyStream = new NoBodyOutputStream();
+    }
+
+    @Override
+    public ServletOutputStream getOutputStream() throws IOException {
+        return noBodyStream;
+    }
+
+    public PrintWriter getWriter() throws UnsupportedEncodingException {
+        if (writer == null) {
+            writer = new PrintWriter(new OutputStreamWriter(noBodyStream, getCharacterEncoding()));
+        }
+
+        return writer;
+    }
+
+    private class NoBodyOutputStream extends ServletOutputStream {
+        @Override
+        public void write(int b) throws IOException {
+            // ignore
+        }
+
+        @Override
+        public void write(byte[] b) throws IOException {
+            // ignore
+        }
+
+        @Override
+        public void write(byte[] b, int off, int len) throws IOException {
+            // ignore
+        }
+    }
+}

Propchange: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/NoBodyHttpServletResponseWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/HttpRequestHeadMethodTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/HttpRequestHeadMethodTest.java?rev=1604259&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/HttpRequestHeadMethodTest.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/HttpRequestHeadMethodTest.java Fri Jun 20 19:42:43 2014
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.chemistry.opencmis.server.impl;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.net.URISyntaxException;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.chemistry.opencmis.commons.data.CacheHeaderContentStream;
+import org.apache.chemistry.opencmis.commons.server.CallContext;
+import org.apache.chemistry.opencmis.commons.server.CmisService;
+import org.apache.chemistry.opencmis.commons.server.CmisServiceFactory;
+import org.apache.chemistry.opencmis.server.impl.atompub.AbstractAtomPubServiceCall;
+import org.apache.chemistry.opencmis.server.impl.atompub.CmisAtomPubServlet;
+import org.apache.chemistry.opencmis.server.impl.browser.CmisBrowserBindingServlet;
+import org.apache.chemistry.opencmis.server.shared.Dispatcher;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+public class HttpRequestHeadMethodTest {
+    private static final String CONTEXT_PATH = "/context";
+    private static final String ATOMPUB_SERVLET_PATH = "/cmisatom";
+    private static final String BROWSER_SERVLET_PATH = "/cmisjson";
+    private static final String REPOSITORY_ID = "22d2880a-bae5-4cfc-a5a9-3b2618e6e11c";
+    private static final int BACKEND_SERVER_PORT = 8080;
+    private static final String BACKEND_SERVER_NAME = "www.backend.be";
+    private static final String BACKEND_SERVER_PROTO = "http";
+
+    @Mock
+    private CmisServiceFactory cmisServiceFactory;
+
+    @Mock
+    private CmisService cmisService;
+
+    @Mock
+    private CacheHeaderContentStream contentStream;
+
+    @Mock
+    private HttpServletRequest request;
+
+    @Mock
+    private ServletConfig config;
+
+    @Mock
+    private ServletContext context;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        when(this.request.getScheme()).thenReturn(BACKEND_SERVER_PROTO);
+        when(this.request.getServerName()).thenReturn(BACKEND_SERVER_NAME);
+        when(this.request.getServerPort()).thenReturn(BACKEND_SERVER_PORT);
+        when(this.request.getContextPath()).thenReturn(CONTEXT_PATH);
+        when(this.config.getServletContext()).thenReturn(context);
+        when(this.context.getAttribute(CmisRepositoryContextListener.SERVICES_FACTORY)).thenReturn(cmisServiceFactory);
+        when(cmisServiceFactory.getService((CallContext) any())).thenReturn(cmisService);
+    }
+
+    @Test
+    public void testAtomPubHeadRequest() throws URISyntaxException, IOException, ServletException {
+        String requestURI = CONTEXT_PATH + ATOMPUB_SERVLET_PATH + "/" + REPOSITORY_ID + "/content";
+        String queryString = "id=123";
+        doTestHeadRequest(new CmisAtomPubServlet(), ATOMPUB_SERVLET_PATH, requestURI, queryString);
+    }
+
+    @Test
+    public void testBrowserBindingHeadRequest() throws URISyntaxException, IOException, ServletException {
+        String requestURI = CONTEXT_PATH + BROWSER_SERVLET_PATH + "/" + REPOSITORY_ID + "/root";
+        String queryString = "cmisselector=content&objectId=123";
+        doTestHeadRequest(new CmisBrowserBindingServlet(), BROWSER_SERVLET_PATH, requestURI, queryString);
+    }
+
+    private void doTestHeadRequest(Servlet servlet, String servletPath, String requestURI, String queryString)
+            throws URISyntaxException, IOException, ServletException {
+
+        when(this.request.getServletPath()).thenReturn(servletPath);
+        when(this.request.getRequestURI()).thenReturn(requestURI);
+        when(this.request.getQueryString()).thenReturn(queryString);
+        when(this.request.getMethod()).thenReturn(Dispatcher.METHOD_HEAD);
+
+        when(this.cmisService.getContentStream(REPOSITORY_ID, "123", null, null, null, null)).thenReturn(contentStream);
+        when(this.contentStream.getStream()).thenReturn(new ByteArrayInputStream("789".getBytes()));
+        when(this.contentStream.getETag()).thenReturn("456");
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        HttpServletResponse response = getMockResponse(baos);
+        servlet.init(config);
+        servlet.service(request, response);
+        verify(response).setStatus(HttpServletResponse.SC_OK);
+        verify(response).setHeader("ETag", "\"456\"");
+    }
+
+    private HttpServletResponse getMockResponse(OutputStream out) throws IOException {
+        HttpServletResponse resp = mock(HttpServletResponse.class);
+        ServletOutputStream sos = new StubServletOutputStream(out);
+        PrintWriter printWriter = new PrintWriter(sos);
+        when(resp.getOutputStream()).thenReturn(sos);
+        when(resp.getWriter()).thenReturn(printWriter);
+        return resp;
+    }
+
+    static class UrlServiceCall extends AbstractAtomPubServiceCall {
+        public void serve(CallContext context, CmisService service, String repositoryId, HttpServletRequest request,
+                HttpServletResponse response) throws Exception {
+            // no implementation
+        }
+    }
+
+    static class StubServletOutputStream extends ServletOutputStream {
+        private OutputStream os;
+
+        public StubServletOutputStream(OutputStream os) {
+            this.os = os;
+        }
+
+        public void write(int i) throws IOException {
+            os.write(i);
+        }
+    }
+}
\ No newline at end of file

Propchange: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/HttpRequestHeadMethodTest.java
------------------------------------------------------------------------------
    svn:eol-style = native