[OLINGO-317] more translatable exceptions

Change-Id: I7cf9ad31b0aa2b136e75133a901516fb02dd11bb

Signed-off-by: Christian Amend <chrisam@apache.org>
This commit is contained in:
Klaus Straubinger 2014-07-25 11:06:59 +02:00 committed by Christian Amend
parent e4b7ef1a96
commit 633ef32216
21 changed files with 215 additions and 164 deletions

View File

@ -23,11 +23,12 @@ import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.format.ODataFormat;
import org.apache.olingo.server.api.edm.provider.EdmProvider;
import org.apache.olingo.server.api.serializer.ODataSerializer;
import org.apache.olingo.server.api.serializer.ODataSerializerException;
/**
* Root object for serving factory tasks and support loosely coupling of implementation (core) from the api. This is not
* a singleton (static variables) to avoid issues with synchronization, OSGi, hot deployment and so on. Each thread
* (request) should keep its own instance.
* Root object for serving factory tasks and support loosely coupling of implementation (core) from the api.
* This is not a singleton (static variables) to avoid issues with synchronization, OSGi, hot deployment and so on.
* Each thread (request) should keep its own instance.
*/
public abstract class OData {
@ -46,26 +47,25 @@ public abstract class OData {
return (OData) object;
} catch (final Exception e) {
// TODO: Change to ODataRuntimeExcfeption
throw new ODataRuntimeException(e);
}
}
/**
* Create a new serializer object for rendering content in the specified format. Serializers are used in Processor
* implementations.
* Creates a new serializer object for rendering content in the specified format.
* Serializers are used in Processor implementations.
* @param format - Any format supported by Olingo (XML, JSON ...)
*/
public abstract ODataSerializer createSerializer(ODataFormat format);
public abstract ODataSerializer createSerializer(ODataFormat format) throws ODataSerializerException;
/**
* Create a new ODataHttpHandler for handling OData requests in a http context.
* Creates a new ODataHttpHandler for handling OData requests in an HTTP context.
* @param edm - metadata object required to handle an OData request
*/
public abstract ODataHttpHandler createHandler(Edm edm);
/**
* Create an metadata object.
* Creates an metadata object.
* @param edmProvider - A custom or default implementation for creating metadata
*/
public abstract Edm createEdm(EdmProvider edmProvider);

View File

@ -36,22 +36,34 @@ public class ODataTranslatedException extends ODataException {
private static final String BUNDLE_NAME = "i18n";
public static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
// MessageKeys
public static final String AMBIGUOUS_XHTTP_METHOD = "ODataTranslatedException.AMBIGUOUS_XHTTP_METHOD";
public static final String HTTP_METHOD_NOT_IMPLEMENTED = "ODataTranslatedException.HTTP_METHOD_NOT_IMPLEMENTED";
public static final String PROCESSOR_NOT_IMPLEMENTED = "ODataTranslatedException.PROCESSOR_NOT_IMPLEMENTED";
public static final String ODATA_VERSION_NOT_SUPPORTED = "ODataTranslatedException.ODATA_VERSION_NOT_SUPPORTED";
private String messageKey;
protected static interface MessageKey {}
public static enum MessageKeys implements MessageKey {
AMBIGUOUS_XHTTP_METHOD,
HTTP_METHOD_NOT_IMPLEMENTED,
PROCESSOR_NOT_IMPLEMENTED,
FUNCTIONALITY_NOT_IMPLEMENTED,
ODATA_VERSION_NOT_SUPPORTED,
/** parameters: HTTP header name, HTTP header value */
WRONG_CHARSET_IN_HEADER,
/** parameter: list of content-type ranges */
UNSUPPORTED_CONTENT_TYPES,
/** parameter: content type */
UNSUPPORTED_CONTENT_TYPE
}
private MessageKey messageKey;
private Object[] parameters;
public ODataTranslatedException(String developmentMessage, String messageKey, String... parameters) {
public ODataTranslatedException(String developmentMessage, MessageKey messageKey, String... parameters) {
super(developmentMessage);
this.messageKey = messageKey;
this.parameters = parameters;
}
public ODataTranslatedException(String developmentMessage, Throwable cause, String messageKey, String... parameters) {
public ODataTranslatedException(String developmentMessage, Throwable cause, MessageKey messageKey,
String... parameters) {
super(developmentMessage, cause);
this.messageKey = messageKey;
this.parameters = parameters;
@ -67,7 +79,7 @@ public class ODataTranslatedException extends ODataException {
return getMessage();
}
public String getMessageKey() {
public MessageKey getMessageKey() {
return messageKey;
}
@ -101,7 +113,7 @@ public class ODataTranslatedException extends ODataException {
String message = null;
try {
message = bundle.getString(messageKey);
message = bundle.getString(getClass().getSimpleName() + '.' + messageKey);
StringBuilder builder = new StringBuilder();
Formatter f = new Formatter(builder, locale);
f.format(message, parameters);

View File

@ -59,10 +59,10 @@ public abstract class EdmProvider {
}
/**
* This method should return a {@link ComplexType} or <b>null</b> if nothing is found
* This method should return a {@link ComplexType} or <b>null</b> if nothing is found.
*
* @param complexTypeName
* @return {@link StructuralType} for the given name
* @return {@link ComplexType} for the given name
* @throws ODataException
*/
public ComplexType getComplexType(final FullQualifiedName complexTypeName) throws ODataException {
@ -92,7 +92,12 @@ public abstract class EdmProvider {
return null;
}
// TODO: document
/**
* This method should return a {@link Term} for the FullQualifiedName or <b>null</b> if nothing is found.
* @param termName the name of the Term
* @return {@link Term} or null
* @throws ODataException
*/
public Term getTerm(final FullQualifiedName termName) throws ODataException {
return null;
}

View File

@ -52,9 +52,8 @@ public class DefaultProcessor implements MetadataProcessor, ServiceDocumentProce
@Override
public void readServiceDocument(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo,
final ContentType requestedContentType) {
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType));
try {
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType));
response.setContent(serializer.serviceDocument(edm, request.getRawBaseUri()));
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
@ -66,8 +65,8 @@ public class DefaultProcessor implements MetadataProcessor, ServiceDocumentProce
@Override
public void readMetadata(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo,
final ContentType requestedContentType) {
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType));
try {
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType));
response.setContent(serializer.metadataDocument(edm));
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());

View File

@ -24,27 +24,26 @@ public class ODataSerializerException extends ODataTranslatedException {
private static final long serialVersionUID = 5358683245923127425L;
// MessageKeys
public static final String NOT_IMPLEMENTED = "ODataSerializerException.NOT_IMPLEMENTED";
public static final String JSON_METADATA = "ODataSerializerException.JSON_METADATA";
public static final String IO_EXCEPTION = "ODataSerializerException.IO_EXCEPTION";
public static final String NO_CONTEXT_URL = "ODataSerializerException.NO_CONTEXT_URL";
/** parameter: property name */
public static final String UNSUPPORTED_PROPERTY_TYPE = "ODataSerializerException.UNSUPPORTED_PROPERTY_TYPE";
/** parameter: property name */
public static final String INCONSISTENT_PROPERTY_TYPE = "ODataSerializerException.INCONSISTENT_PROPERTY_TYPE";
/** parameter: property name */
public static final String MISSING_PROPERTY = "ODataSerializerException.MISSING_PROPERTY";
/** parameters: property name, property value */
public static final String WRONG_PROPERTY_VALUE = "ODataSerializerException.WRONG_PROPERTY_VALUE";
public static enum MessageKeys implements MessageKey {
NOT_IMPLEMENTED,
/** parameter: format */ UNSUPPORTED_FORMAT,
JSON_METADATA,
IO_EXCEPTION,
NULL_INPUT,
NO_CONTEXT_URL,
/** parameter: property name */ UNSUPPORTED_PROPERTY_TYPE,
/** parameter: property name */ INCONSISTENT_PROPERTY_TYPE,
/** parameter: property name */ MISSING_PROPERTY,
/** parameters: property name, property value */ WRONG_PROPERTY_VALUE
}
public ODataSerializerException(final String developmentMessage,
final String messageKey, final String... parameters) {
final MessageKey messageKey, final String... parameters) {
super(developmentMessage, messageKey, parameters);
}
public ODataSerializerException(final String developmentMessage, final Throwable cause,
final String messageKey, final String... parameters) {
final MessageKey messageKey, final String... parameters) {
super(developmentMessage, cause, messageKey, parameters);
}
}

View File

@ -19,13 +19,19 @@
# Basic Apache Olingo exception messages
#
ODataTranslatedException.AMBIGUOUS_XHTTP_METHOD=x-http-method header '%1$s' and x-http-method-override header '%2$s' are not the same.
ODataTranslatedException.HTTP_METHOD_NOT_IMPLEMENTED=Invalid http method given: '%1$s'.
ODataTranslatedException.HTTP_METHOD_NOT_IMPLEMENTED=Invalid HTTP method given: '%1$s'.
ODataTranslatedException.PROCESSOR_NOT_IMPLEMENTED=No processor for interface '%1$s' registered.
ODataTranslatedException.FUNCTIONALITY_NOT_IMPLEMENTED=The requested functionality has not been implemented (yet).
ODataTranslatedException.ODATA_VERSION_NOT_SUPPORTED=OData version '%1$s' is not supported.
ODataTranslatedException.WRONG_CHARSET_IN_HEADER=The HTTP header '%1$s' with value '%2$s' contains an invalid character-set specification.
ODataTranslatedException.UNSUPPORTED_CONTENT_TYPES=The content-type range '%1$s' is not supported.
ODataTranslatedException.UNSUPPORTED_CONTENT_TYPE=The content type '%1$s' is not supported.
ODataSerializerException.NOT_IMPLEMENTED=The requested serialization method has not been implemented yet.
ODataSerializerException.UNSUPPORTED_FORMAT=The format '%1$s' is not supported.
ODataSerializerException.JSON_METADATA=The metadata document cannot be provided in JSON format.
ODataSerializerException.IO_EXCEPTION=An I/O exception occurred.
ODataSerializerException.NULL_INPUT=The input 'null' is not allowed here.
ODataSerializerException.NO_CONTEXT_URL=No context URL has been provided.
ODataSerializerException.UNSUPPORTED_PROPERTY_TYPE=The type of the property '%1$s' is not yet supported.
ODataSerializerException.INCONSISTENT_PROPERTY_TYPE=An inconsistency has been detected in the type definition of property '%1$s'.

View File

@ -30,9 +30,9 @@ import org.junit.Test;
public class TranslatedExceptionsTest {
private static final String DEV = "devMessage";
private static final String BASIC = "BASIC";
private static final String ONEPARAM = "ONEPARAM";
private static final String TWOPARAM = "TWOPARAM";
private static enum Keys implements ODataTranslatedException.MessageKey {
BASIC, ONEPARAM, TWOPARAM, NOMESSAGE
}
public TranslatedExceptionsTest() {
// for test reason we assume a system with a default Locale.ENGLISH
@ -41,7 +41,7 @@ public class TranslatedExceptionsTest {
@Test
public void basic() {
ODataTranslatedException exp = new ODataTranslatedException(DEV, BASIC);
ODataTranslatedException exp = new ODataTranslatedException(DEV, Keys.BASIC);
assertEquals(DEV, exp.getMessage());
ODataErrorMessage translatedMessage = exp.getTranslatedMessage(null);
@ -70,7 +70,7 @@ public class TranslatedExceptionsTest {
@Test
public void unusedParametersMustNotResultInAnException() {
ODataTranslatedException exp = new ODataTranslatedException(DEV, BASIC, "unusedParam1", "unusedParam2");
ODataTranslatedException exp = new ODataTranslatedException(DEV, Keys.BASIC, "unusedParam1", "unusedParam2");
assertEquals(DEV, exp.getMessage());
ODataErrorMessage translatedMessage = exp.getTranslatedMessage(null);
@ -80,7 +80,7 @@ public class TranslatedExceptionsTest {
@Test
public void useOneParameter() {
ODataTranslatedException exp = new ODataTranslatedException(DEV, ONEPARAM, "usedParam1");
ODataTranslatedException exp = new ODataTranslatedException(DEV, Keys.ONEPARAM, "usedParam1");
assertEquals(DEV, exp.getMessage());
ODataErrorMessage translatedMessage = exp.getTranslatedMessage(null);
@ -90,7 +90,7 @@ public class TranslatedExceptionsTest {
@Test
public void useOneParameterExpectedButMultipleGiven() {
ODataTranslatedException exp = new ODataTranslatedException(DEV, ONEPARAM, "usedParam1", "unusedParam2");
ODataTranslatedException exp = new ODataTranslatedException(DEV, Keys.ONEPARAM, "usedParam1", "unusedParam2");
assertEquals(DEV, exp.getMessage());
ODataErrorMessage translatedMessage = exp.getTranslatedMessage(null);
@ -100,7 +100,7 @@ public class TranslatedExceptionsTest {
@Test
public void useTwoParameter() {
ODataTranslatedException exp = new ODataTranslatedException(DEV, TWOPARAM, "usedParam1", "usedParam2");
ODataTranslatedException exp = new ODataTranslatedException(DEV, Keys.TWOPARAM, "usedParam1", "usedParam2");
assertEquals(DEV, exp.getMessage());
ODataErrorMessage translatedMessage = exp.getTranslatedMessage(null);
@ -110,7 +110,7 @@ public class TranslatedExceptionsTest {
@Test
public void parametersNotGivenAltoughNeeded() {
ODataTranslatedException exp = new ODataTranslatedException(DEV, ONEPARAM);
ODataTranslatedException exp = new ODataTranslatedException(DEV, Keys.ONEPARAM);
assertEquals(DEV, exp.getMessage());
ODataErrorMessage translatedMessage = exp.getTranslatedMessage(null);
@ -120,7 +120,7 @@ public class TranslatedExceptionsTest {
@Test
public void noMessageForKey() {
ODataTranslatedException exp = new ODataTranslatedException(DEV, "NOMESSAGE");
ODataTranslatedException exp = new ODataTranslatedException(DEV, Keys.NOMESSAGE);
assertEquals(DEV, exp.getMessage());
ODataErrorMessage translatedMessage = exp.getTranslatedMessage(null);
@ -131,11 +131,11 @@ public class TranslatedExceptionsTest {
@Test
public void keyForRootBundleButNotPresentInDerivedBundle() {
ODataTranslatedException exp =
new ODataTranslatedException(DEV, ODataTranslatedException.HTTP_METHOD_NOT_IMPLEMENTED, "param1");
new ODataTranslatedException(DEV, ODataTranslatedException.MessageKeys.HTTP_METHOD_NOT_IMPLEMENTED, "param1");
assertEquals(DEV, exp.getMessage());
ODataErrorMessage translatedMessage = exp.getTranslatedMessage(Locale.GERMAN);
assertNotNull(translatedMessage);
assertEquals("Invalid http method given: 'param1'.", translatedMessage.getMessage());
assertEquals("Invalid HTTP method given: 'param1'.", translatedMessage.getMessage());
}
}

View File

@ -16,4 +16,4 @@
# specific language governing permissions and limitations
# under the License.
#-------------------------------------------------------------------------------
BASIC=Test DE
ODataTranslatedException.BASIC=Test DE

View File

@ -18,6 +18,6 @@
#-------------------------------------------------------------------------------
# Basic Apache Olingo exception messages
#
BASIC=Test Default
ONEPARAM=Param1: %1$s
TWOPARAM=Param1: %1$s Param2: %2$s
ODataTranslatedException.BASIC=Test Default
ODataTranslatedException.ONEPARAM=Param1: %1$s
ODataTranslatedException.TWOPARAM=Param1: %1$s Param2: %2$s

View File

@ -18,13 +18,13 @@
*/
package org.apache.olingo.server.core;
import org.apache.olingo.commons.api.ODataRuntimeException;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.api.format.AcceptType;
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.server.api.ODataRequest;
import org.apache.olingo.server.api.ODataTranslatedException;
import org.apache.olingo.server.api.processor.CustomContentTypeSupportProcessor;
import org.apache.olingo.server.api.processor.FormatContentTypeMapping;
import org.apache.olingo.server.api.processor.MetadataProcessor;
@ -71,7 +71,7 @@ public class ContentNegotiator {
}
public static ContentType doContentNegotiation(final FormatOption formatOption, final ODataRequest request,
final Processor processor, final Class<? extends Processor> processorClass) {
final Processor processor, final Class<? extends Processor> processorClass) throws ODataTranslatedException {
ContentType requestedContentType = null;
List<FormatContentTypeMapping> supportedContentTypes = getSupportedContentTypes(processor, processorClass);
@ -119,7 +119,8 @@ public class ContentNegotiator {
if ("utf8".equalsIgnoreCase(value) || "utf-8".equalsIgnoreCase(value)) {
ct = ContentType.create(ct, ContentType.PARAMETER_CHARSET_UTF8);
} else {
throw new ODataRuntimeException("charset in accept header not supported: " + acceptHeaderValue);
throw new ODataTranslatedException("charset in accept header not supported: " + acceptHeaderValue,
ODataTranslatedException.MessageKeys.WRONG_CHARSET_IN_HEADER, HttpHeader.ACCEPT, acceptHeaderValue);
}
}
@ -135,8 +136,9 @@ public class ContentNegotiator {
}
if (requestedContentType == null) {
throw new ODataRuntimeException("unsupported accept content type: " + acceptedContentTypes + " != "
+ supportedContentTypes);
throw new ODataTranslatedException(
"unsupported accept content type: " + acceptedContentTypes + " != " + supportedContentTypes,
ODataTranslatedException.MessageKeys.UNSUPPORTED_CONTENT_TYPES, acceptedContentTypes.toString());
}
} else {
@ -155,8 +157,9 @@ public class ContentNegotiator {
}
if (!supported) {
throw new ODataRuntimeException("unsupported accept content type: " + requestedContentType + " != "
+ supportedContentTypes);
throw new ODataTranslatedException(
"unsupported accept content type: " + requestedContentType + " != " + supportedContentTypes,
ODataTranslatedException.MessageKeys.UNSUPPORTED_CONTENT_TYPE, requestedContentType.toContentTypeString());
}
LOG.debug("requested content type: " + requestedContentType);

View File

@ -45,8 +45,8 @@ public class ODataExceptionHandler {
error.setMessage(e.getMessage());
}
ODataSerializer serializer = OData.newInstance().createSerializer(requestedFormat);
try {
ODataSerializer serializer = OData.newInstance().createSerializer(requestedFormat);
resp.setContent(serializer.error(error));
} catch (final ODataSerializerException e1) {}
// Set header

View File

@ -29,6 +29,7 @@ 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.HttpMethod;
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;
@ -105,7 +106,8 @@ public class ODataHandler {
handleResourceDispatching(request, response, uriInfo);
break;
default:
throw new ODataRuntimeException("not implemented");
throw new ODataTranslatedException("not implemented",
ODataTranslatedException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED);
}
} catch (ODataTranslatedException e) {
Locale requestedLocale = null;
@ -130,8 +132,8 @@ public class ODataHandler {
requestedContentType = ODataFormat.JSON.getContentType(ODataServiceVersion.V40);
}
exceptionProcessor.processException(request, response, serverError, requestedContentType);
} catch (ODataTranslatedException e1) {
throw new ODataRuntimeException("Could not instanciate ExceptionProcessor");
} catch (ODataTranslatedException e) {
throw new ODataRuntimeException("Could not instantiate ExceptionProcessor");
}
}
@ -151,9 +153,8 @@ public class ODataHandler {
// }
// }
private void
handleResourceDispatching(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo)
throws ODataTranslatedException {
private void handleResourceDispatching(final ODataRequest request, final ODataResponse response,
final UriInfo uriInfo) throws ODataTranslatedException {
int lastPathSegmentIndex = uriInfo.getUriResourceParts().size() - 1;
UriResource lastPathSegment = uriInfo.getUriResourceParts().get(lastPathSegmentIndex);
ContentType requestedContentType = null;
@ -169,7 +170,8 @@ public class ODataHandler {
cp.readCollection(request, response, uriInfo, requestedContentType);
} else {
throw new ODataRuntimeException("not implemented");
throw new ODataTranslatedException("not implemented",
ODataTranslatedException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED);
}
} else {
if (request.getMethod().equals(HttpMethod.GET)) {
@ -180,7 +182,8 @@ public class ODataHandler {
ep.readEntity(request, response, uriInfo, requestedContentType);
} else {
throw new ODataRuntimeException("not implemented");
throw new ODataTranslatedException("not implemented",
ODataTranslatedException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED);
}
}
break;
@ -194,7 +197,8 @@ public class ODataHandler {
cp.readCollection(request, response, uriInfo, requestedContentType);
} else {
throw new ODataRuntimeException("not implemented");
throw new ODataTranslatedException("not implemented",
ODataTranslatedException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED);
}
} else {
if (request.getMethod().equals(HttpMethod.GET)) {
@ -205,12 +209,14 @@ public class ODataHandler {
ep.readEntity(request, response, uriInfo, requestedContentType);
} else {
throw new ODataRuntimeException("not implemented");
throw new ODataTranslatedException("not implemented",
ODataTranslatedException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED);
}
}
break;
default:
throw new ODataRuntimeException("not implemented");
throw new ODataTranslatedException("not implemented",
ODataTranslatedException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED);
}
}
@ -223,7 +229,7 @@ public class ODataHandler {
if (ODataServiceVersion.isBiggerThan(ODataServiceVersion.V40.toString(), maxVersion)) {
response.setStatusCode(400);
throw new ODataTranslatedException("ODataVersion not supported: " + maxVersion,
ODataTranslatedException.ODATA_VERSION_NOT_SUPPORTED, maxVersion);
ODataTranslatedException.MessageKeys.ODATA_VERSION_NOT_SUPPORTED, maxVersion);
}
}
}
@ -234,9 +240,9 @@ public class ODataHandler {
T p = (T) processors.get(cls);
if (p == null) {
response.setStatusCode(501);
response.setStatusCode(HttpStatusCode.NOT_IMPLEMENTED.getStatusCode());
throw new ODataTranslatedException("Processor: " + cls.getName() + " not registered.",
ODataTranslatedException.PROCESSOR_NOT_IMPLEMENTED, cls.getName());
ODataTranslatedException.MessageKeys.PROCESSOR_NOT_IMPLEMENTED, cls.getName());
}
return p;

View File

@ -29,11 +29,13 @@ 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.Processor;
import org.apache.olingo.server.api.serializer.ODataSerializerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -77,9 +79,9 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
ODataResponse resp = new ODataResponse();
if (e instanceof ODataTranslatedException) {
ODataTranslatedException exp = (ODataTranslatedException) e;
if (exp.getMessageKey() == ODataTranslatedException.AMBIGUOUS_XHTTP_METHOD) {
if (exp.getMessageKey() == ODataTranslatedException.MessageKeys.AMBIGUOUS_XHTTP_METHOD) {
resp.setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode());
} else if (exp.getMessageKey() == ODataTranslatedException.HTTP_METHOD_NOT_IMPLEMENTED) {
} else if (exp.getMessageKey() == ODataTranslatedException.MessageKeys.HTTP_METHOD_NOT_IMPLEMENTED) {
resp.setStatusCode(HttpStatusCode.NOT_IMPLEMENTED.getStatusCode());
}
}
@ -131,8 +133,9 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
extractUri(odRequest, httpRequest, split);
return odRequest;
} catch (IOException e) {
throw new ODataRuntimeException(e);
} catch (final IOException e) {
throw new ODataSerializerException("An I/O exception occurred.", e,
ODataSerializerException.MessageKeys.IO_EXCEPTION);
}
}
@ -154,7 +157,7 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
} else {
if (!xHttpMethod.equalsIgnoreCase(xHttpMethodOverride)) {
throw new ODataTranslatedException("Ambiguous X-HTTP-Methods",
ODataTranslatedException.AMBIGUOUS_XHTTP_METHOD, xHttpMethod, xHttpMethodOverride);
ODataTranslatedException.MessageKeys.AMBIGUOUS_XHTTP_METHOD, xHttpMethod, xHttpMethodOverride);
}
odRequest.setMethod(HttpMethod.valueOf(xHttpMethod));
}
@ -163,7 +166,7 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
}
} catch (IllegalArgumentException e) {
throw new ODataTranslatedException("Invalid http method" + httpRequest.getMethod(),
ODataTranslatedException.HTTP_METHOD_NOT_IMPLEMENTED, httpRequest.getMethod());
ODataTranslatedException.MessageKeys.HTTP_METHOD_NOT_IMPLEMENTED, httpRequest.getMethod());
}
}

View File

@ -18,13 +18,13 @@
*/
package org.apache.olingo.server.core;
import org.apache.olingo.commons.api.ODataRuntimeException;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.format.ODataFormat;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataHttpHandler;
import org.apache.olingo.server.api.edm.provider.EdmProvider;
import org.apache.olingo.server.api.serializer.ODataSerializer;
import org.apache.olingo.server.api.serializer.ODataSerializerException;
import org.apache.olingo.server.core.edm.provider.EdmProviderImpl;
import org.apache.olingo.server.core.serializer.ODataXmlSerializerImpl;
import org.apache.olingo.server.core.serializer.json.ODataJsonSerializer;
@ -32,7 +32,7 @@ import org.apache.olingo.server.core.serializer.json.ODataJsonSerializer;
public class ODataImpl extends OData {
@Override
public ODataSerializer createSerializer(final ODataFormat format) {
public ODataSerializer createSerializer(final ODataFormat format) throws ODataSerializerException {
ODataSerializer serializer;
switch (format) {
case JSON:
@ -44,7 +44,8 @@ public class ODataImpl extends OData {
serializer = new ODataXmlSerializerImpl();
break;
default:
throw new ODataRuntimeException("Unsupported format: " + format);
throw new ODataSerializerException("Unsupported format: " + format,
ODataSerializerException.MessageKeys.UNSUPPORTED_FORMAT, format.toString());
}
return serializer;

View File

@ -44,7 +44,7 @@ public class ODataXmlSerializerImpl implements ODataSerializer {
@Override
public InputStream serviceDocument(final Edm edm, final String serviceRoot) throws ODataSerializerException {
throw new ODataSerializerException("Service Document not implemented for XML format",
ODataSerializerException.NOT_IMPLEMENTED);
ODataSerializerException.MessageKeys.NOT_IMPLEMENTED);
}
@Override
@ -64,13 +64,15 @@ public class ODataXmlSerializerImpl implements ODataSerializer {
return buffer.getInputStream();
} catch (final XMLStreamException e) {
log.error(e.getMessage(), e);
throw new ODataSerializerException("An I/O exception occurred.", e, ODataSerializerException.IO_EXCEPTION);
throw new ODataSerializerException("An I/O exception occurred.", e,
ODataSerializerException.MessageKeys.IO_EXCEPTION);
} finally {
if (xmlStreamWriter != null) {
try {
xmlStreamWriter.close();
} catch (XMLStreamException e) {
throw new ODataSerializerException("An I/O exception occurred.", e, ODataSerializerException.IO_EXCEPTION);
throw new ODataSerializerException("An I/O exception occurred.", e,
ODataSerializerException.MessageKeys.IO_EXCEPTION);
}
}
}
@ -80,20 +82,20 @@ public class ODataXmlSerializerImpl implements ODataSerializer {
public InputStream entity(final EdmEntitySet edmEntitySet, final Entity entity, final ContextURL contextURL)
throws ODataSerializerException {
throw new ODataSerializerException("Entity serialization not implemented for XML format",
ODataSerializerException.NOT_IMPLEMENTED);
ODataSerializerException.MessageKeys.NOT_IMPLEMENTED);
}
@Override
public InputStream entitySet(final EdmEntitySet edmEntitySet, final EntitySet entitySet,
final ContextURL contextURL) throws ODataSerializerException {
throw new ODataSerializerException("Entityset serialization not implemented for XML format",
ODataSerializerException.NOT_IMPLEMENTED);
ODataSerializerException.MessageKeys.NOT_IMPLEMENTED);
}
@Override
public InputStream error(ODataServerError error) throws ODataSerializerException {
throw new ODataSerializerException("error serialization not implemented for XML format",
ODataSerializerException.NOT_IMPLEMENTED);
ODataSerializerException.MessageKeys.NOT_IMPLEMENTED);
}
}

View File

@ -21,17 +21,19 @@ package org.apache.olingo.server.core.serializer.json;
import java.io.IOException;
import org.apache.olingo.commons.api.Constants;
import org.apache.olingo.commons.api.ODataRuntimeException;
import org.apache.olingo.commons.api.domain.ODataError;
import org.apache.olingo.commons.api.domain.ODataErrorDetail;
import org.apache.olingo.server.api.serializer.ODataSerializerException;
import com.fasterxml.jackson.core.JsonGenerator;
public class ODataErrorSerializer {
public void writeErrorDocument(JsonGenerator json, ODataError error) throws IOException {
public void writeErrorDocument(JsonGenerator json, final ODataError error)
throws IOException, ODataSerializerException {
if (error == null) {
throw new ODataRuntimeException("ODataError object MUST NOT be null!");
throw new ODataSerializerException("ODataError object MUST NOT be null!",
ODataSerializerException.MessageKeys.NULL_INPUT);
}
json.writeStartObject();
json.writeFieldName(Constants.JSON_ERROR);

View File

@ -83,13 +83,15 @@ public class ODataJsonSerializer implements ODataSerializer {
} catch (final IOException e) {
log.error(e.getMessage(), e);
throw new ODataSerializerException("An I/O exception occurred.", e, ODataSerializerException.IO_EXCEPTION);
throw new ODataSerializerException("An I/O exception occurred.", e,
ODataSerializerException.MessageKeys.IO_EXCEPTION);
} finally {
if (gen != null) {
try {
gen.close();
} catch (IOException e) {
throw new ODataSerializerException("An I/O exception occurred.", e, ODataSerializerException.IO_EXCEPTION);
throw new ODataSerializerException("An I/O exception occurred.", e,
ODataSerializerException.MessageKeys.IO_EXCEPTION);
}
}
}
@ -98,7 +100,7 @@ public class ODataJsonSerializer implements ODataSerializer {
@Override
public InputStream metadataDocument(final Edm edm) throws ODataSerializerException {
throw new ODataSerializerException("Metadata in JSON format not supported!",
ODataSerializerException.JSON_METADATA);
ODataSerializerException.MessageKeys.JSON_METADATA);
}
@Override
@ -109,7 +111,8 @@ public class ODataJsonSerializer implements ODataSerializer {
new ODataErrorSerializer().writeErrorDocument(json, error);
json.close();
} catch (final IOException e) {
throw new ODataSerializerException("An I/O exception occurred.", e, ODataSerializerException.IO_EXCEPTION);
throw new ODataSerializerException("An I/O exception occurred.", e,
ODataSerializerException.MessageKeys.IO_EXCEPTION);
}
return buffer.getInputStream();
}
@ -123,7 +126,8 @@ public class ODataJsonSerializer implements ODataSerializer {
json.writeStartObject();
if (format != ODataFormat.JSON_NO_METADATA) {
if (contextURL == null) {
throw new ODataSerializerException("ContextURL null!", ODataSerializerException.NO_CONTEXT_URL);
throw new ODataSerializerException("ContextURL null!",
ODataSerializerException.MessageKeys.NO_CONTEXT_URL);
} else {
json.writeStringField(Constants.JSON_CONTEXT, ContextURLBuilder.create(contextURL).toASCIIString());
}
@ -142,7 +146,8 @@ public class ODataJsonSerializer implements ODataSerializer {
}
json.close();
} catch (final IOException e) {
throw new ODataSerializerException("An I/O exception occurred.", e, ODataSerializerException.IO_EXCEPTION);
throw new ODataSerializerException("An I/O exception occurred.", e,
ODataSerializerException.MessageKeys.IO_EXCEPTION);
}
return buffer.getInputStream();
}
@ -151,7 +156,8 @@ public class ODataJsonSerializer implements ODataSerializer {
public InputStream entity(final EdmEntitySet edmEntitySet, final Entity entity, final ContextURL contextURL)
throws ODataSerializerException {
if (format != ODataFormat.JSON_NO_METADATA && contextURL == null) {
throw new ODataSerializerException("ContextURL null!", ODataSerializerException.NO_CONTEXT_URL);
throw new ODataSerializerException("ContextURL null!",
ODataSerializerException.MessageKeys.NO_CONTEXT_URL);
}
CircleStreamBuffer buffer = new CircleStreamBuffer();
try {
@ -159,7 +165,8 @@ public class ODataJsonSerializer implements ODataSerializer {
writeEntity(edmEntitySet, entity, contextURL, json);
json.close();
} catch (final IOException e) {
throw new ODataSerializerException("An I/O exception occurred.", e, ODataSerializerException.IO_EXCEPTION);
throw new ODataSerializerException("An I/O exception occurred.", e,
ODataSerializerException.MessageKeys.IO_EXCEPTION);
}
return buffer.getInputStream();
}
@ -198,7 +205,7 @@ public class ODataJsonSerializer implements ODataSerializer {
if (property == null || property.isNull()) {
if (edmProperty.isNullable() == Boolean.FALSE) {
throw new ODataSerializerException("Non-nullable property not present!",
ODataSerializerException.MISSING_PROPERTY, edmProperty.getName());
ODataSerializerException.MessageKeys.MISSING_PROPERTY, edmProperty.getName());
} else {
json.writeNull();
}
@ -214,11 +221,12 @@ public class ODataJsonSerializer implements ODataSerializer {
writeComplexValue(edmProperty, property.asComplex(), json);
} else {
throw new ODataSerializerException("Property type not yet supported!",
ODataSerializerException.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
ODataSerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
}
} catch (final EdmPrimitiveTypeException e) {
throw new ODataSerializerException("Wrong value for property!", e,
ODataSerializerException.WRONG_PROPERTY_VALUE, edmProperty.getName(), property.getValue().toString());
ODataSerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
edmProperty.getName(), property.getValue().toString());
}
}
}
@ -233,7 +241,7 @@ public class ODataJsonSerializer implements ODataSerializer {
break;
case COLLECTION_GEOSPATIAL:
throw new ODataSerializerException("Property type not yet supported!",
ODataSerializerException.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
ODataSerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
case COLLECTION_ENUM:
json.writeString(value.toString());
break;
@ -245,7 +253,7 @@ public class ODataJsonSerializer implements ODataSerializer {
break;
default:
throw new ODataSerializerException("Property type not yet supported!",
ODataSerializerException.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
ODataSerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
}
}
json.writeEndArray();
@ -257,12 +265,12 @@ public class ODataJsonSerializer implements ODataSerializer {
writePrimitiveValue(edmProperty, property.asPrimitive(), json);
} else if (property.isGeospatial()) {
throw new ODataSerializerException("Property type not yet supported!",
ODataSerializerException.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
ODataSerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
} else if (property.isEnum()) {
writePrimitiveValue(edmProperty, property.asEnum(), json);
} else {
throw new ODataSerializerException("Inconsistent property type!",
ODataSerializerException.INCONSISTENT_PROPERTY_TYPE, edmProperty.getName());
ODataSerializerException.MessageKeys.INCONSISTENT_PROPERTY_TYPE, edmProperty.getName());
}
}

View File

@ -25,6 +25,7 @@ import org.apache.olingo.commons.api.http.HttpMethod;
import org.apache.olingo.server.api.OData;
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.CollectionProcessor;
import org.apache.olingo.server.api.processor.CustomContentTypeSupportProcessor;
import org.apache.olingo.server.api.processor.FormatContentTypeMapping;
@ -105,28 +106,28 @@ public class ContentNegotiatorTest {
private final static Logger LOG = LoggerFactory.getLogger(ContentNegotiatorTest.class);
@Test
public void testServiceDocumentSingleCase() {
public void testServiceDocumentSingleCase() throws Exception {
String[] useCase = { ACCEPT_CASE_MIN_UTF8, null, ACCEPT_CASE_MIN_UTF8, null, null };
testContentNegotiation(useCase, ServiceDocumentProcessor.class);
}
@Test
public void testServiceDocument() {
public void testServiceDocument() throws Exception {
for (String[] useCase : casesServiceDocument) {
testContentNegotiation(useCase, ServiceDocumentProcessor.class);
}
}
@Test
public void testMetadataSingleCase() {
public void testMetadataSingleCase() throws Exception {
String[] useCase = { "application/xml", null, null, null, null };
testContentNegotiation(useCase, MetadataProcessor.class);
}
@Test
public void testMetadata() {
public void testMetadata() throws Exception {
for (String[] useCase : casesMetadata) {
testContentNegotiation(useCase, MetadataProcessor.class);
}
@ -144,7 +145,8 @@ public class ContentNegotiatorTest {
}
}
public void testContentNegotiation(final String[] useCase, final Class<? extends Processor> processorClass) {
public void testContentNegotiation(final String[] useCase, final Class<? extends Processor> processorClass)
throws ODataTranslatedException {
LOG.debug(Arrays.asList(useCase).toString());

View File

@ -26,12 +26,12 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.olingo.commons.api.ODataRuntimeException;
import org.apache.olingo.commons.api.domain.ODataErrorDetail;
import org.apache.olingo.commons.api.format.ODataFormat;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataServerError;
import org.apache.olingo.server.api.serializer.ODataSerializer;
import org.apache.olingo.server.api.serializer.ODataSerializerException;
import org.junit.Before;
import org.junit.Test;
@ -44,7 +44,7 @@ public class ODataErrorSerializerTest {
ODataSerializer ser;
@Before
public void before() {
public void before() throws Exception {
ser = OData.newInstance().createSerializer(ODataFormat.JSON);
}
@ -75,7 +75,7 @@ public class ODataErrorSerializerTest {
assertEquals("{\"error\":{\"code\":\"Code\",\"message\":\"ErrorMessage\",\"target\":\"Target\"}}", jsonString);
}
@Test(expected = ODataRuntimeException.class)
@Test(expected = ODataSerializerException.class)
public void nullErrorResultsInException() throws Exception {
ser.error(null);
}

View File

@ -30,10 +30,10 @@ import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.server.api.OData;
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.CollectionProcessor;
import org.apache.olingo.server.api.processor.EntityProcessor;
import org.apache.olingo.server.api.serializer.ODataSerializer;
import org.apache.olingo.server.api.serializer.ODataSerializerException;
import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.api.uri.UriInfoResource;
import org.apache.olingo.server.api.uri.UriResource;
@ -66,20 +66,20 @@ public class TechnicalProcessor implements CollectionProcessor, EntityProcessor
response.setStatusCode(HttpStatusCode.NOT_IMPLEMENTED.getStatusCode());
return;
}
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType));
final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
try {
final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
final EntitySet entitySet = readEntitySetInternal(edmEntitySet, request.getRawBaseUri());
if (entitySet == null) {
response.setStatusCode(HttpStatusCode.NOT_FOUND.getStatusCode());
} else {
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType));
response.setContent(serializer.entitySet(edmEntitySet, entitySet, getContextUrl(edmEntitySet)));
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 ODataSerializerException e) {
} catch (final ODataTranslatedException e) {
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
}
}
@ -91,20 +91,20 @@ public class TechnicalProcessor implements CollectionProcessor, EntityProcessor
response.setStatusCode(HttpStatusCode.NOT_IMPLEMENTED.getStatusCode());
return;
}
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType));
final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
try {
final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
final Entity entity = readEntityInternal(uriInfo.asUriInfoResource(), edmEntitySet);
if (entity == null) {
response.setStatusCode(HttpStatusCode.NOT_FOUND.getStatusCode());
} else {
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType));
response.setContent(serializer.entity(edmEntitySet, entity, getContextUrl(edmEntitySet)));
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 ODataSerializerException e) {
} catch (final ODataTranslatedException e) {
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
}
}
@ -136,17 +136,20 @@ public class TechnicalProcessor implements CollectionProcessor, EntityProcessor
&& uriInfo.getTopOption() == null;
}
private EdmEntitySet getEdmEntitySet(final UriInfoResource uriInfo) {
private EdmEntitySet getEdmEntitySet(final UriInfoResource uriInfo) throws ODataTranslatedException {
final List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
if (resourcePaths.size() != 1) {
throw new RuntimeException("Invalid resource path.");
throw new ODataTranslatedException("Invalid resource path.",
ODataTranslatedException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED);
}
if (!(resourcePaths.get(0) instanceof UriResourceEntitySet)) {
throw new RuntimeException("Invalid resource type.");
throw new ODataTranslatedException("Invalid resource type.",
ODataTranslatedException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED);
}
final UriResourceEntitySet uriResource = (UriResourceEntitySet) resourcePaths.get(0);
if (uriResource.getTypeFilterOnCollection() != null || uriResource.getTypeFilterOnEntry() != null) {
throw new RuntimeException("Type filters are not supported.");
throw new ODataTranslatedException("Type filters are not supported.",
ODataTranslatedException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED);
}
return uriResource.getEntitySet();
}

View File

@ -117,7 +117,7 @@ public class ODataJsonSerializerTest {
ContextURL.Builder.create().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build());
Assert.fail("Expected exception not thrown!");
} catch (final ODataSerializerException e) {
Assert.assertEquals(ODataSerializerException.WRONG_PROPERTY_VALUE, e.getMessageKey());
Assert.assertEquals(ODataSerializerException.MessageKeys.WRONG_PROPERTY_VALUE, e.getMessageKey());
final String message = e.getLocalizedMessage();
Assert.assertThat(message, CoreMatchers.containsString("PropertyInt16"));
Assert.assertThat(message, CoreMatchers.containsString("false"));