dev@jersey.java.net

[PATCH] Update to Grizzly 1.7.2

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Mon, 18 Feb 2008 11:45:04 -0500

Hi,

attached is a patch that update the current implementation to use the
new Grizzly API (GrizzlyRequest/GrizzlyResponse) and remove customized
Input/OutputStream. Grizzly 1.7.2 will be released this Wednesday, so
you might want to wait for the 1.7.2 release. BTW I'm seeing the
following exception on trunk (without any changes from me):

> [junit] ------------- Standard Output ---------------
> [junit] Starting GrizzlyServer port number = 9997
> [junit] Started GrizzlyServer
> [junit] Starting GrizzlyServer port number = 9997
> [junit] Started GrizzlyServer
> [junit] ------------- ---------------- ---------------
> [junit] Testcase: testContdSlashesProtection(com.sun.ws.rest.impl.container.grizzly.CanonicalizationFeatureTest): Caused an ERROR
> [junit] null
> [junit] com.sun.ws.rest.api.client.UniformInterfaceException
> [junit] at com.sun.ws.rest.api.client.ResourceProxy.handle(ResourceProxy.java:339)
> [junit] at com.sun.ws.rest.api.client.ResourceProxy.access$100(ResourceProxy.java:39)
> [junit] at com.sun.ws.rest.api.client.ResourceProxy$Builder.get(ResourceProxy.java:263)
> [junit] at com.sun.ws.rest.impl.container.grizzly.CanonicalizationFeatureTest.testContdSlashesProtection(CanonicalizationFeatureTest.java:107)

Now time to work on Comet :-)

A+

-- Jeanfrancois


Index: src/impl/com/sun/ws/rest/impl/container/grizzly/GrizzlyResponseAdaptor.java
===================================================================
--- src/impl/com/sun/ws/rest/impl/container/grizzly/GrizzlyResponseAdaptor.java (revision 777)
+++ src/impl/com/sun/ws/rest/impl/container/grizzly/GrizzlyResponseAdaptor.java (working copy)
@@ -27,10 +27,7 @@
 import java.io.OutputStream;
 import java.util.List;
 import java.util.Map;
