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 2013/10/29 13:39:11 UTC

[5/6] [OLINGO-37] - Archetype support - refactoring

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ed52dd3a/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/java/CarODataSingleProcessor.java
----------------------------------------------------------------------
diff --git a/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/java/CarODataSingleProcessor.java b/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/java/CarODataSingleProcessor.java
new file mode 100644
index 0000000..b18355d
--- /dev/null
+++ b/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/java/CarODataSingleProcessor.java
@@ -0,0 +1,154 @@
+#set( $symbol_pound = '#' )
+#set( $symbol_dollar = '$' )
+#set( $symbol_escape = '\' )
+/*******************************************************************************
+ * 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 ${package};
+
+import static ${package}.CarEdmProvider.ENTITY_SET_NAME_CARS;
+import static ${package}.CarEdmProvider.ENTITY_SET_NAME_MANUFACTURERS;
+
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.olingo.odata2.api.edm.EdmEntitySet;
+import org.apache.olingo.odata2.api.edm.EdmLiteralKind;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.EdmSimpleType;
+import org.apache.olingo.odata2.api.ep.EntityProvider;
+import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties;
+import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties.ODataEntityProviderPropertiesBuilder;
+import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.api.exception.ODataNotFoundException;
+import org.apache.olingo.odata2.api.exception.ODataNotImplementedException;
+import org.apache.olingo.odata2.api.processor.ODataResponse;
+import org.apache.olingo.odata2.api.processor.ODataSingleProcessor;
+import org.apache.olingo.odata2.api.uri.KeyPredicate;
+import org.apache.olingo.odata2.api.uri.info.GetEntitySetUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntityUriInfo;
+
+public class CarODataSingleProcessor extends ODataSingleProcessor {
+
+  private final CarDataStore dataStore;
+
+  public CarODataSingleProcessor() {
+    dataStore = new CarDataStore();
+  }
+
+  @Override
+  public ODataResponse readEntitySet(final GetEntitySetUriInfo uriInfo, final String contentType) 
+      throws ODataException {
+
+    EdmEntitySet entitySet;
+
+    if (uriInfo.getNavigationSegments().size() == 0) {
+      entitySet = uriInfo.getStartEntitySet();
+
+      if (ENTITY_SET_NAME_CARS.equals(entitySet.getName())) {
+        return EntityProvider.writeFeed(contentType, entitySet, dataStore.getCars(),
+            EntityProviderWriteProperties.serviceRoot(getContext().getPathInfo().getServiceRoot()).build());
+      } else if (ENTITY_SET_NAME_MANUFACTURERS.equals(entitySet.getName())) {
+        return EntityProvider.writeFeed(contentType, entitySet, dataStore.getManufacturers(),
+            EntityProviderWriteProperties.serviceRoot(getContext().getPathInfo().getServiceRoot()).build());
+      }
+
+      throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
+
+    } else if (uriInfo.getNavigationSegments().size() == 1) {
+      // navigation first level, simplified example for illustration purposes only
+      entitySet = uriInfo.getTargetEntitySet();
+
+      if (ENTITY_SET_NAME_CARS.equals(entitySet.getName())) {
+        int manufacturerKey = getKeyValue(uriInfo.getKeyPredicates().get(0));
+
+        List<Map<String, Object>> cars = new ArrayList<Map<String, Object>>();
+        cars.addAll(dataStore.getCarsFor(manufacturerKey));
+
+        return EntityProvider.writeFeed(contentType, entitySet, cars, EntityProviderWriteProperties.serviceRoot(
+            getContext().getPathInfo().getServiceRoot()).build());
+      }
+
+      throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
+    }
+
+    throw new ODataNotImplementedException();
+  }
+
+  @Override
+  public ODataResponse readEntity(final GetEntityUriInfo uriInfo, final String contentType) throws ODataException {
+
+    if (uriInfo.getNavigationSegments().size() == 0) {
+      EdmEntitySet entitySet = uriInfo.getStartEntitySet();
+
+      if (ENTITY_SET_NAME_CARS.equals(entitySet.getName())) {
+        int id = getKeyValue(uriInfo.getKeyPredicates().get(0));
+        Map<String, Object> data = dataStore.getCar(id);
+
+        if (data != null) {
+          URI serviceRoot = getContext().getPathInfo().getServiceRoot();
+          ODataEntityProviderPropertiesBuilder propertiesBuilder =
+              EntityProviderWriteProperties.serviceRoot(serviceRoot);
+
+          return EntityProvider.writeEntry(contentType, entitySet, data, propertiesBuilder.build());
+        }
+      } else if (ENTITY_SET_NAME_MANUFACTURERS.equals(entitySet.getName())) {
+        int id = getKeyValue(uriInfo.getKeyPredicates().get(0));
+        Map<String, Object> data = dataStore.getManufacturer(id);
+
+        if (data != null) {
+          URI serviceRoot = getContext().getPathInfo().getServiceRoot();
+          ODataEntityProviderPropertiesBuilder propertiesBuilder =
+              EntityProviderWriteProperties.serviceRoot(serviceRoot);
+
+          return EntityProvider.writeEntry(contentType, entitySet, data, propertiesBuilder.build());
+        }
+      }
+
+      throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
+
+    } else if (uriInfo.getNavigationSegments().size() == 1) {
+      // navigation first level, simplified example for illustration purposes only
+      EdmEntitySet entitySet = uriInfo.getTargetEntitySet();
+
+      Map<String, Object> data = null;
+
+      if (ENTITY_SET_NAME_MANUFACTURERS.equals(entitySet.getName())) {
+        int carKey = getKeyValue(uriInfo.getKeyPredicates().get(0));
+        data = dataStore.getManufacturerFor(carKey);
+      }
+
+      if (data != null) {
+        return EntityProvider.writeEntry(contentType, uriInfo.getTargetEntitySet(),
+            data, EntityProviderWriteProperties.serviceRoot(getContext().getPathInfo().getServiceRoot()).build());
+      }
+
+      throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
+    }
+
+    throw new ODataNotImplementedException();
+  }
+
+  private int getKeyValue(final KeyPredicate key) throws ODataException {
+    EdmProperty property = key.getProperty();
+    EdmSimpleType type = (EdmSimpleType) property.getType();
+    return type.valueOfString(key.getLiteral(), EdmLiteralKind.DEFAULT, property.getFacets(), Integer.class);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ed52dd3a/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/java/CarServiceFactory.java
----------------------------------------------------------------------
diff --git a/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/java/CarServiceFactory.java b/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/java/CarServiceFactory.java
new file mode 100644
index 0000000..6806631
--- /dev/null
+++ b/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/java/CarServiceFactory.java
@@ -0,0 +1,42 @@
+#set( $symbol_pound = '#' )
+#set( $symbol_dollar = '$' )
+#set( $symbol_escape = '\' )
+/*******************************************************************************
+ * 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 ${package};
+
+import org.apache.olingo.odata2.api.ODataService;
+import org.apache.olingo.odata2.api.ODataServiceFactory;
+import org.apache.olingo.odata2.api.edm.provider.EdmProvider;
+import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.api.processor.ODataContext;
+import org.apache.olingo.odata2.api.processor.ODataSingleProcessor;
+
+public class CarServiceFactory extends ODataServiceFactory {
+
+  @Override
+  public ODataService createService(final ODataContext ctx) throws ODataException {
+
+    EdmProvider edmProvider = new CarEdmProvider();
+
+    ODataSingleProcessor singleProcessor = new CarODataSingleProcessor();
+
+    return createODataSingleProcessorService(edmProvider, singleProcessor);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ed52dd3a/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/resources/log4j.xml
----------------------------------------------------------------------
diff --git a/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/resources/log4j.xml b/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/resources/log4j.xml
new file mode 100644
index 0000000..ed91239
--- /dev/null
+++ b/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/resources/log4j.xml
@@ -0,0 +1,36 @@
+#set( $symbol_pound = '#' )
+#set( $symbol_dollar = '$' )
+#set( $symbol_escape = '\' )
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+  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.
+-->
+<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
+    <appender name="console" class="org.apache.log4j.ConsoleAppender">
+        <param name="Target" value="System.out" />
+        <layout class="org.apache.log4j.PatternLayout">
+            <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
+        </layout>
+    </appender>
+    
+    <root>
+        <priority value="error" />
+        <appender-ref ref="console" />
+    </root>
+</log4j:configuration>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ed52dd3a/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml b/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..b5b07da
--- /dev/null
+++ b/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,49 @@
+#set( $symbol_pound = '#' )
+#set( $symbol_dollar = '$' )
+#set( $symbol_escape = '\' )
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+  id="WebApp_ID" version="2.5">
+  <display-name>org.apache.olingo.odata2.sample</display-name>
+  <welcome-file-list>
+    <welcome-file>index.html</welcome-file>
+  </welcome-file-list>
+
+  <servlet>
+    <servlet-name>CarServiceServlet</servlet-name>
+    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
+    <init-param>
+      <param-name>javax.ws.rs.Application</param-name>
+      <param-value>org.apache.olingo.odata2.core.rest.app.ODataApplication</param-value>
+    </init-param>
+    <init-param>
+      <param-name>org.apache.olingo.odata2.service.factory</param-name>
+      <param-value>${package}.CarServiceFactory</param-value>
+    </init-param>
+    <load-on-startup>1</load-on-startup>
+  </servlet>
+  <servlet-mapping>
+    <servlet-name>CarServiceServlet</servlet-name>
+    <url-pattern>/CarService.svc/*</url-pattern>
+  </servlet-mapping>
+</web-app>

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ed52dd3a/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/webapp/index.html
----------------------------------------------------------------------
diff --git a/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/webapp/index.html b/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/webapp/index.html
new file mode 100644
index 0000000..810c748
--- /dev/null
+++ b/odata2-sample/cars-service-archetype/src/main/resources/archetype-resources/src/main/webapp/index.html
@@ -0,0 +1,55 @@
+#set( $symbol_pound = '#' )
+#set( $symbol_dollar = '$' )
+#set( $symbol_escape = '\' )
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<!--
+  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.
+-->
+<html>
+  <head>
+    <title></title>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+  </head>
+  <body>
+    <h1>Apache Olingo - OData Car Sample Project</h1>
+    <div>
+      <ul>
+        <li><a href="CarService.svc">Link to Car Sample Service Root - /CarService.svc</a></li>
+        <li><a href="CarService.svc/${symbol_dollar}metadata">Link to Car Sample Service Metadata - /CarService.svc/${symbol_dollar}metadata</a></li>
+      </ul>
+    </div>
+    <div>
+      <ul>
+        <li>Entity Sets
+          <ul>
+            <li><a href="CarService.svc/Cars">/Cars</a></li>        
+            <li><a href="CarService.svc/Manufacturers">/Manufacturers</a></li>        
+          </ul>
+        </li>
+        <li>Sample Entities
+          <ul>
+            <li><a href="CarService.svc/Cars(1)">/Cars(1)</a></li>        
+            <li><a href="CarService.svc/Cars(3)">/Cars(3)</a></li>        
+            <li><a href="CarService.svc/Cars(3)/Manufacturer">/Cars(3)/Manufacturer</a></li>        
+            <li><a href="CarService.svc/Manufacturers(1)">/Manufacturers(1)</a></li>        
+          </ul>
+        </li>
+      </ul>
+    </div>
+  </body>
+</html>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ed52dd3a/odata2-sample/cars-service-archetype/src/test/resources/projects/basic/archetype.properties
----------------------------------------------------------------------
diff --git a/odata2-sample/cars-service-archetype/src/test/resources/projects/basic/archetype.properties b/odata2-sample/cars-service-archetype/src/test/resources/projects/basic/archetype.properties
new file mode 100644
index 0000000..ac7b438
--- /dev/null
+++ b/odata2-sample/cars-service-archetype/src/test/resources/projects/basic/archetype.properties
@@ -0,0 +1,5 @@
+#Tue Oct 29 12:59:58 CET 2013
+package=it.pkg
+version=0.1-SNAPSHOT
+groupId=archetype.it
+artifactId=basic

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ed52dd3a/odata2-sample/cars-service-archetype/src/test/resources/projects/basic/goal.txt
----------------------------------------------------------------------
diff --git a/odata2-sample/cars-service-archetype/src/test/resources/projects/basic/goal.txt b/odata2-sample/cars-service-archetype/src/test/resources/projects/basic/goal.txt
new file mode 100644
index 0000000..e69de29

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ed52dd3a/odata2-sample/pom.xml
----------------------------------------------------------------------
diff --git a/odata2-sample/pom.xml b/odata2-sample/pom.xml
index d80b703..576d4f0 100644
--- a/odata2-sample/pom.xml
+++ b/odata2-sample/pom.xml
@@ -22,6 +22,6 @@
     </parent>
 
     <modules>
-        <module>cars-archetype</module>
+        <module>cars-service-archetype</module>
     </modules>
 </project>