~ubuntu-branches/ubuntu/saucy/restlet/saucy

« back to all changes in this revision

Viewing changes to org.restlet.ext.grizzly/src/org/restlet/ext/grizzly/internal/HttpParserFilter.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 16:25:45 UTC
  • Revision ID: package-import@ubuntu.com-20120611162545-5w2o0resi5y3pybc
Tags: upstream-2.0.14
ImportĀ upstreamĀ versionĀ 2.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2005-2012 Restlet S.A.S.
 
3
 * 
 
4
 * The contents of this file are subject to the terms of one of the following
 
5
 * open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
 
6
 * 1.0 (the "Licenses"). You can select the license that you prefer but you may
 
7
 * not use this file except in compliance with one of these Licenses.
 
8
 * 
 
9
 * You can obtain a copy of the Apache 2.0 license at
 
10
 * http://www.opensource.org/licenses/apache-2.0
 
11
 * 
 
12
 * You can obtain a copy of the LGPL 3.0 license at
 
13
 * http://www.opensource.org/licenses/lgpl-3.0
 
14
 * 
 
15
 * You can obtain a copy of the LGPL 2.1 license at
 
16
 * http://www.opensource.org/licenses/lgpl-2.1
 
17
 * 
 
18
 * You can obtain a copy of the CDDL 1.0 license at
 
19
 * http://www.opensource.org/licenses/cddl1
 
20
 * 
 
21
 * You can obtain a copy of the EPL 1.0 license at
 
22
 * http://www.opensource.org/licenses/eclipse-1.0
 
23
 * 
 
24
 * See the Licenses for the specific language governing permissions and
 
25
 * limitations under the Licenses.
 
26
 * 
 
27
 * Alternatively, you can obtain a royalty free commercial license with less
 
28
 * limitations, transferable or non-transferable, directly at
 
29
 * http://www.restlet.com/products/restlet-framework
 
30
 * 
 
31
 * Restlet is a registered trademark of Restlet S.A.S.
 
32
 */
 
33
 
 
34
package org.restlet.ext.grizzly.internal;
 
35
 
 
36
import java.io.IOException;
 
37
import java.nio.ByteBuffer;
 
38
import java.nio.channels.SelectionKey;
 
39
 
 
40
import org.restlet.ext.grizzly.GrizzlyServerHelper;
 
41
import org.restlet.ext.grizzly.HttpsServerHelper;
 
42
 
 
43
import com.sun.grizzly.Context;
 
44
import com.sun.grizzly.ProtocolFilter;
 
45
import com.sun.grizzly.util.WorkerThread;
 
46
 
 
47
/**
 
48
 * HTTP parser filter for Grizzly.
 
49
 * 
 
50
 * @author Jerome Louvel
 
51
 */
 
52
public class HttpParserFilter implements ProtocolFilter {
 
53
 
 
54
    /** The parent HTTP server helper. */
 
55
    private volatile GrizzlyServerHelper helper;
 
56
 
 
57
    /**
 
58
     * Constructor.
 
59
     * 
 
60
     * @param helper
 
61
     *            The parent HTTP server helper.
 
62
     */
 
63
    public HttpParserFilter(GrizzlyServerHelper helper) {
 
64
        this.helper = helper;
 
65
    }
 
66
 
 
67
    /**
 
68
     * Execute a call.
 
69
     * 
 
70
     * @param context
 
71
     *            The call's context.
 
72
     */
 
73
    public boolean execute(Context context) throws IOException {
 
74
        // Create the HTTP call
 
75
        ByteBuffer byteBuffer = ((WorkerThread) Thread.currentThread())
 
76
                .getByteBuffer();
 
77
        byteBuffer.flip();
 
78
 
 
79
        if (byteBuffer.hasRemaining()) {
 
80
            boolean keepAlive = false;
 
81
            SelectionKey key = context.getSelectionKey();
 
82
            GrizzlyServerCall serverCall = new GrizzlyServerCall(this.helper
 
83
                    .getHelped(), byteBuffer, key,
 
84
                    (this.helper instanceof HttpsServerHelper));
 
85
 
 
86
            // Handle the call
 
87
            this.helper.handle(serverCall);
 
88
 
 
89
            // TODO Should we use httpCall#isKeepAlive?
 
90
            // TODO The "keepAlive" boolean is always set to false at this time.
 
91
            // Prepare for additional calls?
 
92
            if (keepAlive) {
 
93
                context
 
94
                        .setKeyRegistrationState(Context.KeyRegistrationState.REGISTER);
 
95
            } else {
 
96
                context
 
97
                        .setKeyRegistrationState(Context.KeyRegistrationState.CANCEL);
 
98
            }
 
99
        }
 
100
 
 
101
        // Clean up
 
102
        byteBuffer.clear();
 
103
 
 
104
        // This is the last filter
 
105
        return true;
 
106
    }
 
107
 
 
108
    /**
 
109
     * Post execute method.
 
110
     * 
 
111
     * @param context
 
112
     *            The call's context.
 
113
     */
 
114
    public boolean postExecute(Context context) throws IOException {
 
115
        return true;
 
116
    }
 
117
 
 
118
}