users@grizzly.java.net

Re: Logging question

From: kevinb <kbrisso_at_gmail.com>
Date: Fri, 6 Jan 2012 13:46:10 -0800 (PST)

OK, this worked, code below for anyone who is interested.



package com.frk.jerseyrestgrizzly;

import org.glassfish.grizzly.filterchain.*;

import com.sun.jersey.api.container.grizzly2.GrizzlyWebContainerFactory;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.HttpContent;
import org.glassfish.grizzly.http.HttpHeader;
import org.glassfish.grizzly.http.HttpProbe;
import org.glassfish.grizzly.http.ContentEncoding;
import org.glassfish.grizzly.http.TransferEncoding;
import org.glassfish.grizzly.http.HttpPacket;
import org.glassfish.grizzly.http.HttpCodecFilter;

import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.Connection;


import org.glassfish.grizzly.http.jmx.*;
import org.glassfish.grizzly.http.server.StaticHttpHandler;

/**
 * Hello world!
 *
 */
public class App {

    /**
     * @param args the command line arguments
     */
    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost/").port(8080).build();
    }
    public static final URI BASE_URI = getBaseURI();

    protected static HttpServer startServer() throws IOException {

        final Map<String, String> initParams = new HashMap<String,
String>();

        initParams.put("com.sun.jersey.config.property.packages",
                "com.frk.jerseyrestgrizzly");

        System.out.println("Starting grizzly...");

        return GrizzlyWebContainerFactory.create(BASE_URI, initParams);

    }

    public static void main(String[] args) throws IOException {
        final FilterChain filterChain;

        HttpServer httpServer = startServer();

        httpServer.getServerConfiguration().addHttpHandler(new
StaticHttpHandler("C:\\Projects\\JerseyRESTGrizzly\\test\\"), "/test");

        filterChain = httpServer.getListener("grizzly").getFilterChain();

        // Get HttpCodecFilter

        HttpCodecFilter codecFilter = (HttpCodecFilter)
filterChain.get(filterChain.indexOfType(HttpCodecFilter.class));
        codecFilter.getMonitoringConfig().addProbes(new HttpProbe() {

            public void onDataReceivedEvent(Connection connection, Buffer
buffer) {
                System.out.println(buffer.toStringContent()); //
Log incoming traffic
            }

            public void onDataSentEvent(Connection connection, Buffer
buffer) {
                System.out.println(buffer.toStringContent()); //
// Log outgoing traffic
            }

            public void onHeaderParseEvent(Connection connection, HttpHeader
header, int size) {
            }

            public void onHeaderSerializeEvent(Connection connection,
HttpHeader header, Buffer buffer) {
            }

            public void onContentChunkParseEvent(Connection connection,
HttpContent content) {
            }

            public void onContentChunkSerializeEvent(Connection connection,
HttpContent content) {
            }

            public void onContentEncodingParseEvent(Connection connection,
HttpHeader header, Buffer buffer, ContentEncoding contentEncoding) {
            }

            public void onContentEncodingSerializeEvent(Connection
connection, HttpHeader header, Buffer buffer, ContentEncoding
contentEncoding) {
            }

            public void onTransferEncodingParseEvent(Connection connection,
HttpHeader header, Buffer buffer, TransferEncoding transferEncoding) {
            }

            public void onTransferEncodingSerializeEvent(Connection
connection, HttpHeader header, Buffer buffer, TransferEncoding
transferEncoding) {
            }

            public void onErrorEvent(Connection connection, HttpPacket
httpPacket, Throwable error) {
            }

            public void onErrorEvent(Connection cnctn, Throwable thrwbl) {
                throw new UnsupportedOperationException("Not supported
yet.");
            }
        });



        System.out.println(String.format("Jersey app started with WADL
available at "
                + "%sapplication.wadl\nTry out %shelloworld\nHit enter to
stop it...",
                BASE_URI, BASE_URI));
        System.in.read();

        httpServer.stop();

    }
}


--
View this message in context: http://grizzly.1045725.n5.nabble.com/Logging-question-tp5076118p5126754.html
Sent from the Grizzly - Users mailing list archive at Nabble.com.