You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by sk...@apache.org on 2014/06/02 13:38:01 UTC
[05/12] git commit: [OLINGO-266] redirect support
[OLINGO-266] redirect support
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/44e0c649
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/44e0c649
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/44e0c649
Branch: refs/heads/master
Commit: 44e0c6495a81c467a463a10c01746b6042b3558c
Parents: ef5d977
Author: Stephan Klevenz <st...@sap.com>
Authored: Fri May 23 09:33:08 2014 +0200
Committer: Stephan Klevenz <st...@sap.com>
Committed: Fri May 23 09:33:08 2014 +0200
----------------------------------------------------------------------
.../apache/olingo/fit/tecsvc/PingITCase.java | 18 ++-
.../commons/api/http/HttpContentType.java | 57 +++++++
.../olingo/commons/api/http/HttpHeader.java | 155 +++++++++++++++++++
.../olingo/commons/api/http/HttpStatusCode.java | 99 ++++++++++++
.../olingo/server/core/DefaultProcessor.java | 19 ++-
.../apache/olingo/server/core/ODataHandler.java | 16 +-
.../server/core/ODataHttpHandlerImpl.java | 47 +++---
.../olingo/server/core/RedirectProcessor.java | 29 ++++
.../olingo/server/tecsvc/TechnicalServlet.java | 5 +-
.../olingo/server/core/ODataHandlerTest.java | 50 +++---
10 files changed, 437 insertions(+), 58 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/44e0c649/fit/src/test/java/org/apache/olingo/fit/tecsvc/PingITCase.java
----------------------------------------------------------------------
diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/PingITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/PingITCase.java
index 755e087..c13de43 100644
--- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/PingITCase.java
+++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/PingITCase.java
@@ -23,11 +23,14 @@ import static org.junit.Assert.assertEquals;
import java.net.HttpURLConnection;
import java.net.URL;
+import org.apache.olingo.commons.api.http.HttpHeader;
+import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.junit.Test;
public class PingITCase {
- private static final String REF_SERVICE = "http://localhost:9080/tecsvc/odata.svc/";
+ private static final String REF_SERVICE_REDIRECT = "http://localhost:9080/tecsvc/odata.svc";
+ private static final String REF_SERVICE = REF_SERVICE_REDIRECT + "/";
@Test
public void ping() throws Exception {
@@ -41,4 +44,17 @@ public class PingITCase {
assertEquals(200, code);
}
+ @Test
+ public void redirect() throws Exception {
+ URL url = new URL(REF_SERVICE_REDIRECT);
+
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection.setRequestMethod("GET");
+ connection.connect();
+
+ int code = connection.getResponseCode();
+
+ assertEquals(HttpStatusCode.TEMPORARY_REDIRECT, code);
+ assertEquals("/", connection.getHeaderField(HttpHeader.LOCATION));
+ }
}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/44e0c649/lib/commons-api/src/main/java/org/apache/olingo/commons/api/http/HttpContentType.java
----------------------------------------------------------------------
diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/http/HttpContentType.java b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/http/HttpContentType.java
new file mode 100644
index 0000000..6bb1081
--- /dev/null
+++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/http/HttpContentType.java
@@ -0,0 +1,57 @@
+/*******************************************************************************
+ * 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.olingo.commons.api.http;
+
+/**
+ * Constants for <code>Http Content Type</code> definitions as specified in
+ * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">RFC 2616 Section 14</a>.
+ *
+ */
+public interface HttpContentType {
+
+ public static final String APPLICATION_XML = "application/xml";
+ public static final String APPLICATION_XML_UTF8 = APPLICATION_XML + ";charset=utf-8";
+
+ public static final String APPLICATION_ATOM_XML = "application/atom+xml";
+ public static final String APPLICATION_ATOM_XML_UTF8 = APPLICATION_ATOM_XML + ";charset=utf-8";
+ public static final String APPLICATION_ATOM_XML_ENTRY = APPLICATION_ATOM_XML + ";type=entry";
+ public static final String APPLICATION_ATOM_XML_ENTRY_UTF8 = APPLICATION_ATOM_XML_ENTRY + ";charset=utf-8";
+ public static final String APPLICATION_ATOM_XML_FEED = APPLICATION_ATOM_XML + ";type=feed";
+ public static final String APPLICATION_ATOM_XML_FEED_UTF8 = APPLICATION_ATOM_XML_FEED + ";charset=utf-8";
+ public static final String APPLICATION_ATOM_SVC = "application/atomsvc+xml";
+ public static final String APPLICATION_ATOM_SVC_UTF8 = APPLICATION_ATOM_SVC + ";charset=utf-8";
+
+ public static final String APPLICATION_JSON = "application/json";
+ public static final String APPLICATION_JSON_VERBOSE = APPLICATION_JSON + ";odata=verbose";
+ public static final String APPLICATION_JSON_UTF8 = APPLICATION_JSON + ";charset=utf-8";
+ public static final String APPLICATION_JSON_UTF8_VERBOSE = APPLICATION_JSON_UTF8 + ";odata=verbose";
+
+ public static final String TEXT_PLAIN = "text/plain";
+ public static final String TEXT_PLAIN_UTF8 = TEXT_PLAIN + ";charset=utf-8";
+
+ public static final String TEXT_HTML = "text/html";
+
+ public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
+
+ public static final String APPLICATION_HTTP = "application/http";
+
+ public static final String MULTIPART_MIXED = "multipart/mixed";
+
+ public static final String WILDCARD = "*/*";
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/44e0c649/lib/commons-api/src/main/java/org/apache/olingo/commons/api/http/HttpHeader.java
----------------------------------------------------------------------
diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/http/HttpHeader.java b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/http/HttpHeader.java
new file mode 100644
index 0000000..7d1a47d
--- /dev/null
+++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/http/HttpHeader.java
@@ -0,0 +1,155 @@
+/*******************************************************************************
+ * 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.olingo.commons.api.http;
+
+/**
+ * HTTP header constants
+ *
+ *
+ */
+public interface HttpHeader {
+
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1">HTTP/1.1 documentation</a>}.
+ */
+ public static final String ACCEPT = "Accept";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.2">HTTP/1.1 documentation</a>}.
+ */
+ public static final String ACCEPT_CHARSET = "Accept-Charset";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3">HTTP/1.1 documentation</a>}.
+ */
+ public static final String ACCEPT_ENCODING = "Accept-Encoding";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4">HTTP/1.1 documentation</a>}.
+ */
+ public static final String ACCEPT_LANGUAGE = "Accept-Language";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.7">HTTP/1.1 documentation</a>}.
+ */
+ public static final String ALLOW = "Allow";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.8">HTTP/1.1 documentation</a>}.
+ */
+ public static final String AUTHORIZATION = "Authorization";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9">HTTP/1.1 documentation</a>}.
+ */
+ public static final String CACHE_CONTROL = "Cache-Control";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11">HTTP/1.1 documentation</a>}.
+ */
+ public static final String CONTENT_ENCODING = "Content-Encoding";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.12">HTTP/1.1 documentation</a>}.
+ */
+ public static final String CONTENT_LANGUAGE = "Content-Language";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13">HTTP/1.1 documentation</a>}.
+ */
+ public static final String CONTENT_LENGTH = "Content-Length";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.14">HTTP/1.1 documentation</a>}.
+ */
+ public static final String CONTENT_LOCATION = "Content-Location";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17">HTTP/1.1 documentation</a>}.
+ */
+ public static final String CONTENT_TYPE = "Content-Type";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.18">HTTP/1.1 documentation</a>}.
+ */
+ public static final String DATE = "Date";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.19">HTTP/1.1 documentation</a>}.
+ */
+ public static final String ETAG = "ETag";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21">HTTP/1.1 documentation</a>}.
+ */
+ public static final String EXPIRES = "Expires";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23">HTTP/1.1 documentation</a>}.
+ */
+ public static final String HOST = "Host";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.24">HTTP/1.1 documentation</a>}.
+ */
+ public static final String IF_MATCH = "If-Match";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.25">HTTP/1.1 documentation</a>}.
+ */
+ public static final String IF_MODIFIED_SINCE = "If-Modified-Since";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26">HTTP/1.1 documentation</a>}.
+ */
+ public static final String IF_NONE_MATCH = "If-None-Match";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.28">HTTP/1.1 documentation</a>}.
+ */
+ public static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.29">HTTP/1.1 documentation</a>}.
+ */
+ public static final String LAST_MODIFIED = "Last-Modified";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30">HTTP/1.1 documentation</a>}.
+ */
+ public static final String LOCATION = "Location";
+ /**
+ * See {@link <a href="http://tools.ietf.org/html/rfc5988#page-6">Web Linking (IETF RFC-5988) documentation</a>}.
+ */
+ public static final String LINK = "Link";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.37">HTTP/1.1 documentation</a>}.
+ */
+ public static final String RETRY_AFTER = "Retry-After";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43">HTTP/1.1 documentation</a>}.
+ */
+ public static final String USER_AGENT = "User-Agent";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.44">HTTP/1.1 documentation</a>}.
+ */
+ public static final String VARY = "Vary";
+ /**
+ * See {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.47">HTTP/1.1 documentation</a>}.
+ */
+ public static final String WWW_AUTHENTICATE = "WWW-Authenticate";
+ /**
+ * See {@link <a href="http://www.ietf.org/rfc/rfc2109.txt">IETF RFC 2109</a>}.
+ */
+ public static final String COOKIE = "Cookie";
+ /**
+ * See {@link <a href="http://www.ietf.org/rfc/rfc2109.txt">IETF RFC 2109</a>}.
+ */
+ public static final String SET_COOKIE = "Set-Cookie";
+
+ /**
+ * non standard header
+ */
+ public static final String X_HTTP_METHOD = "X-HTTP-Method";
+
+ /**
+ * non standard header
+ */
+ public static final String X_HTTP_METHOD_OVERRIDE = "X-HTTP-Method-Override";
+
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/44e0c649/lib/commons-api/src/main/java/org/apache/olingo/commons/api/http/HttpStatusCode.java
----------------------------------------------------------------------
diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/http/HttpStatusCode.java b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/http/HttpStatusCode.java
new file mode 100644
index 0000000..00e2200
--- /dev/null
+++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/http/HttpStatusCode.java
@@ -0,0 +1,99 @@
+/*******************************************************************************
+ * 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.olingo.commons.api.http;
+
+/**
+ * HTTP status codes as defined in RFC2616-sec10
+ * and additional status codes as defined in RFC6585
+ *
+ */
+public enum HttpStatusCode {
+
+ OK(200, "OK"), CREATED(201, "Created"), ACCEPTED(202, "Accepted"), NO_CONTENT(204, "No Content"), RESET_CONTENT(205,
+ "Reset Content"), PARTIAL_CONTENT(206, "Partial Content"),
+
+ MOVED_PERMANENTLY(301, "Moved Permanently"), FOUND(302, "Found"), SEE_OTHER(303, "See Other"), NOT_MODIFIED(304,
+ "Not Modified"), USE_PROXY(305, "Use Proxy"), TEMPORARY_REDIRECT(307, "Temporary Redirect"),
+
+ BAD_REQUEST(400, "Bad Request"), UNAUTHORIZED(401, "Unauthorized"), PAYMENT_REQUIRED(402, "Payment Required"),
+ FORBIDDEN(
+ 403, "Forbidden"), NOT_FOUND(404, "Not Found"), METHOD_NOT_ALLOWED(405, "Method Not Allowed"), NOT_ACCEPTABLE(
+ 406, "Not Acceptable"), PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required"), REQUEST_TIMEOUT(408,
+ "Request Timeout"), CONFLICT(409, "Conflict"), GONE(410, "Gone"), LENGTH_REQUIRED(411, "Length Required"),
+ PRECONDITION_FAILED(412, "Precondition Failed"), REQUEST_ENTITY_TOO_LARGE(413, "Request Entity Too Large"),
+ REQUEST_URI_TOO_LONG(414, "Request-URI Too Long"), UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
+ REQUESTED_RANGE_NOT_SATISFIABLE(416, "Requested Range Not Satisfiable"),
+ EXPECTATION_FAILED(417, "Expectation Failed"), PRECONDITION_REQUIRED(428, "Precondition Required"),
+
+ INTERNAL_SERVER_ERROR(500, "Internal Server Error"), NOT_IMPLEMENTED(501, "Not Implemented"), BAD_GATEWAY(502,
+ "Bad Gateway"), SERVICE_UNAVAILABLE(503, "Service Unavailable"), GATEWAY_TIMEOUT(504, "Gateway Timeout"),
+ HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version Not Supported");
+
+ private final int code;
+ private final String info;
+
+ HttpStatusCode(final int statusCode, final String info) {
+ code = statusCode;
+ this.info = info;
+ }
+
+ /**
+ * Convert a numerical status code into the corresponding status enum object.
+ *
+ * @param statusCode the numerical status code
+ * @return the matching status enum object or null if no matching enum is defined
+ */
+ public static HttpStatusCode fromStatusCode(final int statusCode) {
+ for (final HttpStatusCode s : HttpStatusCode.values()) {
+ if (s.code == statusCode) {
+ return s;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Get the associated status code.
+ *
+ * @return the status code.
+ */
+ public int getStatusCode() {
+ return code;
+ }
+
+ /**
+ * Get the status code info.
+ *
+ * @return the status code info
+ */
+ public String getInfo() {
+ return toString();
+ }
+
+ /**
+ * Get the status code info.
+ *
+ * @return the status code info
+ */
+ @Override
+ public String toString() {
+ return info;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/44e0c649/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultProcessor.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultProcessor.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultProcessor.java
index 1bf9dac..9a64b50 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultProcessor.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultProcessor.java
@@ -21,6 +21,9 @@ package org.apache.olingo.server.core;
import java.io.InputStream;
import org.apache.olingo.commons.api.edm.Edm;
+import org.apache.olingo.commons.api.http.HttpContentType;
+import org.apache.olingo.commons.api.http.HttpHeader;
+import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataRequest;
import org.apache.olingo.server.api.ODataResponse;
@@ -30,8 +33,8 @@ import org.apache.olingo.server.api.serializer.ODataFormat;
import org.apache.olingo.server.api.serializer.ODataSerializer;
import org.apache.olingo.server.api.uri.UriInfo;
-public class DefaultProcessor implements MetadataProcessor, ServiceDocumentProcessor {
-
+public class DefaultProcessor implements MetadataProcessor, ServiceDocumentProcessor, RedirectProcessor {
+
private OData odata;
private Edm edm;
@@ -50,7 +53,7 @@ public class DefaultProcessor implements MetadataProcessor, ServiceDocumentProce
responseEntity = serializer.serviceDocument(edm, request.getRawBaseUri());
response.setStatusCode(200);
- response.setHeader("Content-Type", "application/json");
+ response.setHeader(HttpHeader.CONTENT_TYPE, HttpContentType.APPLICATION_JSON);
response.setContent(responseEntity);
}
@@ -63,8 +66,16 @@ public class DefaultProcessor implements MetadataProcessor, ServiceDocumentProce
serializer = odata.createSerializer(ODataFormat.XML);
responseEntity = serializer.metadataDocument(edm);
response.setStatusCode(200);
- response.setHeader("Content-Type", "application/xml");
+ response.setHeader(HttpHeader.CONTENT_TYPE, HttpContentType.APPLICATION_XML);
response.setContent(responseEntity);
}
+ @Override
+ public void redirect(ODataRequest request, ODataResponse response) {
+ response.setStatusCode(HttpStatusCode.TEMPORARY_REDIRECT.getStatusCode());
+
+ String location = request.getRawRequestUri() + "/";
+ response.setHeader(HttpHeader.LOCATION, location);
+ }
+
}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/44e0c649/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
index d2d9b63..5d0f09f 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
@@ -23,6 +23,7 @@ import java.util.Map;
import org.apache.olingo.commons.api.ODataRuntimeException;
import org.apache.olingo.commons.api.edm.Edm;
+import org.apache.olingo.commons.api.http.HttpContentType;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataRequest;
import org.apache.olingo.server.api.ODataResponse;
@@ -33,8 +34,6 @@ import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.core.uri.parser.Parser;
import org.apache.olingo.server.core.uri.validator.UriValidator;
-import sun.reflect.generics.reflectiveObjects.NotImplementedException;
-
public class ODataHandler {
private final OData odata;
@@ -63,11 +62,16 @@ public class ODataHandler {
switch (uriInfo.getKind()) {
case metadata:
MetadataProcessor mp = selectProcessor(MetadataProcessor.class);
- mp.readMetadata(request, response, uriInfo, "application/xml");
+ mp.readMetadata(request, response, uriInfo, HttpContentType.APPLICATION_XML);
break;
case service:
- ServiceDocumentProcessor sdp = selectProcessor(ServiceDocumentProcessor.class);
- sdp.readServiceDocument(request, response, uriInfo, "application/json");
+ if ("".equals(request.getRawODataPath())) {
+ RedirectProcessor rdp = selectProcessor(RedirectProcessor.class);
+ rdp.redirect(request, response);
+ }else{
+ ServiceDocumentProcessor sdp = selectProcessor(ServiceDocumentProcessor.class);
+ sdp.readServiceDocument(request, response, uriInfo, HttpContentType.APPLICATION_JSON);
+ }
break;
default:
throw new ODataRuntimeException("not implemented");
@@ -85,7 +89,7 @@ public class ODataHandler {
T p = (T) processors.get(cls);
if (p == null) {
- throw new NotImplementedException();
+ throw new ODataRuntimeException("Not implemented");
}
return p;
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/44e0c649/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java
index a212ea8..3703e6b 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java
@@ -33,6 +33,7 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.olingo.commons.api.ODataRuntimeException;
import org.apache.olingo.commons.api.edm.Edm;
+import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.commons.api.http.HttpMethod;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataHttpHandler;
@@ -46,20 +47,16 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
private static final Logger LOG = LoggerFactory.getLogger(ODataHttpHandlerImpl.class);
-// private Edm edm;
-// private OData server;
private ODataHandler handler;
public ODataHttpHandlerImpl(final OData server, final Edm edm) {
-// this.edm = edm;
-// this.server = server;
handler = new ODataHandler(server, edm);
}
@Override
public void process(final HttpServletRequest request, final HttpServletResponse response) {
ODataRequest odRequest = createODataRequest(request, 0);
-
+
ODataResponse odResponse = handler.process(odRequest);
convertToHttp(response, odResponse);
@@ -73,23 +70,25 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
}
InputStream input = odResponse.getContent();
- OutputStream output;
- try {
- output = response.getOutputStream();
- byte[] buffer = new byte[1024];
- int n = 0;
- while (-1 != (n = input.read(buffer))) {
- output.write(buffer, 0, n);
- }
- } catch (IOException e) {
- LOG.error(e.getMessage(), e);
- throw new ODataRuntimeException(e);
- } finally {
- if (input != null) {
- try {
- input.close();
- } catch (IOException e) {
- throw new ODataRuntimeException(e);
+ if (input != null) {
+ OutputStream output;
+ try {
+ output = response.getOutputStream();
+ byte[] buffer = new byte[1024];
+ int n = 0;
+ while (-1 != (n = input.read(buffer))) {
+ output.write(buffer, 0, n);
+ }
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ throw new ODataRuntimeException(e);
+ } finally {
+ if (input != null) {
+ try {
+ input.close();
+ } catch (IOException e) {
+ throw new ODataRuntimeException(e);
+ }
}
}
}
@@ -116,8 +115,8 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
HttpMethod httpRequestMethod = HttpMethod.valueOf(httpRequest.getMethod());
if (httpRequestMethod == HttpMethod.POST) {
- String xHttpMethod = httpRequest.getHeader("X-HTTP-Method");
- String xHttpMethodOverride = httpRequest.getHeader("X-HTTP-Method-Override");
+ String xHttpMethod = httpRequest.getHeader(HttpHeader.X_HTTP_METHOD);
+ String xHttpMethodOverride = httpRequest.getHeader(HttpHeader.X_HTTP_METHOD_OVERRIDE);
if (xHttpMethod == null && xHttpMethodOverride == null) {
odRequest.setMethod(httpRequestMethod);
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/44e0c649/lib/server-core/src/main/java/org/apache/olingo/server/core/RedirectProcessor.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/RedirectProcessor.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/RedirectProcessor.java
new file mode 100644
index 0000000..c741cac
--- /dev/null
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/RedirectProcessor.java
@@ -0,0 +1,29 @@
+/*
+ * 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.olingo.server.core;
+
+import org.apache.olingo.server.api.ODataRequest;
+import org.apache.olingo.server.api.ODataResponse;
+import org.apache.olingo.server.api.processor.Processor;
+
+public interface RedirectProcessor extends Processor {
+
+ void redirect(ODataRequest request, ODataResponse response);
+
+}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/44e0c649/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java
----------------------------------------------------------------------
diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java
index f49cd99..1daf164 100644
--- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java
+++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java
@@ -48,10 +48,7 @@ public class TechnicalServlet extends HttpServlet {
ODataHttpHandler handler = odata.createHandler(edm);
handler.register(new TechnicalProcessor());
-
-// handler.registerServiceDocumentProcessor(new TechnicalProcessor());
-// handler.registerMetadataProcessor(new TechnicalProcessor());
-
+
handler.process(req, resp);
}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/44e0c649/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java
----------------------------------------------------------------------
diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java
index 3021f27..ade4245 100644
--- a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java
+++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java
@@ -25,12 +25,14 @@ import static org.mockito.Mockito.mock;
import org.apache.commons.io.IOUtils;
import org.apache.olingo.commons.api.edm.Edm;
+import org.apache.olingo.commons.api.http.HttpContentType;
+import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.commons.api.http.HttpMethod;
+import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataRequest;
import org.apache.olingo.server.api.ODataResponse;
import org.apache.olingo.server.api.processor.MetadataProcessor;
-import org.apache.olingo.server.api.processor.Processor;
import org.apache.olingo.server.api.processor.ServiceDocumentProcessor;
import org.apache.olingo.server.tecsvc.provider.EdmTechProvider;
import org.junit.Before;
@@ -53,34 +55,31 @@ public class ODataHandlerTest {
ODataRequest request = new ODataRequest();
request.setMethod(HttpMethod.GET);
- request.setRawBaseUri("http://localhost/odata/");
- request.setRawODataPath("");
+ request.setRawBaseUri("http://localhost/odata");
+ request.setRawODataPath("/");
ServiceDocumentProcessor processor = mock(ServiceDocumentProcessor.class);
handler.register(processor);
-
+
ODataResponse response = handler.process(request);
-
+
assertNotNull(response);
assertEquals(0, response.getStatusCode());
}
-
+
@Test
public void testServiceDocumentDefault() throws Exception {
ODataRequest request = new ODataRequest();
request.setMethod(HttpMethod.GET);
- request.setRawBaseUri("http://localhost/odata/");
- request.setRawODataPath("");
+ request.setRawBaseUri("http://localhost/odata");
+ request.setRawODataPath("/");
- Processor processor = mock(Processor.class);
- handler.register(processor);
-
ODataResponse response = handler.process(request);
-
+
assertNotNull(response);
assertEquals(200, response.getStatusCode());
- assertEquals("application/json", response.getHeaders().get("Content-Type"));
+ assertEquals(HttpContentType.APPLICATION_JSON, response.getHeaders().get(HttpHeader.CONTENT_TYPE));
assertNotNull(response.getContent());
String doc = IOUtils.toString(response.getContent());
@@ -90,6 +89,22 @@ public class ODataHandlerTest {
}
@Test
+ public void testServiceDocumentRedirect() throws Exception {
+ ODataRequest request = new ODataRequest();
+
+ request.setMethod(HttpMethod.GET);
+ request.setRawBaseUri("http://localhost/odata");
+ request.setRawRequestUri("http://localhost/odata");
+ request.setRawODataPath("");
+
+ ODataResponse response = handler.process(request);
+
+ assertNotNull(response);
+ assertEquals(HttpStatusCode.TEMPORARY_REDIRECT.getStatusCode(), response.getStatusCode());
+ assertEquals("http://localhost/odata/", response.getHeaders().get(HttpHeader.LOCATION));
+ }
+
+ @Test
public void testMetadataNonDefault() throws Exception {
ODataRequest request = new ODataRequest();
@@ -98,9 +113,9 @@ public class ODataHandlerTest {
MetadataProcessor processor = mock(MetadataProcessor.class);
handler.register(processor);
-
+
ODataResponse response = handler.process(request);
-
+
assertNotNull(response);
assertEquals(0, response.getStatusCode());
}
@@ -112,14 +127,11 @@ public class ODataHandlerTest {
request.setMethod(HttpMethod.GET);
request.setRawODataPath("$metadata");
- Processor processor = mock(Processor.class);
- handler.register(processor);
-
ODataResponse response = handler.process(request);
assertNotNull(response);
assertEquals(200, response.getStatusCode());
- assertEquals("application/xml", response.getHeaders().get("Content-Type"));
+ assertEquals(HttpContentType.APPLICATION_XML, response.getHeaders().get(HttpHeader.CONTENT_TYPE));
assertNotNull(response.getContent());
String doc = IOUtils.toString(response.getContent());