You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2014/04/04 17:22:19 UTC
git commit: [OLINGO-211] Preliminary Servlet
Repository: olingo-odata4
Updated Branches:
refs/heads/master 7dae5ef33 -> 86ab7ef0f
[OLINGO-211] Preliminary Servlet
Only a prototype to give back metadata and service document
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/86ab7ef0
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/86ab7ef0
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/86ab7ef0
Branch: refs/heads/master
Commit: 86ab7ef0f701e4a3c4506d6a6dec518cc2cb286a
Parents: 7dae5ef
Author: Christian Amend <ch...@apache.org>
Authored: Fri Apr 4 17:21:32 2014 +0200
Committer: Christian Amend <ch...@apache.org>
Committed: Fri Apr 4 17:21:32 2014 +0200
----------------------------------------------------------------------
lib/server-core/pom.xml | 5 ++
.../server/core/servlet/ODataServlet.java | 86 ++++++++++++++++++++
2 files changed, 91 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/86ab7ef0/lib/server-core/pom.xml
----------------------------------------------------------------------
diff --git a/lib/server-core/pom.xml b/lib/server-core/pom.xml
index 5d90ae8..28693fc 100644
--- a/lib/server-core/pom.xml
+++ b/lib/server-core/pom.xml
@@ -45,6 +45,11 @@
<artifactId>olingo-commons-core</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>javax.servlet-api</artifactId>
+ <version>${servlet.version}</version>
+ </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/86ab7ef0/lib/server-core/src/main/java/org/apache/olingo/server/core/servlet/ODataServlet.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/servlet/ODataServlet.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/servlet/ODataServlet.java
new file mode 100644
index 0000000..aeb9880
--- /dev/null
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/servlet/ODataServlet.java
@@ -0,0 +1,86 @@
+/*
+ * 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.servlet;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.olingo.server.api.edm.provider.EdmProvider;
+import org.apache.olingo.server.api.serializer.ODataFormat;
+import org.apache.olingo.server.api.serializer.ODataSerializer;
+import org.apache.olingo.server.core.ODataServerImpl;
+import org.apache.olingo.server.core.edm.provider.EdmProviderImpl;
+
+public class ODataServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ private HttpServletResponse resp;
+
+ @Override
+ protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
+ this.resp = resp;
+
+ EdmProvider provider = createEdmProvider();
+ EdmProviderImpl edm = new EdmProviderImpl(provider);
+
+ ODataServerImpl impl = new ODataServerImpl();
+ InputStream responseEntity = null;
+ if (req.getPathInfo().contains("$metadata")) {
+ ODataSerializer serializer = impl.getSerializer(ODataFormat.XML);
+ responseEntity = serializer.metadataDocument(edm);
+ } else {
+ ODataSerializer serializer = impl.getSerializer(ODataFormat.JSON);
+ responseEntity = serializer.serviceDocument(edm, "http//:root");
+ }
+ sendResponse(responseEntity);
+ }
+
+ protected EdmProvider createEdmProvider() {
+ return null;
+ }
+
+ // TODO: check throws
+ private void sendResponse(Object entity) throws IOException {
+ resp.setStatus(200);
+ resp.setContentType("application/json");
+ if (entity != null) {
+ ServletOutputStream out = resp.getOutputStream();
+ int curByte = -1;
+ if (entity instanceof InputStream) {
+ while ((curByte = ((InputStream) entity).read()) != -1) {
+ out.write((char) curByte);
+ }
+ ((InputStream) entity).close();
+ } else if (entity instanceof String) {
+ String body = (String) entity;
+ out.write(body.getBytes("utf-8"));
+ }
+
+ out.flush();
+ out.close();
+ }
+ }
+
+}