Skip to content

Commit

Permalink
ISPN-714 - REST server shouldn't attempt to deserialize application/x…
Browse files Browse the repository at this point in the history
…-java-serialized-object mime type
  • Loading branch information
maniksurtani committed Oct 20, 2010
1 parent f1fdc21 commit 3e791c2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
12 changes: 9 additions & 3 deletions server/rest/src/main/scala/org/infinispan/rest/Server.scala
Expand Up @@ -42,6 +42,8 @@ class Server(@Context request: Request, @HeaderParam("performAsync") useAsync: B
case MediaType.APPLICATION_XML => Response.ok.`type`(selectedMediaType).entity(streamIt(xstream.toXML(obj, _))).build
case _ =>
obj match {
case ba: Array[Byte] =>
Response.ok.`type`("application/x-java-serialized-object").entity(streamIt(_.write(ba))).build
case ser: Serializable =>
Response.ok.`type`("application/x-java-serialized-object").entity(streamIt(new ObjectOutputStream(_).writeObject(ser))).build
case _ => Response.notAcceptable(variantList).build
Expand Down Expand Up @@ -90,9 +92,13 @@ class Server(@Context request: Request, @HeaderParam("performAsync") useAsync: B
if (request.getMethod == "POST" && cache.containsKey(key)) {
Response.status(Status.CONFLICT).build()
} else {
val obj = if (mediaType == "application/x-java-serialized-object")
new ObjectInputStream(new ByteArrayInputStream(data)).readObject
else new MIMECacheEntry(mediaType, data)
val obj = if (mediaType == "application/x-java-serialized-object") {
try {
new ObjectInputStream(new ByteArrayInputStream(data)).readObject
} catch {
case e: Exception => data
}
} else new MIMECacheEntry(mediaType, data)
(ttl, idleTime, useAsync) match {
case (0, 0, false) => cache.put(key, obj)
case (x, 0, false) => cache.put(key, obj, ttl, TimeUnit.SECONDS)
Expand Down
Expand Up @@ -304,6 +304,29 @@ class IntegrationTest {
Client call get
assertEquals(HttpServletResponse.SC_NOT_FOUND, get.getStatusCode)
}

def testSerializedObjects(m: Method) = {
// assume this has been serialized on the client. So it is a byte array.
val serializedOnClient: Array[Byte] = Array(0x65, 0x66, 0x67)
val fullPathKey = fullPath + "/" + m.getName
val put = new PutMethod(fullPathKey)
put.setRequestHeader("Content-Type", "application/x-java-serialized-object")
put.setRequestBody(new ByteArrayInputStream(serializedOnClient))
Client call put
assertEquals(HttpServletResponse.SC_OK, put.getStatusCode)

val get = new GetMethod(fullPathKey)
get.setRequestHeader("Accept", "application/x-java-serialized-object")
Client call get
assertEquals(HttpServletResponse.SC_OK, get.getStatusCode)

// lets assert that the byte array received is the same as what we put in
val dataRead = new BufferedInputStream(get.getResponseBodyAsStream)
val bytesRead = new Array[Byte](3)
dataRead.read(bytesRead)

assertEquals(serializedOnClient, bytesRead)
}
}


Expand Down

0 comments on commit 3e791c2

Please sign in to comment.