[OLINGO-1203] Locale information is not set to error context
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java
index a942693..11aff62 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java
@@ -148,6 +148,7 @@
private void enhanceContextWithApplicationException(final ODataApplicationException toHandleException) {
errorContext.setHttpStatus(toHandleException.getHttpStatus());
errorContext.setErrorCode(toHandleException.getCode());
+ errorContext.setLocale(messageLocale);
}
private void enhanceContextWithMessageException(final ODataMessageException toHandleException) {
diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ODataExceptionWrapperTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ODataExceptionWrapperTest.java
index 98c6b26..4af0449 100644
--- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ODataExceptionWrapperTest.java
+++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ODataExceptionWrapperTest.java
@@ -24,12 +24,16 @@
import java.net.URI;
import java.net.URISyntaxException;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
+import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.UriInfo;
import org.apache.olingo.odata2.api.ODataCallback;
import org.apache.olingo.odata2.api.ODataService;
@@ -85,7 +89,83 @@
String contentTypeHeader = response.getContentHeader();
assertEquals("text/html", contentTypeHeader);
}
+
+ @Test
+ public void testCallbackWithLocales() throws Exception {
+ ODataContextImpl context = getMockedContextWithLocale("http://localhost:80/test", "ODataServiceRoot");
+ ODataErrorCallback errorCallback = new ODataErrorCallback() {
+ @Override
+ public ODataResponse handleError(final ODataErrorContext context) throws ODataApplicationException {
+ PathInfo pathInfo = context.getPathInfo();
+ assertEquals("ODataServiceRoot", pathInfo.getServiceRoot().toString());
+ assertEquals("http://localhost:80/test", pathInfo.getRequestUri().toString());
+ assertEquals("de", context.getLocale().getLanguage());
+ assertEquals("DE", context.getLocale().getCountry());
+ return ODataResponse.entity("bla").status(HttpStatusCodes.BAD_REQUEST).contentHeader("text/html").build();
+ }
+ };
+ when(context.getServiceFactory()).thenReturn(new MapperServiceFactory(errorCallback));
+ //
+ Map<String, String> queryParameters = Collections.emptyMap();
+ List<String> acceptContentTypes = Arrays.asList("text/html");
+ ODataExceptionWrapper exceptionWrapper = createWrapper(context, queryParameters, acceptContentTypes);
+ ODataResponse response = exceptionWrapper.wrapInExceptionResponse(
+ new ODataApplicationException("Error",Locale.GERMANY));
+
+ // verify
+ assertNotNull(response);
+ assertEquals(HttpStatusCodes.BAD_REQUEST.getStatusCode(), response.getStatus().getStatusCode());
+ String errorMessage = (String) response.getEntity();
+ assertEquals("bla", errorMessage);
+ String contentTypeHeader = response.getContentHeader();
+ assertEquals("text/html", contentTypeHeader);
+ }
+
+ @Test
+ public void testCallbackWithLocales1() throws Exception {
+ UriInfo uriInfo = getMockedUriInfo();
+ HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
+ List<Locale> locales = new ArrayList<Locale>();
+ locales.add(Locale.GERMANY);
+ locales.add(Locale.FRANCE);
+ when(httpHeaders.getAcceptableLanguages()).thenReturn(locales);
+
+ ODataErrorCallback errorCallback = new ODataErrorCallback() {
+ @Override
+ public ODataResponse handleError(final ODataErrorContext context) throws ODataApplicationException {
+ assertEquals("de", context.getLocale().getLanguage());
+ assertEquals("DE", context.getLocale().getCountry());
+ return ODataResponse.entity("bla").status(HttpStatusCodes.BAD_REQUEST).contentHeader("text/html").build();
+ }
+ };
+
+ ODataExceptionWrapper exceptionWrapper = createWrapper1(uriInfo, httpHeaders, errorCallback);
+ ODataResponse response = exceptionWrapper.wrapInExceptionResponse(
+ new ODataApplicationException("Error",Locale.GERMANY));
+
+ // verify
+ assertNotNull(response);
+ assertEquals(HttpStatusCodes.BAD_REQUEST.getStatusCode(), response.getStatus().getStatusCode());
+ String errorMessage = (String) response.getEntity();
+ assertEquals("bla", errorMessage);
+ String contentTypeHeader = response.getContentHeader();
+ assertEquals("text/html", contentTypeHeader);
+ }
+
+ private UriInfo getMockedUriInfo() {
+ UriInfo uriInfo = Mockito.mock(UriInfo.class);
+ when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost:80/test"));
+ return uriInfo;
+ }
+
+ private ODataExceptionWrapper createWrapper1(final UriInfo uriInfo,
+ final HttpHeaders httpHeaders, ODataErrorCallback errorCallback) throws URISyntaxException {
+ ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(uriInfo, httpHeaders, errorCallback);
+
+ return exceptionWrapper;
+ }
+
private ODataExceptionWrapper createWrapper(final ODataContextImpl context,
final Map<String, String> queryParameters, final List<String> acceptContentTypes) throws URISyntaxException {
ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(context, queryParameters, acceptContentTypes);
@@ -103,6 +183,23 @@
when(context.getRequestHeaders()).thenReturn(new MultivaluedHashMap<String, String>());
return context;
}
+
+ private ODataContextImpl getMockedContextWithLocale(final String requestUri,
+ final String serviceRoot) throws ODataException,
+ URISyntaxException {
+ ODataContextImpl context = Mockito.mock(ODataContextImpl.class);
+ PathInfoImpl pathInfo = new PathInfoImpl();
+ pathInfo.setRequestUri(new URI(requestUri));
+ pathInfo.setServiceRoot(new URI(serviceRoot));
+ when(context.getPathInfo()).thenReturn(pathInfo);
+ MultivaluedHashMap<String,String> headers = new MultivaluedHashMap<String, String>();
+ headers.add("Accept-Language","de-DE, de;q=0.7");
+ when(context.getRequestHeaders()).thenReturn(headers);
+ List<Locale> locales = new ArrayList<Locale>();
+ locales.add(Locale.GERMANY);
+ when(context.getAcceptableLanguages()).thenReturn(locales);
+ return context;
+ }
public static final class MapperServiceFactory extends ODataServiceFactory {
private ODataErrorCallback errorCallback;