diff --git a/pom.xml b/pom.xml index 257fbc26d..2659e3b9a 100644 --- a/pom.xml +++ b/pom.xml @@ -30,8 +30,8 @@ - 2.7 - 2.5.1 + 2.11 + @@ -44,16 +44,16 @@ import - - org.eclipse.persistence - org.eclipse.persistence.moxy - ${org.eclipse.persistence.version} - - - org.eclipse.persistence - org.eclipse.persistence.antlr - ${org.eclipse.persistence.version} - + + + + + + + + + + diff --git a/src/main/java/li/strolch/rest/endpoint/AuthenticationService.java b/src/main/java/li/strolch/rest/endpoint/AuthenticationService.java index 3f3594ea0..59334464c 100644 --- a/src/main/java/li/strolch/rest/endpoint/AuthenticationService.java +++ b/src/main/java/li/strolch/rest/endpoint/AuthenticationService.java @@ -15,6 +15,8 @@ */ package li.strolch.rest.endpoint; +import java.util.Locale; + import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -24,6 +26,7 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.GenericEntity; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; @@ -31,6 +34,7 @@ import javax.ws.rs.core.Response.Status; import li.strolch.exception.StrolchException; import li.strolch.rest.RestfulStrolchComponent; import li.strolch.rest.StrolchSessionHandler; +import li.strolch.rest.helper.RestfulHelper; import li.strolch.rest.model.Login; import li.strolch.rest.model.LoginResult; import li.strolch.rest.model.LogoutResult; @@ -50,13 +54,10 @@ public class AuthenticationService { private static final Logger logger = LoggerFactory.getLogger(AuthenticationService.class); - @Context - HttpServletRequest request; - @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public Response login(Login login) { + public Response login(Login login, @Context HttpServletRequest request, @Context HttpHeaders headers) { LoginResult loginResult = new LoginResult(); GenericEntity entity = new GenericEntity(loginResult, LoginResult.class) { @@ -83,9 +84,12 @@ public class AuthenticationService { Certificate certificate = sessionHandler.authenticate(origin, login.getUsername(), login.getPassword() .getBytes()); + Locale locale = RestfulHelper.getLocale(headers); + certificate.setLocale(locale); + loginResult.setSessionId(certificate.getAuthToken()); loginResult.setUsername(certificate.getUsername()); - loginResult.setLocale(certificate.getLocale().getLanguage() + "_" + certificate.getLocale().getCountry()); + loginResult.setLocale(locale.toString()); loginResult.setParameters(certificate.getPropertyMap()); return Response.ok().entity(entity).build(); @@ -106,7 +110,7 @@ public class AuthenticationService { @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("{authToken}") - public Response logout(@PathParam("authToken") String authToken) { + public Response logout(@PathParam("authToken") String authToken, @Context HttpServletRequest request) { LogoutResult logoutResult = new LogoutResult(); diff --git a/src/main/java/li/strolch/rest/endpoint/EnumQuery.java b/src/main/java/li/strolch/rest/endpoint/EnumQuery.java index 78de753fa..4264f5c28 100644 --- a/src/main/java/li/strolch/rest/endpoint/EnumQuery.java +++ b/src/main/java/li/strolch/rest/endpoint/EnumQuery.java @@ -21,11 +21,14 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; import javax.ws.rs.core.GenericEntity; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import li.strolch.rest.RestfulStrolchComponent; +import li.strolch.rest.helper.RestfulHelper; import li.strolch.runtime.query.enums.EnumHandler; import li.strolch.runtime.query.enums.StrolchEnum; @@ -43,12 +46,14 @@ public class EnumQuery { @GET @Produces(MediaType.APPLICATION_JSON) @Path("{name}") - public Response getEnum(@PathParam("name") String name) { + public Response getEnum(@PathParam("name") String name, @Context HttpHeaders headers) { try { EnumHandler enumHandler = RestfulStrolchComponent.getInstance().getContainer() .getComponent(EnumHandler.class); - StrolchEnum strolchEnum = enumHandler.getEnum(name, Locale.getDefault()); + + Locale locale = RestfulHelper.getLocale(headers); + StrolchEnum strolchEnum = enumHandler.getEnum(name, locale); GenericEntity entity = new GenericEntity(strolchEnum, StrolchEnum.class) { }; diff --git a/src/main/java/li/strolch/rest/helper/RestfulHelper.java b/src/main/java/li/strolch/rest/helper/RestfulHelper.java new file mode 100644 index 000000000..bd0393b12 --- /dev/null +++ b/src/main/java/li/strolch/rest/helper/RestfulHelper.java @@ -0,0 +1,38 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed 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 li.strolch.rest.helper; + +import java.util.Locale; + +import javax.ws.rs.core.HttpHeaders; + +import ch.eitchnet.utils.helper.StringHelper; + +/** + * @author Robert von Burg + */ +public class RestfulHelper { + + public static Locale getLocale(HttpHeaders headers) { + Locale locale; + if (headers == null || StringHelper.isEmpty(headers.getHeaderString(HttpHeaders.ACCEPT_LANGUAGE))) + locale = Locale.getDefault(); + else + locale = headers.getAcceptableLanguages().get(0); + + return locale; + } +} diff --git a/src/test/java/li/strolch/rest/inspector/test/AbstractRestfulTest.java b/src/test/java/li/strolch/rest/inspector/test/AbstractRestfulTest.java index 327c7b0a3..907fd2ec1 100644 --- a/src/test/java/li/strolch/rest/inspector/test/AbstractRestfulTest.java +++ b/src/test/java/li/strolch/rest/inspector/test/AbstractRestfulTest.java @@ -16,22 +16,22 @@ package li.strolch.rest.inspector.test; import java.io.File; +import java.io.IOException; import java.net.URI; -import java.util.HashMap; -import java.util.Map; import javax.ws.rs.core.Application; -import javax.ws.rs.ext.ContextResolver; +import li.strolch.rest.endpoint.Inspector; import li.strolch.testbase.runtime.RuntimeMock; import org.glassfish.grizzly.http.server.HttpServer; import org.glassfish.jersey.client.ClientConfig; -import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; -import org.glassfish.jersey.moxy.json.MoxyJsonConfig; +import org.glassfish.jersey.filter.LoggingFilter; +import org.glassfish.jersey.grizzly2.servlet.GrizzlyWebContainerFactory; import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.server.ServerProperties; +import org.glassfish.jersey.server.TracingConfig; import org.glassfish.jersey.test.JerseyTest; -import org.glassfish.jersey.test.TestProperties; import org.junit.AfterClass; import org.junit.BeforeClass; import org.slf4j.Logger; @@ -50,7 +50,7 @@ public abstract class AbstractRestfulTest extends JerseyTest { private static HttpServer server; @BeforeClass - public static void beforeClass() { + public static void beforeClass() throws IllegalArgumentException, IOException { File rootPath = new File(RUNTIME_PATH); File configSrc = new File(CONFIG_SRC); @@ -58,7 +58,7 @@ public abstract class AbstractRestfulTest extends JerseyTest { runtimeMock.mockRuntime(rootPath, configSrc); runtimeMock.startContainer(); - server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, createApp()); + server = GrizzlyWebContainerFactory.create(BASE_URI); } @AfterClass @@ -69,26 +69,32 @@ public abstract class AbstractRestfulTest extends JerseyTest { @Override protected Application configure() { - enable(TestProperties.LOG_TRAFFIC); - enable(TestProperties.DUMP_ENTITY); +// enable(TestProperties.LOG_TRAFFIC); +// enable(TestProperties.DUMP_ENTITY); return createApp(); } @Override protected void configureClient(ClientConfig config) { - config.register(createMoxyJsonResolver()); + //config.register(createMoxyJsonResolver()); } public static ResourceConfig createApp() { - return new ResourceConfig().packages("li.strolch.rest.endpoint").register(createMoxyJsonResolver()); + return new ResourceConfig()// + .packages(Inspector.class.getPackage().getName())// + //.register(createMoxyJsonResolver()) + // Logging. + .register(LoggingFilter.class) + // Tracing support. + .property(ServerProperties.TRACING, TracingConfig.ON_DEMAND.name()); } - public static ContextResolver createMoxyJsonResolver() { - final MoxyJsonConfig moxyJsonConfig = new MoxyJsonConfig(); - Map namespacePrefixMapper = new HashMap(1); - namespacePrefixMapper.put("http://www.w3.org/2001/XMLSchema-instance", "xsi"); - moxyJsonConfig.setNamespacePrefixMapper(namespacePrefixMapper).setNamespaceSeparator(':'); - return moxyJsonConfig.resolver(); - } +// public static ContextResolver createMoxyJsonResolver() { +// final MoxyJsonConfig moxyJsonConfig = new MoxyJsonConfig(); +// Map namespacePrefixMapper = new HashMap(1); +// namespacePrefixMapper.put("http://www.w3.org/2001/XMLSchema-instance", "xsi"); +// moxyJsonConfig.setNamespacePrefixMapper(namespacePrefixMapper).setNamespaceSeparator(':'); +// return moxyJsonConfig.resolver(); +// } } diff --git a/src/test/java/li/strolch/rest/inspector/test/AuthenticationTest.java b/src/test/java/li/strolch/rest/inspector/test/AuthenticationTest.java index 327772550..1404a6998 100644 --- a/src/test/java/li/strolch/rest/inspector/test/AuthenticationTest.java +++ b/src/test/java/li/strolch/rest/inspector/test/AuthenticationTest.java @@ -21,7 +21,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import java.util.Locale; + import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; @@ -68,6 +71,35 @@ public class AuthenticationTest extends AbstractRestfulTest { assertNull(logoutResult.getMsg()); } + @Test + public void shouldUseRequestedLanguage() { + + // login + Login login = new Login(); + login.setUsername("jill"); + login.setPassword("jill"); + Entity loginEntity = Entity.entity(login, MediaType.APPLICATION_JSON); + Builder builder = target().path(ROOT_PATH).request(MediaType.APPLICATION_JSON); + builder = builder.acceptLanguage(Locale.ITALY); + Response result = builder.post(loginEntity); + assertEquals(Status.OK.getStatusCode(), result.getStatus()); + LoginResult loginResult = result.readEntity(LoginResult.class); + assertNotNull(loginResult); + assertEquals("jill", loginResult.getUsername()); + assertEquals(64, loginResult.getSessionId().length()); + assertEquals(Locale.ITALY.toString(), loginResult.getLocale()); + assertNull(loginResult.getMsg()); + + // logout + result = target().path(ROOT_PATH + "/" + loginResult.getSessionId()).request(MediaType.APPLICATION_JSON) + .delete(); + assertEquals(Status.OK.getStatusCode(), result.getStatus()); + assertNotNull(loginResult); + LogoutResult logoutResult = result.readEntity(LogoutResult.class); + assertNotNull(logoutResult); + assertNull(logoutResult.getMsg()); + } + @Test public void shouldNotAuthenticate() { diff --git a/src/test/java/li/strolch/rest/inspector/test/EnumTest.java b/src/test/java/li/strolch/rest/inspector/test/EnumTest.java index 55d18f8f9..ee162fd27 100644 --- a/src/test/java/li/strolch/rest/inspector/test/EnumTest.java +++ b/src/test/java/li/strolch/rest/inspector/test/EnumTest.java @@ -18,6 +18,8 @@ package li.strolch.rest.inspector.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import java.util.Locale; + import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; @@ -55,5 +57,20 @@ public class EnumTest extends AbstractRestfulTest { assertNotNull(strolchEnum); assertEquals("salutation", strolchEnum.getName()); assertEquals(3, strolchEnum.getValues().size()); + assertEquals("Mrs", strolchEnum.getValue("mrs")); + } + + @Test + public void shouldQueryGermanSalutation() { + + // query + Response result = target().path(ROOT_PATH + "/salutation").request(MediaType.APPLICATION_JSON) + .acceptLanguage(Locale.GERMAN).get(); + assertEquals(Status.OK.getStatusCode(), result.getStatus()); + StrolchEnum strolchEnum = result.readEntity(StrolchEnum.class); + assertNotNull(strolchEnum); + assertEquals("salutation", strolchEnum.getName()); + assertEquals(3, strolchEnum.getValues().size()); + assertEquals("Frau", strolchEnum.getValue("mrs")); } } diff --git a/src/test/resources/withPrivilegeRuntime/data/Enums.xml b/src/test/resources/withPrivilegeRuntime/data/Enums.xml index 7340252a4..8e7afdc99 100644 --- a/src/test/resources/withPrivilegeRuntime/data/Enums.xml +++ b/src/test/resources/withPrivilegeRuntime/data/Enums.xml @@ -7,6 +7,11 @@ + + + + +