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/03/18 17:00:06 UTC
git commit: [OLINGO-211] First prototype
Repository: incubator-olingo-odata4
Updated Branches:
refs/heads/olingo211 [created] 2884d02ba
[OLINGO-211] First prototype
Project: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/commit/2884d02b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/tree/2884d02b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/diff/2884d02b
Branch: refs/heads/olingo211
Commit: 2884d02ba715f78dbc7c548527d11b800b4b1337
Parents: ec39fd6
Author: Christian Amend <ch...@apache.org>
Authored: Tue Mar 18 16:58:53 2014 +0100
Committer: Christian Amend <ch...@apache.org>
Committed: Tue Mar 18 16:58:53 2014 +0100
----------------------------------------------------------------------
lib/server-core/pom.xml | 22 ++++++
.../server/core/servlet/ODataServlet.java | 80 ++++++++++++++++++++
.../server/core/testutil/ODataTestServlet.java | 32 ++++++++
.../core/testutil/ServiceDocumentFitTest.java | 48 ++++++++++++
.../olingo/server/core/testutil/TestServer.java | 78 +++++++++++++++++++
5 files changed, 260 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/blob/2884d02b/lib/server-core/pom.xml
----------------------------------------------------------------------
diff --git a/lib/server-core/pom.xml b/lib/server-core/pom.xml
index bffe70b..2cc2e84 100644
--- a/lib/server-core/pom.xml
+++ b/lib/server-core/pom.xml
@@ -36,6 +36,28 @@
<version>${project.version}</version>
</dependency>
<dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-servlet</artifactId>
+ <version>7.5.4.v20111024</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-server</artifactId>
+ <version>7.5.4.v20111024</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.2.3</version>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/blob/2884d02b/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..f273663
--- /dev/null
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/servlet/ODataServlet.java
@@ -0,0 +1,80 @@
+/*
+ * 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.ODataFormat;
+import org.apache.olingo.server.api.ODataSerializer;
+import org.apache.olingo.server.api.edm.provider.EdmProvider;
+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();
+ ODataSerializer serializer = impl.getSerializer(ODataFormat.JSON);
+ InputStream serviceDocument = serializer.serviceDocument(edm, "http//:root");
+ sendResponse(serviceDocument);
+ }
+
+ 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();
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/blob/2884d02b/lib/server-core/src/test/java/org/apache/olingo/server/core/testutil/ODataTestServlet.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/testutil/ODataTestServlet.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/testutil/ODataTestServlet.java
new file mode 100644
index 0000000..b7ffbcb
--- /dev/null
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/testutil/ODataTestServlet.java
@@ -0,0 +1,32 @@
+/*
+ * 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.testutil;
+
+import org.apache.olingo.server.api.edm.provider.EdmProvider;
+import org.apache.olingo.server.core.servlet.ODataServlet;
+
+public class ODataTestServlet extends ODataServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ protected EdmProvider createEdmProvider() {
+ return new EdmTechProvider();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/blob/2884d02b/lib/server-core/src/test/java/org/apache/olingo/server/core/testutil/ServiceDocumentFitTest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/testutil/ServiceDocumentFitTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/testutil/ServiceDocumentFitTest.java
new file mode 100644
index 0000000..51c0f38
--- /dev/null
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/testutil/ServiceDocumentFitTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.testutil;
+
+import java.io.InputStream;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.junit.Test;
+
+public class ServiceDocumentFitTest {
+
+ @Test
+ public void test() throws Exception{
+ TestServer server = new TestServer();
+ server.startServer();
+
+ HttpGet get = new HttpGet();
+ get.setURI(server.getEndpoint());
+ HttpClient httpClient = new DefaultHttpClient();
+ final HttpResponse response = httpClient.execute(get);
+ HttpEntity entity = response.getEntity();
+ InputStream content = entity.getContent();
+ String string = StringUtils.inputStreamToString(content, false);
+ System.out.println(string);
+ server.stopServer();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata4/blob/2884d02b/lib/server-core/src/test/java/org/apache/olingo/server/core/testutil/TestServer.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/testutil/TestServer.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/testutil/TestServer.java
new file mode 100644
index 0000000..f66da83
--- /dev/null
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/testutil/TestServer.java
@@ -0,0 +1,78 @@
+/*
+ * 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.testutil;
+
+import java.net.InetSocketAddress;
+import java.net.URI;
+
+import javax.servlet.http.HttpServlet;
+
+import org.apache.olingo.commons.api.ODataRuntimeException;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
+
+public class TestServer {
+
+ private static final String DEFAULT_SCHEME = "http";
+ private static final String DEFAULT_HOST = "localhost";
+ private static final String DEFAULT_PATH = "/test";
+
+ private Server server;
+ private URI endpoint; // = URI.create("http://localhost:19080/test"); // no slash at the end !!!
+
+ public URI getEndpoint() {
+ return endpoint;
+ }
+
+ public void startServer() {
+ try {
+ final ServletContextHandler contextHandler = createContextHandler();
+ final InetSocketAddress isa = new InetSocketAddress(DEFAULT_HOST, 512);
+ server = new Server(isa);
+
+ server.setHandler(contextHandler);
+ server.start();
+ endpoint = new URI(DEFAULT_SCHEME, null, DEFAULT_HOST, isa.getPort(), "/abc" + DEFAULT_PATH, null, null);
+ } catch (final Exception e) {
+ throw new ODataRuntimeException(e);
+ }
+ }
+
+ private ServletContextHandler createContextHandler() throws Exception {
+ final HttpServlet httpServlet = new ODataTestServlet();
+ ServletHolder odataServletHolder = new ServletHolder(httpServlet);
+
+ final ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ contextHandler.setContextPath("/abc");
+ contextHandler.addServlet(odataServletHolder, DEFAULT_PATH + "/*");
+ return contextHandler;
+ }
+
+ public void stopServer() {
+ try {
+ if (server != null) {
+ server.stop();
+ }
+ } catch (final Exception e) {
+ throw new ODataRuntimeException(e);
+ }
+ }
+
+}