-import com.sun.grizzly.tcp.Response;
-import com.sun.grizzly.util.buf.ByteChunk;
-import com.sun.grizzly.util.buf.MessageBytes;
-import com.sun.grizzly.util.http.MimeHeaders;
+import com.sun.grizzly.tcp.http11.GrizzlyResponse;
 import com.sun.ws.rest.spi.container.MessageBodyContext;
 
 /**
@@ -39,43 +36,29 @@
  */
 public final class GrizzlyResponseAdaptor extends AbstractContainerResponse {
     
- private final Response response;
+ private final GrizzlyResponse response;
     
- private OutputStream output;
-
-
- /* package */ GrizzlyResponseAdaptor(Response response, MessageBodyContext bodyContext, GrizzlyRequestAdaptor requestContext) {
+ /* package */ GrizzlyResponseAdaptor(GrizzlyResponse response,
+ MessageBodyContext bodyContext, GrizzlyRequestAdaptor requestContext) {
         super(bodyContext, requestContext);
         this.response = response;
     }
     
     
- // HttpResponseContextImpl
-
- protected OutputStream getUnderlyingOutputStream() throws IOException {
- if (output != null)
- return output;
-
- return output = new GrizzlyResponseOutputStream();
- }
-
     protected void commitStatusAndHeaders() throws IOException {
         response.setStatus(this.getStatus());
         
- MimeHeaders mh = response.getMimeHeaders();
         for (Map.Entry<String, List<Object>> e : this.getHttpHeaders().entrySet()) {
             String key = e.getKey();
             for (Object value: e.getValue()) {
- MessageBytes mb = mh.addValue(key);
- mb.setString(getHeaderValue(value));
+ response.addHeader(key,value.toString());
             }
         }
 
- if (mh.getValue("Content-Type") != null) {
- response.setContentType(mh.getValue("Content-Type").getString());
+ String contentType = response.getHeader("Content-Type");
+ if (contentType != null) {
+ response.setContentType(contentType);
         }
-
- response.sendHeaders();
     }
     
     /* package */ void commitAll() throws IOException {
@@ -86,46 +69,12 @@
         
         commitStatusAndHeaders();
         
- final OutputStream out = getUnderlyingOutputStream();
+ final OutputStream out = response.getOutputStream();
         writeEntity(out);
         out.close();
     }
-
- private final class GrizzlyResponseOutputStream extends OutputStream {
- public final static int BUFFER_SIZE = 4096;
-
- final ByteChunk chunk;
-
- public GrizzlyResponseOutputStream() {
- chunk = new ByteChunk(BUFFER_SIZE);
- }
-
- public void write(int b) throws IOException {
- if (chunk.getLength() > BUFFER_SIZE)
- flush();
- chunk.append((byte)b);
- }
-
- public void write(byte[] b) throws IOException {
- if (chunk.getLength() > BUFFER_SIZE)
- flush();
- chunk.append(b, 0, b.length);
- }
-
- public void write(byte[] b, int off, int len) throws IOException {
- if (chunk.getLength() > BUFFER_SIZE)
- flush();
- chunk.append(b, off, len);
- }
-
- public void flush() throws IOException {
- response.doWrite(chunk);
- chunk.reset();
- }
-
- public void close() throws IOException {
- flush();
- chunk.recycle();
- }
+
+ protected OutputStream getUnderlyingOutputStream() throws IOException {
+ return response.getOutputStream();
     }
 }
Index: src/impl/com/sun/ws/rest/impl/container/grizzly/GrizzlyContainer.java
===================================================================
--- src/impl/com/sun/ws/rest/impl/container/grizzly/GrizzlyContainer.java (revision 777)
+++ src/impl/com/sun/ws/rest/impl/container/grizzly/GrizzlyContainer.java (working copy)
@@ -22,21 +22,21 @@
 
 package com.sun.ws.rest.impl.container.grizzly;
 
+import com.sun.grizzly.tcp.http11.GrizzlyRequest;
+import com.sun.grizzly.tcp.http11.GrizzlyResponse;
 import com.sun.ws.rest.api.container.ContainerException;
 import com.sun.ws.rest.api.core.HttpResponseContext;
 import com.sun.ws.rest.spi.container.WebApplication;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-import com.sun.grizzly.tcp.ActionCode;
-import com.sun.grizzly.tcp.Adapter;
-import com.sun.grizzly.tcp.Request;
-import com.sun.grizzly.tcp.Response;
+import com.sun.grizzly.tcp.http11.GrizzlyAdapter;
+import java.io.IOException;
 
 /**
  *
  * @author Marc.Hadley_at_Sun.Com
  */
-public class GrizzlyContainer implements Adapter {
+public class GrizzlyContainer extends GrizzlyAdapter {
     
     private WebApplication application;
     
@@ -44,17 +44,20 @@
         this.application = app;
     }
 
- public void service(Request request, Response response) throws Exception {
- GrizzlyRequestAdaptor requestAdaptor =
- new GrizzlyRequestAdaptor(application.getMessageBodyContext(),
- request);
- GrizzlyResponseAdaptor responseAdaptor =
- new GrizzlyResponseAdaptor(response,
- application.getMessageBodyContext(),
- requestAdaptor);
-
+ public void service(GrizzlyRequest request, GrizzlyResponse response) {
+ GrizzlyResponseAdaptor responseAdaptor = null;
         try {
+ GrizzlyRequestAdaptor requestAdaptor =
+ new GrizzlyRequestAdaptor(application.getMessageBodyContext(),
+ request);
+ responseAdaptor =
+ new GrizzlyResponseAdaptor(response,
+ application.getMessageBodyContext(),
+ requestAdaptor);
+
             application.handleRequest(requestAdaptor, responseAdaptor);
+ } catch (IOException ex){
+ ex.printStackTrace();
         } catch (ContainerException e) {
             onException(e, responseAdaptor);
         } catch (RuntimeException e) {
@@ -65,23 +68,17 @@
         
         try {
             responseAdaptor.commitAll();
- request.action( ActionCode.ACTION_POST_REQUEST , null);
         } catch (Exception e) {
             // TODO
             e.printStackTrace();
- } finally {
- response.finish();
         }
     }
-
- public void afterService(Request request, Response response) throws Exception {
- request.recycle();
- response.recycle();
- }
 
- public void fireAdapterEvent(String string, Object object) {
- // Not used.
+ public void afterService(GrizzlyRequest request, GrizzlyResponse response)
+ throws Exception {
+ ;
     }
+
     
     private static void onException(Exception e, HttpResponseContext response) {
         // Log the stack trace
@@ -96,4 +93,5 @@
         response.setResponse(javax.ws.rs.core.Response.serverError().
                 entity(sw.toString()).type("text/plain").build());
     }
+
 }
Index: src/impl/com/sun/ws/rest/impl/container/grizzly/GrizzlyRequestAdaptor.java
===================================================================
--- src/impl/com/sun/ws/rest/impl/container/grizzly/GrizzlyRequestAdaptor.java (revision 777)
+++ src/impl/com/sun/ws/rest/impl/container/grizzly/GrizzlyRequestAdaptor.java (working copy)
@@ -23,14 +23,12 @@
 package com.sun.ws.rest.impl.container.grizzly;
 
 import com.sun.grizzly.tcp.Request;
-import com.sun.grizzly.util.buf.ByteChunk;
+import com.sun.grizzly.tcp.http11.GrizzlyRequest;
 import com.sun.grizzly.util.http.MimeHeaders;
 import com.sun.ws.rest.spi.container.AbstractContainerRequest;
 import com.sun.ws.rest.impl.http.header.HttpHeaderFactory;
 import com.sun.ws.rest.spi.container.MessageBodyContext;
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Enumeration;
@@ -42,11 +40,12 @@
  */
 public final class GrizzlyRequestAdaptor extends AbstractContainerRequest {
     
- private final Request request;
+ private final GrizzlyRequest request;
     
     /** Creates a new instance of GrizzlyRequestAdaptor */
- public GrizzlyRequestAdaptor(MessageBodyContext bodyContext, Request request) {
- super(bodyContext, request.method().toString(), new GrizzlyRequestInputStream(request));
+ public GrizzlyRequestAdaptor(MessageBodyContext bodyContext, GrizzlyRequest request)
+ throws IOException {
+ super(bodyContext, request.getMethod(), request.getInputStream());
         this.request = request;
         
         initiateUriInfo();
@@ -56,9 +55,9 @@
     private void initiateUriInfo() {
         try {
             this.baseUri = new URI(
- request.scheme().toString(),
+ request.getScheme(),
                     null,
- request.serverName().toString(),
+ request.getServerName(),
                     request.getServerPort(),
                     "/",
                     null,
@@ -68,7 +67,8 @@
              * request.unparsedURI() is a URI in encoded form that contains
              * the URI path and URI query components.
              */
- this.completeUri = baseUri.resolve(request.unparsedURI().toString());
+ this.completeUri = baseUri.resolve(
+ request.getRequest().unparsedURI().toString());
         } catch (URISyntaxException ex) {
             ex.printStackTrace();
             throw new IllegalArgumentException(ex);
@@ -78,46 +78,14 @@
     private void copyHttpHeaders() {
         MultivaluedMap<String, String> headers = getRequestHeaders();
         
- MimeHeaders mh = request.getMimeHeaders();
- Enumeration names = mh.names();
+ Enumeration names = request.getHeaderNames();
         while (names.hasMoreElements()) {
             String name = (String)names.nextElement();
- String value = mh.getHeader(name);
+ String value = request.getHeader(name);
             headers.add(name, value);
             if (name.equalsIgnoreCase("cookie")) {
                 getCookies().putAll(HttpHeaderFactory.createCookies(value));
             }
         }
     }
-
- private static final class GrizzlyRequestInputStream extends InputStream {
-
- private final Request request;
- private final ByteChunk chunk;
- private ByteArrayInputStream stream;
-
- public GrizzlyRequestInputStream(Request request) {
- this.request = request;
- this.chunk = new ByteChunk();
- }
-
- public int read() throws IOException {
- refillIfRequired();
- return stream.read();
- }
-
- public int read(byte[] b) throws IOException {
- refillIfRequired();
- return stream.read(b);
- }
-
- private void refillIfRequired() throws IOException {
- if (stream==null || stream.available()==0) {
- //chunk.recycle();
- request.doRead(chunk);
- if (chunk.getLength() > 0)
- stream = new ByteArrayInputStream(chunk.getBytes(), chunk.getStart(), chunk.getLength());
- }
- }
- }
 }