diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-01-04 17:33:40 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-01-04 17:33:40 +0100 |
commit | 8cce8890eca34fead35ad19a0db6d95dd047b3a6 (patch) | |
tree | 1843c791ab50c96c650ddd70a7ac8ec815dde267 /src/main/java/io/trygvis/esper/testing/web | |
parent | 4a6c9c52d006ecb717bae7d9b502d9b661a08ccd (diff) | |
download | esper-testing-8cce8890eca34fead35ad19a0db6d95dd047b3a6.tar.gz esper-testing-8cce8890eca34fead35ad19a0db6d95dd047b3a6.tar.bz2 esper-testing-8cce8890eca34fead35ad19a0db6d95dd047b3a6.tar.xz esper-testing-8cce8890eca34fead35ad19a0db6d95dd047b3a6.zip |
o Trying out a custom UUID type for prettier formatting.
Diffstat (limited to 'src/main/java/io/trygvis/esper/testing/web')
4 files changed, 77 insertions, 6 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java b/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java index a02bb5c..8f66548 100755 --- a/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java +++ b/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java @@ -34,7 +34,7 @@ public class JerseyApplication extends Application { } public Set<Class<?>> getClasses() { - return new HashSet<>(Arrays.<Class<?>>asList(ResourceParamInjector.class)); + return new HashSet<>(Arrays.<Class<?>>asList(ResourceParamInjector.class, MyObjectMapper.class)); } public Set<Object> getSingletons() { @@ -97,6 +97,38 @@ public class JerseyApplication extends Application { } } }; + } else if (Uuid.class.equals(type)) { + + return new AbstractHttpContextInjectable() { + public Object getValue(HttpContext hc) { + + if (a.query().length() > 0) { + return parse(hc.getRequest().getQueryParameters().getFirst(a.query())); + } else { + MultivaluedMap<String, String> pathParameters = hc.getUriInfo().getPathParameters(); + + for (Map.Entry<String, List<String>> entry : pathParameters.entrySet()) { + if ("uuid".equals(entry.getKey())) { + return parse(entry.getValue().get(0)); + } + } + } + + throw new RuntimeException("@MagicParam used with Uuid argument with no {uuid} path variable."); + } + + private Uuid parse(String s) { + if(s == null) { + return null; + } + + try { + return Uuid.fromString(s); + } catch (IllegalArgumentException e) { + throw new WebApplicationException(400); + } + } + }; } return null; diff --git a/src/main/java/io/trygvis/esper/testing/web/MyObjectMapper.java b/src/main/java/io/trygvis/esper/testing/web/MyObjectMapper.java new file mode 100755 index 0000000..bfbd6eb --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/web/MyObjectMapper.java @@ -0,0 +1,37 @@ +package io.trygvis.esper.testing.web; + +import io.trygvis.esper.testing.*; +import org.codehaus.jackson.*; +import org.codehaus.jackson.map.*; +import org.codehaus.jackson.map.module.*; + +import java.io.*; +import javax.ws.rs.ext.*; + +public class MyObjectMapper implements ContextResolver<ObjectMapper> { + private ObjectMapper objectMapper; + + public MyObjectMapper() throws Exception { + objectMapper = new ObjectMapper(); + SimpleModule module = new SimpleModule("wat", Version.unknownVersion()); + module.addDeserializer(Uuid.class, new UuidDeserializer()); + module.addSerializer(Uuid.class, new UuidSerializer()); + objectMapper.registerModule(module); + } + + public ObjectMapper getContext(Class<?> type) { + return objectMapper; + } + + private static class UuidDeserializer extends JsonDeserializer<Uuid> { + public Uuid deserialize(JsonParser jp, DeserializationContext context) throws IOException { + return Uuid.fromString(jp.getText()); + } + } + + private static class UuidSerializer extends JsonSerializer<Uuid> { + public void serialize(Uuid value, JsonGenerator generator, SerializerProvider provider) throws IOException { + generator.writeString(value.toStringBase64()); + } + } +} diff --git a/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java b/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java index f006261..5849647 100755 --- a/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java +++ b/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java @@ -50,9 +50,9 @@ public class CoreResource extends AbstractResource { @GET @Path("/person/{uuid}") - public PersonJson getPerson(@PathParam("uuid") final String s) throws Exception { - final UUID uuid = JenkinsResource.parseUuid(s); - + public PersonJson getPerson(@PathParam("uuid") final Uuid uuid) throws Exception { + System.out.println("uuid.toStringBase64() = " + uuid.toStringBase64()); + System.out.println("uuid.toUuidString() = " + uuid.toUuidString()); return get(new DatabaseAccess.DaosCallback<Option<PersonJson>>() { public Option<PersonJson> run(Daos daos) throws SQLException { SqlOption<PersonDto> o = daos.personDao.selectPerson(uuid); diff --git a/src/main/java/io/trygvis/esper/testing/web/resource/PersonJson.java b/src/main/java/io/trygvis/esper/testing/web/resource/PersonJson.java index e8008e3..e26b120 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/web/resource/PersonJson.java +++ b/src/main/java/io/trygvis/esper/testing/web/resource/PersonJson.java @@ -1,14 +1,16 @@ package io.trygvis.esper.testing.web.resource; +import io.trygvis.esper.testing.*; + import java.util.*; public class PersonJson { - public final UUID uuid; + public final Uuid uuid; public final String name; public final List<BadgeJson> badges; public final List<BadgeJson> badgesInProgress; - public PersonJson(UUID uuid, String name, List<BadgeJson> badges, List<BadgeJson> badgesInProgress) { + public PersonJson(Uuid uuid, String name, List<BadgeJson> badges, List<BadgeJson> badgesInProgress) { this.uuid = uuid; this.name = name; this.badges = badges; |