diff --git a/samples/pom.xml b/samples/pom.xml index 765bd953e..95e3ddbed 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -45,5 +45,11 @@ client + + tutorials + + tutorials + + diff --git a/samples/tutorials/p1_read/pom.xml b/samples/tutorials/p1_read/pom.xml new file mode 100755 index 000000000..c815f8d88 --- /dev/null +++ b/samples/tutorials/p1_read/pom.xml @@ -0,0 +1,75 @@ + + + + 4.0.0 + myservice.mynamespace + DemoService + war + 0.0.1 + + DemoService Maven Webapp + + + 2.5 + 4.0.0-beta-03 + 1.7.7 + + + + + javax.servlet + servlet-api + ${javax.version} + provided + + + + org.apache.olingo + odata-server-api + ${odata.version} + + + org.apache.olingo + odata-server-core + ${odata.version} + + + + org.apache.olingo + odata-commons-api + ${odata.version} + + + org.apache.olingo + odata-commons-core + ${odata.version} + + + + org.slf4j + slf4j-simple + ${slf4j.version} + runtime + + + diff --git a/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java b/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java new file mode 100755 index 000000000..dd46e864e --- /dev/null +++ b/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/service/DemoEdmProvider.java @@ -0,0 +1,153 @@ +/* + * 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 myservice.mynamespace.service; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.apache.olingo.commons.api.ODataException; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.CsdlAbstractEdmProvider; +import org.apache.olingo.commons.api.edm.provider.CsdlEntityContainer; +import org.apache.olingo.commons.api.edm.provider.CsdlEntityContainerInfo; +import org.apache.olingo.commons.api.edm.provider.CsdlEntitySet; +import org.apache.olingo.commons.api.edm.provider.CsdlEntityType; +import org.apache.olingo.commons.api.edm.provider.CsdlProperty; +import org.apache.olingo.commons.api.edm.provider.CsdlPropertyRef; +import org.apache.olingo.commons.api.edm.provider.CsdlSchema; + +/** + * this class is supposed to declare the metadata of the OData service + * it is invoked by the Olingo framework e.g. when the metadata document of the service is invoked + * e.g. http://localhost:8080/ExampleService1/ExampleService1.svc/$metadata + */ +public class DemoEdmProvider extends CsdlAbstractEdmProvider { + + // Service Namespace + public static final String NAMESPACE = "OData.Demo"; + + // EDM Container + public static final String CONTAINER_NAME = "Container"; + public static final FullQualifiedName CONTAINER = new FullQualifiedName(NAMESPACE, CONTAINER_NAME); + + // Entity Types Names + public static final String ET_PRODUCT_NAME = "Product"; + public static final FullQualifiedName ET_PRODUCT_FQN = new FullQualifiedName(NAMESPACE, ET_PRODUCT_NAME); + + // Entity Set Names + public static final String ES_PRODUCTS_NAME = "Products"; + + + @Override + public List getSchemas() throws ODataException { + + // create Schema + CsdlSchema schema = new CsdlSchema(); + schema.setNamespace(NAMESPACE); + + // add EntityTypes + List entityTypes = new ArrayList(); + entityTypes.add(getEntityType(ET_PRODUCT_FQN)); + schema.setEntityTypes(entityTypes); + + // add EntityContainer + schema.setEntityContainer(getEntityContainer()); + + // finally + List schemas = new ArrayList(); + schemas.add(schema); + + return schemas; + } + + + @Override + public CsdlEntityType getEntityType(FullQualifiedName entityTypeName) throws ODataException { + + // this method is called for one of the EntityTypes that are configured in the Schema + if(entityTypeName.equals(ET_PRODUCT_FQN)){ + + //create EntityType properties + CsdlProperty id = new CsdlProperty().setName("ID").setType(EdmPrimitiveTypeKind.Int32.getFullQualifiedName()); + CsdlProperty name = new CsdlProperty().setName("Name").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName()); + CsdlProperty description = new CsdlProperty().setName("Description").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName()); + + // create CsdlPropertyRef for Key element + CsdlPropertyRef propertyRef = new CsdlPropertyRef(); + propertyRef.setName("ID"); + + // configure EntityType + CsdlEntityType entityType = new CsdlEntityType(); + entityType.setName(ET_PRODUCT_NAME); + entityType.setProperties(Arrays.asList(id, name , description)); + entityType.setKey(Collections.singletonList(propertyRef)); + + return entityType; + } + + return null; + } + + @Override + public CsdlEntitySet getEntitySet(FullQualifiedName entityContainer, String entitySetName) throws ODataException { + + if(entityContainer.equals(CONTAINER)){ + if(entitySetName.equals(ES_PRODUCTS_NAME)){ + CsdlEntitySet entitySet = new CsdlEntitySet(); + entitySet.setName(ES_PRODUCTS_NAME); + entitySet.setType(ET_PRODUCT_FQN); + + return entitySet; + } + } + + return null; + } + + @Override + public CsdlEntityContainer getEntityContainer() throws ODataException { + + // create EntitySets + List entitySets = new ArrayList(); + entitySets.add(getEntitySet(CONTAINER, ES_PRODUCTS_NAME)); + + // create EntityContainer + CsdlEntityContainer entityContainer = new CsdlEntityContainer(); + entityContainer.setName(CONTAINER_NAME); + entityContainer.setEntitySets(entitySets); + + return entityContainer; + } + + @Override + public CsdlEntityContainerInfo getEntityContainerInfo(FullQualifiedName entityContainerName) throws ODataException { + + // This method is invoked when displaying the service document at e.g. http://localhost:8080/DemoService/DemoService.svc + if(entityContainerName == null || entityContainerName.equals(CONTAINER)){ + CsdlEntityContainerInfo entityContainerInfo = new CsdlEntityContainerInfo(); + entityContainerInfo.setContainerName(CONTAINER); + return entityContainerInfo; + } + + return null; + } +} diff --git a/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java b/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java new file mode 100755 index 000000000..e82a3caa6 --- /dev/null +++ b/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/service/DemoEntityCollectionProcessor.java @@ -0,0 +1,129 @@ +/* + * 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 myservice.mynamespace.service; + +import java.util.List; + +import org.apache.olingo.commons.api.data.ContextURL; +import org.apache.olingo.commons.api.data.Entity; +import org.apache.olingo.commons.api.data.EntityCollection; +import org.apache.olingo.commons.api.data.Property; +import org.apache.olingo.commons.api.data.ValueType; +import org.apache.olingo.commons.api.edm.EdmEntitySet; +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.format.ContentType; +import org.apache.olingo.commons.api.format.ODataFormat; +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.ODataApplicationException; +import org.apache.olingo.server.api.ODataRequest; +import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.ServiceMetadata; +import org.apache.olingo.server.api.processor.EntityCollectionProcessor; +import org.apache.olingo.server.api.serializer.EntityCollectionSerializerOptions; +import org.apache.olingo.server.api.serializer.ODataSerializer; +import org.apache.olingo.server.api.serializer.SerializerException; +import org.apache.olingo.server.api.serializer.SerializerResult; +import org.apache.olingo.server.api.uri.UriInfo; +import org.apache.olingo.server.api.uri.UriResource; +import org.apache.olingo.server.api.uri.UriResourceEntitySet; + +/** + * This class is invoked by the Olingo framework when the the OData service is invoked order to display a list/collection of data (entities). + * This is the case if an EntitySet is requested by the user. + * Such an example URL would be: + * http://localhost:8080/ExampleService1/ExampleService1.svc/Products + */ +public class DemoEntityCollectionProcessor implements EntityCollectionProcessor { + + private OData odata; + private ServiceMetadata serviceMetadata; + + // our processor is initialized with the OData context object + public void init(OData odata, ServiceMetadata serviceMetadata) { + this.odata = odata; + this.serviceMetadata = serviceMetadata; + } + + + // the only method that is declared in the EntityCollectionProcessor interface + // this method is called, when the user fires a request to an EntitySet + // in our example, the URL would be: + // http://localhost:8080/ExampleService1/ExampleServlet1.svc/Products + public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat) throws ODataApplicationException, SerializerException { + + // 1st we have retrieve the requested EntitySet from the uriInfo object (representation of the parsed service URI) + List resourcePaths = uriInfo.getUriResourceParts(); + UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); // in our example, the first segment is the EntitySet + EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet(); + + // 2nd: fetch the data from backend for this requested EntitySetName // it has to be delivered as EntitySet object + EntityCollection entitySet = getData(edmEntitySet); + + // 3rd: create a serializer based on the requested format (json) + ODataFormat format = ODataFormat.fromContentType(responseFormat); + ODataSerializer serializer = odata.createSerializer(format); + + // 4th: Now serialize the content: transform from the EntitySet object to InputStream + EdmEntityType edmEntityType = edmEntitySet.getEntityType(); + ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build(); + + EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with().contextURL(contextUrl).build(); + SerializerResult serializedContent = serializer.entityCollection(serviceMetadata, edmEntityType, entitySet, opts); + + // Finally: configure the response object: set the body, headers and status code + response.setContent(serializedContent.getContent()); + response.setStatusCode(HttpStatusCode.OK.getStatusCode()); + response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString()); + } + + + /** + * Helper method for providing some sample data + * @param edmEntitySet for which the data is requested + * @return data of requested entity set + */ + private EntityCollection getData(EdmEntitySet edmEntitySet){ + + EntityCollection entityCollection = new EntityCollection(); + // check for which EdmEntitySet the data is requested + if(DemoEdmProvider.ES_PRODUCTS_NAME.equals(edmEntitySet.getName())) { + List entityList = entityCollection.getEntities(); + + // add some sample product entities + entityList.add(new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB"))); + + entityList.add(new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network"))); + + entityList.add(new Entity() + .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)) + .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen")) + .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "17 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960"))); + } + + return entityCollection; + } +} diff --git a/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/web/DemoServlet.java b/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/web/DemoServlet.java new file mode 100755 index 000000000..486770c45 --- /dev/null +++ b/samples/tutorials/p1_read/src/main/java/myservice/mynamespace/web/DemoServlet.java @@ -0,0 +1,67 @@ +/* + * 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 myservice.mynamespace.web; + +import java.io.IOException; +import java.util.ArrayList; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import myservice.mynamespace.service.DemoEdmProvider; +import myservice.mynamespace.service.DemoEntityCollectionProcessor; + +import org.apache.olingo.server.api.OData; +import org.apache.olingo.server.api.ODataHttpHandler; +import org.apache.olingo.server.api.ServiceMetadata; +import org.apache.olingo.server.api.edmx.EdmxReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class represents a standard HttpServlet implementation. + * It is used as main entry point for the web application that carries the OData service. + * The implementation of this HttpServlet simply delegates the user requests to the ODataHttpHandler + */ +public class DemoServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + private static final Logger LOG = LoggerFactory.getLogger(DemoServlet.class); + + @Override + protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { + + try { + // create odata handler and configure it with EdmProvider and Processor + OData odata = OData.newInstance(); + ServiceMetadata edm = odata.createServiceMetadata(new DemoEdmProvider(), new ArrayList()); + ODataHttpHandler handler = odata.createHandler(edm); + handler.register(new DemoEntityCollectionProcessor()); + + // let the handler do the work + handler.process(req, resp); + + } catch (RuntimeException e) { + LOG.error("Server Error occurred in ExampleServlet", e); + throw new ServletException(e); + } + } +} diff --git a/samples/tutorials/p1_read/src/main/webapp/WEB-INF/web.xml b/samples/tutorials/p1_read/src/main/webapp/WEB-INF/web.xml new file mode 100755 index 000000000..21de52a8f --- /dev/null +++ b/samples/tutorials/p1_read/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,40 @@ + + + + + + + DemoServlet + myservice.mynamespace.web.DemoServlet + 1 + + + + + DemoServlet + /DemoService.svc/* + + \ No newline at end of file diff --git a/samples/tutorials/p1_read/src/main/webapp/index.jsp b/samples/tutorials/p1_read/src/main/webapp/index.jsp new file mode 100755 index 000000000..2baf543ea --- /dev/null +++ b/samples/tutorials/p1_read/src/main/webapp/index.jsp @@ -0,0 +1,6 @@ + + +

Hello World!

+OData Olingo V4 Demo Service + + diff --git a/samples/tutorials/pom.xml b/samples/tutorials/pom.xml new file mode 100644 index 000000000..ba862332c --- /dev/null +++ b/samples/tutorials/pom.xml @@ -0,0 +1,52 @@ + + + + 4.0.0 + + odata-tutorials + pom + ${project.artifactId} + + + org.apache.olingo + odata-samples + 4.0.0-SNAPSHOT + .. + + + + p1_read + + + + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + + + +