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

View File

@ -18,6 +18,7 @@
*/
package org.apache.olingo.fit;
import org.apache.catalina.LifecycleException;
import org.apache.commons.io.IOUtils;
import org.apache.olingo.client.api.CommonODataClient;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
@ -48,12 +50,11 @@ public abstract class AbstractBaseTestITCase {
*/
protected static final Logger LOG = LoggerFactory.getLogger(AbstractBaseTestITCase.class);
@SuppressWarnings("rawtypes")
protected abstract CommonODataClient getClient();
protected abstract CommonODataClient<?> getClient();
@BeforeClass
public static void init() throws Exception {
public static void init()
throws LifecycleException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
TomcatTestServer.init(9080)
.addServlet(TechnicalServlet.class, "/olingo-server-tecsvc/odata.svc/*")
.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 {
private static final long serialVersionUID = -6663569573355398997L;
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getOutputStream().write(IOUtils.toByteArray(
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.fit.AbstractBaseTestITCase;
import org.apache.olingo.fit.tecsvc.TecSvcConst;
import org.junit.Before;
import org.junit.Test;
public class BasicITCase extends AbstractBaseTestITCase {
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
public void readServiceDocument() {
ODataServiceDocumentRequest request = odata.getRetrieveRequestFactory().getServiceDocumentRequest(SERVICE_URI);
ODataServiceDocumentRequest request = getClient().getRetrieveRequestFactory()
.getServiceDocumentRequest(SERVICE_URI);
assertNotNull(request);
ODataRetrieveResponse<ODataServiceDocument> response = request.execute();
@ -87,7 +78,7 @@ public class BasicITCase extends AbstractBaseTestITCase {
@Test
public void readMetadata() {
EdmMetadataRequest request = odata.getRetrieveRequestFactory().getMetadataRequest(SERVICE_URI);
EdmMetadataRequest request = getClient().getRetrieveRequestFactory().getMetadataRequest(SERVICE_URI);
assertNotNull(request);
ODataRetrieveResponse<Edm> response = request.execute();
@ -104,7 +95,7 @@ public class BasicITCase extends AbstractBaseTestITCase {
@Test
public void readEntitySet() {
final ODataEntitySetRequest<ODataEntitySet> request = odata.getRetrieveRequestFactory()
final ODataEntitySetRequest<ODataEntitySet> request = getClient().getRetrieveRequestFactory()
.getEntitySetRequest(getClient().newURIBuilder(SERVICE_URI)
.appendEntitySetSegment("ESMixPrimCollComp").build());
assertNotNull(request);
@ -134,7 +125,7 @@ public class BasicITCase extends AbstractBaseTestITCase {
@Test
public void readException() throws Exception {
final ODataEntityRequest<ODataEntity> request = odata.getRetrieveRequestFactory()
final ODataEntityRequest<ODataEntity> request = getClient().getRetrieveRequestFactory()
.getEntityRequest(getClient().newURIBuilder(SERVICE_URI)
.appendEntitySetSegment("ESMixPrimCollComp").appendKeySegment("42").build());
assertNotNull(request);
@ -151,7 +142,7 @@ public class BasicITCase extends AbstractBaseTestITCase {
@Test
public void readEntityRawResult() throws IOException {
final ODataEntityRequest<ODataEntity> request = odata.getRetrieveRequestFactory()
final ODataEntityRequest<ODataEntity> request = getClient().getRetrieveRequestFactory()
.getEntityRequest(getClient().newURIBuilder(SERVICE_URI)
.appendEntitySetSegment("ESCollAllPrim").appendKeySegment(1).build());
assertNotNull(request);
@ -187,7 +178,10 @@ public class BasicITCase extends AbstractBaseTestITCase {
assertEquals(expectedResult, IOUtils.toString(response.getRawResponse(), "UTF-8"));
}
@Override protected CommonODataClient<?> getClient() {
return ODataClientFactory.getV4();
@Override
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());
for (final String propertyName : entityType.getPropertyNames()) {
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 Set<List<String>> selectedPaths = all || edmProperty.isPrimitive() ? null :
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 {
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",
"http://localhost:9080/olingo-server-tecsvc/v4.0/cs02/vocabularies/Org.OData.Core.V1.xml");
writer.writeEmptyElement(NS_EDMX, "Include");
// TODO: Where is this value comming from?
writer.writeAttribute(XML_NAMESPACE, "Org.OData.Core.V1");
// TODO: Where is this value comming from?
writer.writeAttribute(XML_ALIAS, "Core");
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.ODataRequest;
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.EntityProcessor;
import org.apache.olingo.server.api.serializer.ODataSerializer;
@ -76,12 +75,14 @@ public class TechnicalProcessor implements EntityCollectionProcessor, EntityProc
if (entitySet == null) {
response.setStatusCode(HttpStatusCode.NOT_FOUND.getStatusCode());
} 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 SelectOption select = uriInfo.getSelectOption();
response.setContent(serializer.entitySet(edmEntitySet, entitySet,
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())
.expand(expand).select(select)
.build()));
@ -90,7 +91,7 @@ public class TechnicalProcessor implements EntityCollectionProcessor, EntityProc
}
} catch (final DataProvider.DataProviderException e) {
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
} catch (final ODataTranslatedException e) {
} catch (final ODataSerializerException e) {
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
} catch (final ODataApplicationException e) {
response.setStatusCode(e.getStatusCode());
@ -110,21 +111,23 @@ public class TechnicalProcessor implements EntityCollectionProcessor, EntityProc
if (entity == null) {
response.setStatusCode(HttpStatusCode.NOT_FOUND.getStatusCode());
} 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 SelectOption select = uriInfo.getSelectOption();
response.setContent(serializer.entity(edmEntitySet, entity,
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())
.expand(uriInfo.getExpandOption()).select(uriInfo.getSelectOption())
.expand(expand).select(select)
.build()));
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
}
} catch (final DataProvider.DataProviderException e) {
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
} catch (final ODataTranslatedException e) {
} catch (final ODataSerializerException e) {
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
} catch (final ODataApplicationException e) {
response.setStatusCode(e.getStatusCode());