From: <id...@us...> - 2009-04-22 18:56:38
|
Revision: 161 http://cse-ip.svn.sourceforge.net/cse-ip/?rev=161&view=rev Author: idueppe Date: 2009-04-22 18:56:27 +0000 (Wed, 22 Apr 2009) Log Message: ----------- - refactor mapper-rest to support PUT and GET methods. Modified Paths: -------------- trunk/cse-ip/sc-mapper-rest/pom.xml trunk/cse-ip/sc-mapper-rest/src/main/java/de/campussource/cse/mapper/rest/IdentityMapperRestBean.java trunk/cse-ip/sc-mapper-rest/src/main/java/de/campussource/cse/mapper/rest/MappedObject.java trunk/cse-ip/sc-mapper-rest/src/test/java/de/campussource/cse/mapper/rest/RestIdentityMapperIntegrationTest.java Modified: trunk/cse-ip/sc-mapper-rest/pom.xml =================================================================== --- trunk/cse-ip/sc-mapper-rest/pom.xml 2009-04-22 17:56:12 UTC (rev 160) +++ trunk/cse-ip/sc-mapper-rest/pom.xml 2009-04-22 18:56:27 UTC (rev 161) @@ -43,6 +43,11 @@ <artifactId>junit</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + </dependency> + </dependencies> </project> \ No newline at end of file Modified: trunk/cse-ip/sc-mapper-rest/src/main/java/de/campussource/cse/mapper/rest/IdentityMapperRestBean.java =================================================================== --- trunk/cse-ip/sc-mapper-rest/src/main/java/de/campussource/cse/mapper/rest/IdentityMapperRestBean.java 2009-04-22 17:56:12 UTC (rev 160) +++ trunk/cse-ip/sc-mapper-rest/src/main/java/de/campussource/cse/mapper/rest/IdentityMapperRestBean.java 2009-04-22 18:56:27 UTC (rev 161) @@ -4,18 +4,25 @@ import java.util.Map; import javax.ws.rs.GET; +import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; -import de.campussource.cse.common.exception.IdNotFoundException; +import org.apache.commons.lang.StringUtils; -@Path("/mapper") +import de.campussource.cse.mapper.IdentityMapper; + +@Path("/mapped") public class IdentityMapperRestBean { - - private Map<Long, String> busToClient = new HashMap<Long, String>(); - private Map<String, Long> clientToBus = new HashMap<String, Long>(); - + + private IdentityMapper mapper; + + private static Map<Long, String> busToClient = new HashMap<Long, String>(); + private static Map<String, Long> clientToBus = new HashMap<String, Long>(); + private Long toBus(String client) { Long bus = clientToBus.get(client); if (bus == null) { @@ -23,47 +30,50 @@ busToClient.put(bus, client); clientToBus.put(client, bus); } - + return bus; } private String toClient(Long bus) { return busToClient.get(bus); } - - @GET - @Path("/client/{instanceid}/{objectid}") + + @PUT + @Path("/{instanceid}/{objectid}") @Produces("application/xml") - public MappedObject toBusId(@PathParam("instanceid") Long clientInstanceId, + public MappedObject putMapping(@PathParam("instanceid") Long clientInstanceId, @PathParam("objectid") String clientObjectId) { + + if (clientInstanceId == null || StringUtils.isEmpty(clientObjectId)) { + throw new WebApplicationException(Response.Status.NOT_ACCEPTABLE); + } MappedObject mapped = new MappedObject(); mapped.setBusId(toBus(clientObjectId)); -// mapped.setBusId(mapper.toBusId(clientInstanceId, clientObjectId)); + // mapped.setBusId(mapper.toBusId(clientInstanceId, clientObjectId)); mapped.setClientInstanceId(clientInstanceId); mapped.setClientObjectId(clientObjectId); return mapped; } - + @GET - @Path("/bus/{busid}/{instanceid}") + @Path("/{busid}/{instanceid}") @Produces("application/xml") - public MappedObject toClientObjectId( - @PathParam("busid") Long busId, - @PathParam("instanceid") Long clientInstanceId) throws IdNotFoundException { + public MappedObject toClientObjectId(@PathParam("busid") Long busId, @PathParam("instanceid") Long clientInstanceId) { MappedObject mapped = new MappedObject(); mapped.setBusId(busId); mapped.setClientInstanceId(clientInstanceId); mapped.setClientObjectId(toClient(busId)); -// mapped.setClientObjectId(mapper.toClientObjectId(busId, clientInstanceId)); - + // mapped.setClientObjectId(mapper.toClientObjectId(busId, + // clientInstanceId)); + return mapped; } + + public void setMapper(IdentityMapper identityMapper) { + mapper = identityMapper; + } -// public static void setMapper(IdentityMapper identityMapper) { -// mapper = identityMapper; -// } - } Modified: trunk/cse-ip/sc-mapper-rest/src/main/java/de/campussource/cse/mapper/rest/MappedObject.java =================================================================== --- trunk/cse-ip/sc-mapper-rest/src/main/java/de/campussource/cse/mapper/rest/MappedObject.java 2009-04-22 17:56:12 UTC (rev 160) +++ trunk/cse-ip/sc-mapper-rest/src/main/java/de/campussource/cse/mapper/rest/MappedObject.java 2009-04-22 18:56:27 UTC (rev 161) @@ -6,7 +6,10 @@ import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; + @XmlRootElement(name="Mapped") @XmlType(name="MappedObjectType", namespace="http://cse.campussource.de/mapper") @XmlAccessorType(XmlAccessType.FIELD) @@ -45,4 +48,48 @@ this.clientObjectId = clientObjectId; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((busId == null) ? 0 : busId.hashCode()); + result = prime * result + ((clientInstanceId == null) ? 0 : clientInstanceId.hashCode()); + result = prime * result + ((clientObjectId == null) ? 0 : clientObjectId.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + MappedObject other = (MappedObject) obj; + if (busId == null) { + if (other.busId != null) + return false; + } else if (!busId.equals(other.busId)) + return false; + if (clientInstanceId == null) { + if (other.clientInstanceId != null) + return false; + } else if (!clientInstanceId.equals(other.clientInstanceId)) + return false; + if (clientObjectId == null) { + if (other.clientObjectId != null) + return false; + } else if (!clientObjectId.equals(other.clientObjectId)) + return false; + return true; + } + + @Override + public String toString() { + return new ToStringBuilder(this,ToStringStyle.DEFAULT_STYLE).append(busId).append(clientInstanceId).append(clientObjectId).toString(); + } + + + } Modified: trunk/cse-ip/sc-mapper-rest/src/test/java/de/campussource/cse/mapper/rest/RestIdentityMapperIntegrationTest.java =================================================================== --- trunk/cse-ip/sc-mapper-rest/src/test/java/de/campussource/cse/mapper/rest/RestIdentityMapperIntegrationTest.java 2009-04-22 17:56:12 UTC (rev 160) +++ trunk/cse-ip/sc-mapper-rest/src/test/java/de/campussource/cse/mapper/rest/RestIdentityMapperIntegrationTest.java 2009-04-22 18:56:27 UTC (rev 161) @@ -1,15 +1,19 @@ package de.campussource.cse.mapper.rest; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.StringReader; import java.net.HttpURLConnection; import java.net.URL; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Unmarshaller; + import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -19,7 +23,7 @@ public class RestIdentityMapperIntegrationTest{ - private static final String LOCALHOST = "http://localhost:8888/"; + private static final String LOCALHOST = "http://localhost:8000/"; private static HttpServer server; @BeforeClass @@ -44,11 +48,15 @@ // JOptionPane.showMessageDialog(null, "JAX-RS Server is running!"); // } - private static String testResourceAtUrl(URL url) throws Exception { + private static MappedObject sendHttpRequestToUrl(URL url) throws Exception { + return sendHttpRequestToUrl(url,"GET"); + } + + private static MappedObject sendHttpRequestToUrl(URL url, String method) throws Exception { try { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("GET"); + connection.setRequestMethod(method); connection.connect(); InputStream is = connection.getInputStream(); @@ -63,7 +71,12 @@ connection.disconnect(); - return buffer.toString(); + + JAXBContext context = JAXBContext.newInstance(MappedObject.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + MappedObject mapped = (MappedObject) unmarshaller.unmarshal(new StringReader(buffer.toString())); + + return mapped; } catch (IOException e) { e.printStackTrace(); @@ -72,12 +85,17 @@ } @Test - public void testRest() throws Exception { - String activationText = testResourceAtUrl(new URL(LOCALHOST + "mapper/client/12345/objectid")); - assertNotNull(activationText); - assertFalse(activationText.isEmpty()); + public void testMapper() throws Exception { + MappedObject mapped = sendHttpRequestToUrl(new URL(LOCALHOST + "mapped/12345/objectid"), "PUT"); + assertNotNull(mapped); + assertNotNull(mapped.getBusId()); + assertEquals(Long.valueOf(12345L),mapped.getClientInstanceId()); + assertEquals("objectid", mapped.getClientObjectId()); + + MappedObject loaded = sendHttpRequestToUrl(new URL(LOCALHOST + "mapped/"+mapped.getBusId()+"/12345")); + assertEquals(mapped,loaded); } - + @AfterClass public static void tearDown() throws IOException { if (server != null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |