You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by tb...@apache.org on 2013/12/19 16:32:00 UTC

[49/50] [abbrv] git commit: [OLINGO-67] develop

[OLINGO-67] develop


Project: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/commit/07a1c45a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/tree/07a1c45a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/diff/07a1c45a

Branch: refs/heads/ODataServlet
Commit: 07a1c45af1865423bbe5bfe89c6d9052f1295daa
Parents: f3d7800
Author: Tamara Boehm <ta...@sap.com>
Authored: Thu Dec 19 16:09:28 2013 +0100
Committer: Tamara Boehm <ta...@sap.com>
Committed: Thu Dec 19 16:09:28 2013 +0100

----------------------------------------------------------------------
 .../odata2/core/servlet/ODataServlet.java       | 143 ++++++++++++-------
 .../olingo/odata2/core/servlet/RestUtil.java    | 106 +++++++++-----
 2 files changed, 159 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/07a1c45a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
index d296915..e5c9760 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
@@ -2,8 +2,6 @@ package org.apache.olingo.odata2.core.servlet;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.Reader;
-import java.io.StringReader;
 
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServlet;
@@ -18,6 +16,9 @@ import org.apache.olingo.odata2.api.commons.ODataHttpMethod;
 import org.apache.olingo.odata2.api.exception.MessageReference;
 import org.apache.olingo.odata2.api.exception.ODataBadRequestException;
 import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.api.exception.ODataHttpException;
+import org.apache.olingo.odata2.api.exception.ODataMethodNotAllowedException;
+import org.apache.olingo.odata2.api.exception.ODataNotAcceptableException;
 import org.apache.olingo.odata2.api.exception.ODataNotImplementedException;
 import org.apache.olingo.odata2.api.processor.ODataContext;
 import org.apache.olingo.odata2.api.processor.ODataRequest;
@@ -63,45 +64,28 @@ public class ODataServlet extends HttpServlet {
     if (req.getPathInfo() == null) {
       handleRedirect(req, resp);
     } else {
-      handle(req, resp, xHttpMethod);
+      handle(req, resp, xHttpMethod, xHttpMethodOverride);
     }
   }
 
