minor improvements

Change-Id: Ia9872a613f3221e2de885f9d325a14f3c50b4beb

Signed-off-by: Christian Amend <chrisam@apache.org>
This commit is contained in:
Klaus Straubinger 2014-09-25 13:50:32 +02:00 committed by Christian Amend
parent 68f51d84d0
commit f947afc108
6 changed files with 59 additions and 58 deletions

View File

@ -85,16 +85,16 @@ public class TomcatTestServer {
boolean keepRunning = false; boolean keepRunning = false;
for (String param : params) { for (String param : params) {
if(param.equalsIgnoreCase("keeprunning")) { if (param.equalsIgnoreCase("keeprunning")) {
keepRunning = true; keepRunning = true;
} else if(param.equalsIgnoreCase("addwebapp")) { } else if (param.equalsIgnoreCase("addwebapp")) {
server.addWebApp(); server.addWebApp();
} else if(param.startsWith("port")) { } else if (param.startsWith("port")) {
server.atPort(extractPortParam(param)); server.atPort(extractPortParam(param));
} }
} }
if(keepRunning) { if (keepRunning) {
LOG.info("...and keep server running."); LOG.info("...and keep server running.");
server.startAndWait(); server.startAndWait();
} else { } else {
@ -110,16 +110,14 @@ public class TomcatTestServer {
public static int extractPortParam(String portParameter) { public static int extractPortParam(String portParameter) {
String[] portParam = portParameter.split("="); String[] portParam = portParameter.split("=");
if(portParam.length == 2) { if (portParam.length == 2) {
try { try {
return Integer.parseInt(portParam[1]); return Integer.parseInt(portParam[1]);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new IllegalArgumentException("Port parameter (" + portParameter + throw new IllegalArgumentException("Port parameter (" + portParameter + ") could not be parsed.");
") could not be parsed.");
} }
} }
throw new IllegalArgumentException("Port parameter (" + portParameter + throw new IllegalArgumentException("Port parameter (" + portParameter + ") could not be parsed.");
") could not be parsed.");
} }
public static class StaticContent extends HttpServlet { public static class StaticContent extends HttpServlet {
@ -138,7 +136,7 @@ public class TomcatTestServer {
String result; String result;
File resourcePath = new File(resource); File resourcePath = new File(resource);
if(resourcePath.exists() && resourcePath.isFile()) { if (resourcePath.exists() && resourcePath.isFile()) {
FileInputStream fin = new FileInputStream(resourcePath); FileInputStream fin = new FileInputStream(resourcePath);
result = IOUtils.toString(fin, "UTF-8"); result = IOUtils.toString(fin, "UTF-8");
LOG.info("Mapped uri '{}' to resource '{}'.", uri, resource); LOG.info("Mapped uri '{}' to resource '{}'.", uri, resource);
@ -152,8 +150,9 @@ public class TomcatTestServer {
} }
private static TestServerBuilder builder; private static TestServerBuilder builder;
public static TestServerBuilder init(int port) { public static TestServerBuilder init(int port) {
if(builder == null) { if (builder == null) {
builder = new TestServerBuilder(port); builder = new TestServerBuilder(port);
} }
return builder; return builder;
@ -173,7 +172,7 @@ public class TomcatTestServer {
initializeProperties(); initializeProperties();
//baseDir = new File(System.getProperty("java.io.tmpdir"), "tomcat-test"); //baseDir = new File(System.getProperty("java.io.tmpdir"), "tomcat-test");
baseDir = getFileForDirProperty(TOMCAT_BASE_DIR); baseDir = getFileForDirProperty(TOMCAT_BASE_DIR);
if(!baseDir.exists() && !baseDir.mkdirs()) { if (!baseDir.exists() && !baseDir.mkdirs()) {
throw new RuntimeException("Unable to create temporary test directory at {" + baseDir.getAbsolutePath() + "}"); throw new RuntimeException("Unable to create temporary test directory at {" + baseDir.getAbsolutePath() + "}");
} }
// //
@ -204,14 +203,14 @@ public class TomcatTestServer {
} }
public TestServerBuilder addWebApp() throws IOException { public TestServerBuilder addWebApp() throws IOException {
if(server != null) { if (server != null) {
return this; return this;
} }
File webAppProjectDir = getFileForDirProperty(PROJECT_WEB_APP_DIR); File webAppProjectDir = getFileForDirProperty(PROJECT_WEB_APP_DIR);
File webAppDir = new File(baseDir, webAppProjectDir.getName()); File webAppDir = new File(baseDir, webAppProjectDir.getName());
FileUtils.deleteDirectory(webAppDir); FileUtils.deleteDirectory(webAppDir);
if(!webAppDir.mkdirs()) { if (!webAppDir.mkdirs()) {
throw new RuntimeException("Unable to create temporary directory at {" + webAppDir.getAbsolutePath() + "}"); throw new RuntimeException("Unable to create temporary directory at {" + webAppDir.getAbsolutePath() + "}");
} }
FileUtils.copyDirectory(webAppProjectDir, webAppDir); FileUtils.copyDirectory(webAppProjectDir, webAppDir);
@ -226,22 +225,23 @@ public class TomcatTestServer {
private File getFileForDirProperty(String propertyName) { private File getFileForDirProperty(String propertyName) {
File targetFile = new File(properties.getProperty(propertyName)); File targetFile = new File(properties.getProperty(propertyName));
if(targetFile.exists() && targetFile.isDirectory()) { if (targetFile.exists() && targetFile.isDirectory()) {
return targetFile; return targetFile;
} else if(targetFile.mkdirs()) { } else if (targetFile.mkdirs()) {
return targetFile; return targetFile;
} }
URL targetURL = Thread.currentThread().getContextClassLoader().getResource(targetFile.getPath()); URL targetURL = Thread.currentThread().getContextClassLoader().getResource(targetFile.getPath());
if(targetURL == null) { if (targetURL == null) {
throw new RuntimeException("Project target was not found at '" + throw new RuntimeException("Project target was not found at '" +
properties.getProperty(propertyName) + "'."); properties.getProperty(propertyName) + "'.");
} }
return new File(targetURL.getFile()); return new File(targetURL.getFile());
} }
public TestServerBuilder addServlet(final Class<? extends HttpServlet> factoryClass, String path) throws Exception { public TestServerBuilder addServlet(final Class<? extends HttpServlet> factoryClass, String path)
if(server != null) { throws InstantiationException, IllegalAccessException, ClassNotFoundException {
if (server != null) {
return this; return this;
} }
String odataServlet = factoryClass.getName(); String odataServlet = factoryClass.getName();
@ -264,7 +264,7 @@ public class TomcatTestServer {
} }
public TestServerBuilder addServlet(HttpServlet httpServlet, String name, String path) throws IOException { public TestServerBuilder addServlet(HttpServlet httpServlet, String name, String path) throws IOException {
if(server != null) { if (server != null) {
return this; return this;
} }
Context cxt = getContext(); Context cxt = getContext();
@ -278,14 +278,14 @@ public class TomcatTestServer {
private Context baseContext = null; private Context baseContext = null;
private Context getContext() { private Context getContext() {
if(baseContext == null) { if (baseContext == null) {
baseContext = tomcat.addContext("/", baseDir.getAbsolutePath()); baseContext = tomcat.addContext("/", baseDir.getAbsolutePath());
} }
return baseContext; return baseContext;
} }
public TomcatTestServer start() throws LifecycleException { public TomcatTestServer start() throws LifecycleException {
if(server != null) { if (server != null) {
return server; return server;
} }
tomcat.start(); tomcat.start();
@ -313,4 +313,4 @@ public class TomcatTestServer {
tomcat.destroy(); tomcat.destroy();
} }
} }
} }

View File

@ -18,6 +18,7 @@
*/ */
package org.apache.olingo.fit; package org.apache.olingo.fit;
import org.apache.catalina.LifecycleException;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.olingo.client.api.CommonODataClient; import org.apache.olingo.client.api.CommonODataClient;
import org.apache.olingo.commons.api.data.Entity; import org.apache.olingo.commons.api.data.Entity;
@ -37,6 +38,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringWriter; import java.io.StringWriter;
@ -48,12 +50,11 @@ public abstract class AbstractBaseTestITCase {
*/ */
protected static final Logger LOG = LoggerFactory.getLogger(AbstractBaseTestITCase.class); protected static final Logger LOG = LoggerFactory.getLogger(AbstractBaseTestITCase.class);
@SuppressWarnings("rawtypes") protected abstract CommonODataClient<?> getClient();
protected abstract CommonODataClient getClient();
@BeforeClass @BeforeClass
public static void init() throws Exception { public static void init()
throws LifecycleException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
TomcatTestServer.init(9080) TomcatTestServer.init(9080)
.addServlet(TechnicalServlet.class, "/olingo-server-tecsvc/odata.svc/*") .addServlet(TechnicalServlet.class, "/olingo-server-tecsvc/odata.svc/*")
.addServlet(StaticContent.class, "/olingo-server-tecsvc/v4.0/cs02/vocabularies/Org.OData.Core.V1.xml") .addServlet(StaticContent.class, "/olingo-server-tecsvc/v4.0/cs02/vocabularies/Org.OData.Core.V1.xml")
@ -128,8 +129,8 @@ public abstract class AbstractBaseTestITCase {
public static class StaticContent extends HttpServlet { public static class StaticContent extends HttpServlet {
private static final long serialVersionUID = -6663569573355398997L; private static final long serialVersionUID = -6663569573355398997L;
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) @Override
throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getOutputStream().write(IOUtils.toByteArray( resp.getOutputStream().write(IOUtils.toByteArray(
Thread.currentThread().getContextClassLoader().getResourceAsStream("org-odata-core-v1.xml"))); Thread.currentThread().getContextClassLoader().getResourceAsStream("org-odata-core-v1.xml")));
} }

View File

@ -53,25 +53,16 @@ import org.apache.olingo.commons.api.format.ODataFormat;
import org.apache.olingo.commons.api.http.HttpStatusCode; import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.fit.AbstractBaseTestITCase; import org.apache.olingo.fit.AbstractBaseTestITCase;
import org.apache.olingo.fit.tecsvc.TecSvcConst; import org.apache.olingo.fit.tecsvc.TecSvcConst;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
public class BasicITCase extends AbstractBaseTestITCase { public class BasicITCase extends AbstractBaseTestITCase {
private static final String SERVICE_URI = TecSvcConst.BASE_URI; private static final String SERVICE_URI = TecSvcConst.BASE_URI;
private ODataClient odata;
@Before
public void before() {
odata = ODataClientFactory.getV4();
odata.getConfiguration().setDefaultPubFormat(ODataFormat.JSON);
}
@Test @Test
public void readServiceDocument() { public void readServiceDocument() {
ODataServiceDocumentRequest request = odata.getRetrieveRequestFactory().getServiceDocumentRequest(SERVICE_URI); ODataServiceDocumentRequest request = getClient().getRetrieveRequestFactory()
.getServiceDocumentRequest(SERVICE_URI);
assertNotNull(request); assertNotNull(request);
ODataRetrieveResponse<ODataServiceDocument> response = request.execute(); ODataRetrieveResponse<ODataServiceDocument> response = request.execute();
@ -87,7 +78,7 @@ public class BasicITCase extends AbstractBaseTestITCase {
@Test @Test
public void readMetadata() { public void readMetadata() {
EdmMetadataRequest request = odata.getRetrieveRequestFactory().getMetadataRequest(SERVICE_URI); EdmMetadataRequest request = getClient().getRetrieveRequestFactory().getMetadataRequest(SERVICE_URI);
assertNotNull(request); assertNotNull(request);
ODataRetrieveResponse<Edm> response = request.execute(); ODataRetrieveResponse<Edm> response = request.execute();
@ -104,7 +95,7 @@ public class BasicITCase extends AbstractBaseTestITCase {
@Test @Test
public void readEntitySet() { public void readEntitySet() {
final ODataEntitySetRequest<ODataEntitySet> request = odata.getRetrieveRequestFactory() final ODataEntitySetRequest<ODataEntitySet> request = getClient().getRetrieveRequestFactory()
.getEntitySetRequest(getClient().newURIBuilder(SERVICE_URI) .getEntitySetRequest(getClient().newURIBuilder(SERVICE_URI)
.appendEntitySetSegment("ESMixPrimCollComp").build()); .appendEntitySetSegment("ESMixPrimCollComp").build());
assertNotNull(request); assertNotNull(request);
@ -134,7 +125,7 @@ public class BasicITCase extends AbstractBaseTestITCase {
@Test @Test
public void readException() throws Exception { public void readException() throws Exception {
final ODataEntityRequest<ODataEntity> request = odata.getRetrieveRequestFactory() final ODataEntityRequest<ODataEntity> request = getClient().getRetrieveRequestFactory()
.getEntityRequest(getClient().newURIBuilder(SERVICE_URI) .getEntityRequest(getClient().newURIBuilder(SERVICE_URI)
.appendEntitySetSegment("ESMixPrimCollComp").appendKeySegment("42").build()); .appendEntitySetSegment("ESMixPrimCollComp").appendKeySegment("42").build());
assertNotNull(request); assertNotNull(request);
@ -151,7 +142,7 @@ public class BasicITCase extends AbstractBaseTestITCase {
@Test @Test
public void readEntityRawResult() throws IOException { public void readEntityRawResult() throws IOException {
final ODataEntityRequest<ODataEntity> request = odata.getRetrieveRequestFactory() final ODataEntityRequest<ODataEntity> request = getClient().getRetrieveRequestFactory()
.getEntityRequest(getClient().newURIBuilder(SERVICE_URI) .getEntityRequest(getClient().newURIBuilder(SERVICE_URI)
.appendEntitySetSegment("ESCollAllPrim").appendKeySegment(1).build()); .appendEntitySetSegment("ESCollAllPrim").appendKeySegment(1).build());
assertNotNull(request); assertNotNull(request);
@ -187,7 +178,10 @@ public class BasicITCase extends AbstractBaseTestITCase {
assertEquals(expectedResult, IOUtils.toString(response.getRawResponse(), "UTF-8")); assertEquals(expectedResult, IOUtils.toString(response.getRawResponse(), "UTF-8"));
} }
@Override protected CommonODataClient<?> getClient() { @Override
return ODataClientFactory.getV4(); protected CommonODataClient<?> getClient() {
ODataClient odata = ODataClientFactory.getV4();
odata.getConfiguration().setDefaultPubFormat(ODataFormat.JSON);
return odata;
} }
} }

View File

@ -223,7 +223,7 @@ public class ODataJsonSerializer implements ODataSerializer {
ExpandSelectHelper.getSelectedPropertyNames(select.getSelectItems()); ExpandSelectHelper.getSelectedPropertyNames(select.getSelectItems());
for (final String propertyName : entityType.getPropertyNames()) { for (final String propertyName : entityType.getPropertyNames()) {
if (all || selected.contains(propertyName)) { if (all || selected.contains(propertyName)) {
final EdmProperty edmProperty = (EdmProperty) entityType.getProperty(propertyName); final EdmProperty edmProperty = entityType.getStructuralProperty(propertyName);
final Property property = entity.getProperty(propertyName); final Property property = entity.getProperty(propertyName);
final Set<List<String>> selectedPaths = all || edmProperty.isPrimitive() ? null : final Set<List<String>> selectedPaths = all || edmProperty.isPrimitive() ? null :
ExpandSelectHelper.getSelectedPaths(select.getSelectItems(), propertyName); ExpandSelectHelper.getSelectedPaths(select.getSelectItems(), propertyName);

View File

@ -527,15 +527,18 @@ public class MetadataDocumentXmlSerializer {
} }
} }
/** Appends a reference to the OData Core Vocabulary as defined in the OData specification
* and mentioned in its Common Schema Definition Language (CSDL) document.
*/
private void appendReference(final XMLStreamWriter writer) throws XMLStreamException { private void appendReference(final XMLStreamWriter writer) throws XMLStreamException {
writer.writeStartElement(NS_EDMX, "Reference"); writer.writeStartElement(NS_EDMX, "Reference");
// TODO: Where is this value comming from? // TODO: Which value can we use here?
// <http://docs.oasis-open.org/odata/odata/v4.0/cs02/vocabularies/Org.OData.Core.V1.xml>
// is an external site we don't want to query each time an EDM-enabled client is used.
writer.writeAttribute("Uri", writer.writeAttribute("Uri",
"http://localhost:9080/olingo-server-tecsvc/v4.0/cs02/vocabularies/Org.OData.Core.V1.xml"); "http://localhost:9080/olingo-server-tecsvc/v4.0/cs02/vocabularies/Org.OData.Core.V1.xml");
writer.writeEmptyElement(NS_EDMX, "Include"); writer.writeEmptyElement(NS_EDMX, "Include");
// TODO: Where is this value comming from?
writer.writeAttribute(XML_NAMESPACE, "Org.OData.Core.V1"); writer.writeAttribute(XML_NAMESPACE, "Org.OData.Core.V1");
// TODO: Where is this value comming from?
writer.writeAttribute(XML_ALIAS, "Core"); writer.writeAttribute(XML_ALIAS, "Core");
writer.writeEndElement(); writer.writeEndElement();
} }

View File

@ -32,7 +32,6 @@ import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataApplicationException; import org.apache.olingo.server.api.ODataApplicationException;
import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataRequest;
import org.apache.olingo.server.api.ODataResponse; import org.apache.olingo.server.api.ODataResponse;
import org.apache.olingo.server.api.ODataTranslatedException;
import org.apache.olingo.server.api.processor.EntityCollectionProcessor; import org.apache.olingo.server.api.processor.EntityCollectionProcessor;
import org.apache.olingo.server.api.processor.EntityProcessor; import org.apache.olingo.server.api.processor.EntityProcessor;
import org.apache.olingo.server.api.serializer.ODataSerializer; import org.apache.olingo.server.api.serializer.ODataSerializer;
@ -76,12 +75,14 @@ public class TechnicalProcessor implements EntityCollectionProcessor, EntityProc
if (entitySet == null) { if (entitySet == null) {
response.setStatusCode(HttpStatusCode.NOT_FOUND.getStatusCode()); response.setStatusCode(HttpStatusCode.NOT_FOUND.getStatusCode());
} else { } else {
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType)); final ODataFormat format = ODataFormat.fromContentType(requestedContentType);
ODataSerializer serializer = odata.createSerializer(format);
final ExpandOption expand = uriInfo.getExpandOption(); final ExpandOption expand = uriInfo.getExpandOption();
final SelectOption select = uriInfo.getSelectOption(); final SelectOption select = uriInfo.getSelectOption();
response.setContent(serializer.entitySet(edmEntitySet, entitySet, response.setContent(serializer.entitySet(edmEntitySet, entitySet,
ODataSerializerOptions.with() ODataSerializerOptions.with()
.contextURL(getContextUrl(serializer, edmEntitySet, false, expand, select)) .contextURL(format == ODataFormat.JSON_NO_METADATA ? null :
getContextUrl(serializer, edmEntitySet, false, expand, select))
.count(uriInfo.getCountOption()) .count(uriInfo.getCountOption())
.expand(expand).select(select) .expand(expand).select(select)
.build())); .build()));
@ -90,7 +91,7 @@ public class TechnicalProcessor implements EntityCollectionProcessor, EntityProc
} }
} catch (final DataProvider.DataProviderException e) { } catch (final DataProvider.DataProviderException e) {
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
} catch (final ODataTranslatedException e) { } catch (final ODataSerializerException e) {
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
} catch (final ODataApplicationException e) { } catch (final ODataApplicationException e) {
response.setStatusCode(e.getStatusCode()); response.setStatusCode(e.getStatusCode());
@ -110,21 +111,23 @@ public class TechnicalProcessor implements EntityCollectionProcessor, EntityProc
if (entity == null) { if (entity == null) {
response.setStatusCode(HttpStatusCode.NOT_FOUND.getStatusCode()); response.setStatusCode(HttpStatusCode.NOT_FOUND.getStatusCode());
} else { } else {
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType)); final ODataFormat format = ODataFormat.fromContentType(requestedContentType);
ODataSerializer serializer = odata.createSerializer(format);
final ExpandOption expand = uriInfo.getExpandOption(); final ExpandOption expand = uriInfo.getExpandOption();
final SelectOption select = uriInfo.getSelectOption(); final SelectOption select = uriInfo.getSelectOption();
response.setContent(serializer.entity(edmEntitySet, entity, response.setContent(serializer.entity(edmEntitySet, entity,
ODataSerializerOptions.with() ODataSerializerOptions.with()
.contextURL(getContextUrl(serializer, edmEntitySet, true, expand, select)) .contextURL(format == ODataFormat.JSON_NO_METADATA ? null :
getContextUrl(serializer, edmEntitySet, true, expand, select))
.count(uriInfo.getCountOption()) .count(uriInfo.getCountOption())
.expand(uriInfo.getExpandOption()).select(uriInfo.getSelectOption()) .expand(expand).select(select)
.build())); .build()));
response.setStatusCode(HttpStatusCode.OK.getStatusCode()); response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
} }
} catch (final DataProvider.DataProviderException e) { } catch (final DataProvider.DataProviderException e) {
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
} catch (final ODataTranslatedException e) { } catch (final ODataSerializerException e) {
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
} catch (final ODataApplicationException e) { } catch (final ODataApplicationException e) {
response.setStatusCode(e.getStatusCode()); response.setStatusCode(e.getStatusCode());