-  private void handle(final HttpServletRequest req, final HttpServletResponse resp, final String xHttpMethod)
-      throws IOException {
+  private void handle(final HttpServletRequest req, final HttpServletResponse resp, final String xHttpMethod,
+      final String xHttpMethodOverride) throws IOException {
     String method = req.getMethod();
     if (ODataHttpMethod.GET.name().equals(method)) {
       handleRequest(req, ODataHttpMethod.GET, resp);
     } else if (ODataHttpMethod.POST.name().equals(method)) {
-      if (xHttpMethod == null) {
+      if (xHttpMethod == null && xHttpMethodOverride == null) {
         handleRequest(req, ODataHttpMethod.POST, resp);
+      } else if (xHttpMethod == null && xHttpMethodOverride != null) {
+        /* tunneling */
+        boolean methodHandled = methodForTunneling(req, resp, xHttpMethodOverride);
+        if (!methodHandled) {
+          createMethodNotAllowedResponse(req, ODataHttpException.COMMON, resp);
+        }
       } else {
         /* tunneling */
-        if ("MERGE".equals(xHttpMethod)) {
-          handleRequest(req, ODataHttpMethod.MERGE, resp);
-
-        } else if ("PATCH".equals(xHttpMethod)) {
-          handleRequest(req, ODataHttpMethod.PATCH, resp);
-
-        } else if ("DELETE".equals(xHttpMethod)) {
-          handleRequest(req, ODataHttpMethod.DELETE, resp);
-
-        } else if ("PUT".equals(xHttpMethod)) {
-          handleRequest(req, ODataHttpMethod.PUT, resp);
-
-        } else if ("GET".equals(xHttpMethod)) {
-          handleRequest(req, ODataHttpMethod.GET, resp);
-
-        } else if ("POST".equals(xHttpMethod)) {
-          handleRequest(req, ODataHttpMethod.POST, resp);
-
-        } else if ("HEAD".equals(xHttpMethod)) {
-          createNotImplementedResponse(req, ODataNotImplementedException.COMMON, resp);
-
-        } else if ("OPTIONS".equals(xHttpMethod)) {
-          createNotImplementedResponse(req, ODataNotImplementedException.COMMON, resp);
-
-        } else {
+        boolean methodHandled = methodForTunneling(req, resp, xHttpMethod);
+        if (!methodHandled) {
           createNotImplementedResponse(req, ODataNotImplementedException.TUNNELING, resp);
         }
       }
@@ -117,32 +101,50 @@ public class ODataServlet extends HttpServlet {
     } else if ("HEAD".equals(method) || "OPTIONS".equals(method)) {
       createNotImplementedResponse(req, ODataNotImplementedException.COMMON, resp);
     } else {
-      resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+      createMethodNotAllowedResponse(req, ODataHttpException.COMMON, resp);
     }
   }
 
-  private void handleRedirect(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
-    String method = req.getMethod();
-    if (ODataHttpMethod.GET.name().equals(method) || ODataHttpMethod.POST.name().equals(method)
-        || ODataHttpMethod.PUT.name().equals(method) || ODataHttpMethod.DELETE.name().equals(method)
-        || ODataHttpMethod.PATCH.name().equals(method) || ODataHttpMethod.MERGE.name().equals(method)
-        || "HEAD".equals(method) || "OPTIONS".equals(method)) {
-      createResponse(resp, ODataResponse.status(HttpStatusCodes.TEMPORARY_REDIRECT)
-          .header(HttpHeaders.LOCATION, "/")
-          .build());
+  private boolean methodForTunneling(final HttpServletRequest req, final HttpServletResponse resp,
+      final String xHttpMethod) throws IOException {
+    /* tunneling */
+    if (ODataHttpMethod.MERGE.name().equals(xHttpMethod)) {
+      handleRequest(req, ODataHttpMethod.MERGE, resp);
+
+    } else if (ODataHttpMethod.PATCH.name().equals(xHttpMethod)) {
+      handleRequest(req, ODataHttpMethod.PATCH, resp);
+
+    } else if (ODataHttpMethod.DELETE.name().equals(xHttpMethod)) {
+      handleRequest(req, ODataHttpMethod.DELETE, resp);
+
+    } else if (ODataHttpMethod.PUT.name().equals(xHttpMethod)) {
+      handleRequest(req, ODataHttpMethod.PUT, resp);
+
+    } else if (ODataHttpMethod.GET.name().equals(xHttpMethod)) {
+      handleRequest(req, ODataHttpMethod.GET, resp);
+
+    } else if (ODataHttpMethod.POST.name().equals(xHttpMethod)) {
+      handleRequest(req, ODataHttpMethod.POST, resp);
+
+    } else if ("HEAD".equals(xHttpMethod) || "OPTIONS".equals(xHttpMethod)) {
+      createNotImplementedResponse(req, ODataNotImplementedException.COMMON, resp);
+
     } else {
-      resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+      return false;
     }
-
+    return true;
   }
 
   private void handleRequest(final HttpServletRequest req, final ODataHttpMethod method, final HttpServletResponse resp)
       throws IOException {
     try {
+      if (req.getHeader(HttpHeaders.ACCEPT) != null && req.getHeader(HttpHeaders.ACCEPT).isEmpty()) {
+        createNotAcceptableResponse(req, ODataNotAcceptableException.COMMON, resp);
+      }
       ODataRequest request = ODataRequest.method(method)
           .contentType(RestUtil.extractRequestContentType(req.getContentType()).toContentTypeString())
-          .acceptHeaders(RestUtil.extractAcceptHeaders(req.getHeader("Accept")))
-          .acceptableLanguages(RestUtil.extractAcceptableLanguage(req.getHeader("Accept-Language")))
+          .acceptHeaders(RestUtil.extractAcceptHeaders(req.getHeader(HttpHeaders.ACCEPT)))
+          .acceptableLanguages(RestUtil.extractAcceptableLanguage(req.getHeader(HttpHeaders.ACCEPT_LANGUAGE)))
           .pathInfo(RestUtil.buildODataPathInfo(req, pathSplit))
           .queryParameters(RestUtil.extractQueryParameters(req.getQueryString()))
           .requestHeaders(RestUtil.extractHeaders(req))
@@ -162,6 +164,26 @@ public class ODataServlet extends HttpServlet {
     }
   }
 
+  private void handleRedirect(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
+    String method = req.getMethod();
+    if (ODataHttpMethod.GET.name().equals(method) ||
+        ODataHttpMethod.POST.name().equals(method) ||
+        ODataHttpMethod.PUT.name().equals(method) ||
+        ODataHttpMethod.DELETE.name().equals(method) ||
+        ODataHttpMethod.PATCH.name().equals(method) ||
+        ODataHttpMethod.MERGE.name().equals(method) ||
+        "HEAD".equals(method) ||
+        "OPTIONS".equals(method)) {
+      ODataResponse odataResponse = ODataResponse.status(HttpStatusCodes.TEMPORARY_REDIRECT)
+          .header(HttpHeaders.LOCATION, "/")
+          .build();
+      createResponse(resp, odataResponse);
+    } else {
+      createMethodNotAllowedResponse(req, ODataHttpException.COMMON, resp);
+    }
+
+  }
+
   private void createResponse(final HttpServletResponse resp, final ODataResponse response) throws IOException {
 
     resp.setStatus(response.getStatus().getStatusCode());
@@ -175,19 +197,17 @@ public class ODataServlet extends HttpServlet {
       ServletOutputStream out = resp.getOutputStream();
       int curByte = -1;
       if (entity instanceof InputStream) {
-
         while ((curByte = ((InputStream) entity).read()) != -1) {
           out.write((char) curByte);
         }
-        out.flush();
-        out.close();
+        ((InputStream) entity).close();
       } else if (entity instanceof String) {
-        Reader sr = new StringReader((String) entity);
-        while ((curByte = sr.read()) > -1) {
-          out.write((char) curByte);
-        }
-        out.flush();
+        String body = (String) entity;
+        byte[] byteArray = body.getBytes("utf-8");
+        out.write(byteArray);
       }
+
+      out.flush();
       out.close();
     }
   }
@@ -202,4 +222,21 @@ public class ODataServlet extends HttpServlet {
     createResponse(resp, response);
   }
 
+  private void createMethodNotAllowedResponse(final HttpServletRequest req, final MessageReference messageReference,
+      final HttpServletResponse resp) throws IOException {
+    ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req);
+    ODataResponse response =
+        exceptionWrapper.wrapInExceptionResponse(new ODataMethodNotAllowedException(messageReference));
+    createResponse(resp, response);
+  }
+
+  private void createNotAcceptableResponse(final HttpServletRequest req, final MessageReference messageReference,
+      final HttpServletResponse resp) throws IOException {
+    ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req);
+    ODataResponse response =
+        exceptionWrapper.wrapInExceptionResponse(new ODataNotAcceptableException(messageReference));
+    createResponse(resp, response);
+
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/07a1c45a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/RestUtil.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/RestUtil.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/RestUtil.java
index ce493bd..ede0a88 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/RestUtil.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/RestUtil.java
@@ -12,13 +12,14 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Scanner;
 import java.util.regex.MatchResult;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import javax.servlet.http.HttpServletRequest;
-import javax.ws.rs.core.UriBuilder;
 
 import org.apache.olingo.odata2.api.exception.ODataBadRequestException;
 import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.api.exception.ODataNotFoundException;
 import org.apache.olingo.odata2.api.exception.ODataUnsupportedMediaTypeException;
 import org.apache.olingo.odata2.api.uri.PathInfo;
 import org.apache.olingo.odata2.api.uri.PathSegment;
@@ -37,6 +38,7 @@ public class RestUtil {
       .compile("((?:[a-z]{1,8})|(?:\\*))\\-?([a-zA-Z]{1,8})?");
   private static final Pattern REG_EX_ACCEPT_LANGUAGES_WITH_Q_FACTOR = Pattern.compile(REG_EX_ACCEPT_LANGUAGES + "(?:;"
       + REG_EX_OPTIONAL_WHITESPACE + REG_EX_QUALITY_FACTOR + ")?");
+  private static final Pattern REG_EX_MATRIX_PARAMETER = Pattern.compile("([^=]*)(?:=(.*))?");
 
   public static List<String> extractAcceptHeaders(final String header) {
     List<String> acceptHeaders = new ArrayList<String>();
@@ -83,28 +85,25 @@ public class RestUtil {
   private static URI buildBaseUri(final HttpServletRequest req, final List<PathSegment> precedingPathSegments)
       throws ODataException {
     try {
-      String path = req.getContextPath() + req.getServletPath();
-      UriBuilder uriBuilder = UriBuilder.fromUri(path);
+      StringBuilder stringBuilder = new StringBuilder();
+      stringBuilder.append(req.getContextPath()).append(req.getServletPath());
       for (final PathSegment ps : precedingPathSegments) {
-        uriBuilder = uriBuilder.path(ps.getPath());
+        stringBuilder.append("/").append(ps.getPath());
         for (final String key : ps.getMatrixParameters().keySet()) {
-          final Object[] v = ps.getMatrixParameters().get(key).toArray();
-          uriBuilder = uriBuilder.matrixParam(key, v);
+          List<String> matrixParameters = ps.getMatrixParameters().get(key);
+          String matrixParameterString = ";" + key + "=";
+          for (String matrixParam : matrixParameters) {
+            matrixParameterString += Decoder.decode(matrixParam) + ",";
+          }
+          stringBuilder.append(matrixParameterString.substring(0, matrixParameterString.length() - 1));
         }
       }
 
-      /*
-       * workaround because of host name is cached by uriInfo
-       */
-      uriBuilder.host(req.getServerName()).port(req.getServerPort());
-      uriBuilder.scheme(req.getScheme());
-
-      String uriString = uriBuilder.build().toString();
-      if (!uriString.endsWith("/")) {
-        uriString = uriString + "/";
+      String path = stringBuilder.toString();
+      if (!path.endsWith("/")) {
+        path = path + "/";
       }
-
-      return new URI(uriString);
+      return new URI(req.getScheme(), null, req.getServerName(), req.getServerPort(), path, null, null);
     } catch (final URISyntaxException e) {
       throw new ODataException(e);
     }
@@ -129,18 +128,15 @@ public class RestUtil {
 
   private static PathInfoImpl splitPath(final HttpServletRequest servletRequest, final int pathSplit)
       throws ODataException {
-    /* String pathInfoString = servletRequest.getContextPath()+servletRequest.getServletPath()
-     * +servletRequest.getPathInfo();*/
-    //  String pathInfoString = servletRequest.getPathInfo();
+    PathInfoImpl pathInfo = new PathInfoImpl();
+    List<String> precedingPathSegments;
+    List<String> pathSegments;
+
     String pathInfoString = extractPathInfo(servletRequest);
     while (pathInfoString.startsWith("/")) {
       pathInfoString = pathInfoString.substring(1);
     }
-    List<String> segments = Arrays.asList(pathInfoString.split("/"));
-    PathInfoImpl pathInfo = new PathInfoImpl();
-
-    List<String> precedingPathSegments;
-    List<String> pathSegments;
+    List<String> segments = Arrays.asList(pathInfoString.split("/", -1));
 
     if (pathSplit == 0) {
       precedingPathSegments = Collections.emptyList();
@@ -161,13 +157,59 @@ public class RestUtil {
 
     List<PathSegment> odataSegments = new ArrayList<PathSegment>();
     for (final String segment : pathSegments) {
-      odataSegments.add(new ODataPathSegmentImpl(segment, null));
+
+      int index = segment.indexOf(";");
+      if (index < 0) {
+        odataSegments.add(new ODataPathSegmentImpl(segment, null));
+      } else {
+        String path = segment.substring(0, index);
+        Map<String, List<String>> parameterMap = extractMatrixParameter(segment, index);
+        if (!parameterMap.get("matrix").isEmpty()) {
+          System.out.println(parameterMap.get("matrix"));
+        }
+        throw new ODataNotFoundException(ODataNotFoundException.MATRIX.addContent(parameterMap.keySet(), path));
+      }
     }
     pathInfo.setODataPathSegment(odataSegments);
-
     return pathInfo;
   }
 
+  private static List<PathSegment> convertPathSegmentList(final List<String> pathSegments) {
+    ArrayList<PathSegment> converted = new ArrayList<PathSegment>();
+    for (final String segment : pathSegments) {
+      int index = segment.indexOf(";");
+      if (index == -1) {
+        converted.add(new ODataPathSegmentImpl(Decoder.decode(segment), null));
+      } else {
+        String path = segment.substring(0, index);
+        Map<String, List<String>> parameterMap = extractMatrixParameter(segment, index);
+        converted.add(new ODataPathSegmentImpl(Decoder.decode(path), parameterMap));
+      }
+    }
+    return converted;
+  }
+
+  private static Map<String, List<String>> extractMatrixParameter(final String segment, final int index) {
+    List<String> matrixParameters = Arrays.asList(segment.substring(index + 1).split(";"));
+    String matrixParameterName = "";
+    String matrixParamaterValues = "";
+    Map<String, List<String>> parameterMap = new HashMap<String, List<String>>();
+
+    for (String matrixParameter : matrixParameters) {
+      List<String> values = Arrays.asList("");
+      Matcher matcher = REG_EX_MATRIX_PARAMETER.matcher(matrixParameter);
+      if (matcher.find()) {
+        matrixParameterName = matcher.group(1);
+        matrixParamaterValues = matcher.group(2);
+      }
+      if (matrixParamaterValues != null) {
+        values = Arrays.asList(matrixParamaterValues.split(","));
+      }
+      parameterMap.put(matrixParameterName, values);
+    }
+    return parameterMap;
+  }
+
   private static String extractPathInfo(final HttpServletRequest servletRequest) {
     String pathInfoString;
     final String requestUri = servletRequest.getRequestURI();
@@ -185,16 +227,6 @@ public class RestUtil {
     return pathInfoString;
   }
 
-  private static List<PathSegment> convertPathSegmentList(final List<String> pathSegments) {
-    ArrayList<PathSegment> converted = new ArrayList<PathSegment>();
-    for (final String pathSegment : pathSegments) {
-      final PathSegment segment =
-          new ODataPathSegmentImpl(Decoder.decode(pathSegment), null);
-      converted.add(segment);
-    }
-    return converted;
-  }
-
   public static ContentType extractRequestContentType(final String contentType)
       throws ODataUnsupportedMediaTypeException {
     if (contentType == null || contentType.isEmpty()) {