01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.transport.http.server;
21:
22: import org.apache.axis2.Constants;
23: import org.apache.axis2.context.MessageContext;
24: import org.apache.axis2.transport.http.HTTPConstants;
25: import org.apache.http.HttpException;
26: import org.apache.http.HttpResponse;
27: import org.apache.http.HttpResponseInterceptor;
28: import org.apache.http.message.BufferedHeader;
29: import org.apache.http.protocol.HttpContext;
30: import org.apache.http.util.CharArrayBuffer;
31:
32: import java.io.IOException;
33:
34: public class ResponseSessionCookie implements HttpResponseInterceptor {
35:
36: public void process(final HttpResponse response,
37: final HttpContext context) throws HttpException,
38: IOException {
39: if (response == null) {
40: throw new IllegalArgumentException(
41: "HTTP response may not be null");
42: }
43: if (context == null) {
44: throw new IllegalArgumentException(
45: "HTTP context may not be null");
46: }
47:
48: String sessionCookie = null;
49: MessageContext msgctx = (MessageContext) context
50: .getAttribute(AxisParams.MESSAGE_CONTEXT);
51: if (msgctx != null) {
52: sessionCookie = (String) msgctx
53: .getProperty(HTTPConstants.COOKIE_STRING);
54: }
55: if (sessionCookie == null) {
56: sessionCookie = (String) context
57: .getAttribute(HTTPConstants.COOKIE_STRING);
58: }
59: if (sessionCookie != null) {
60: // Generate Netscape style cookie header
61: CharArrayBuffer buffer1 = new CharArrayBuffer(sessionCookie
62: .length() + 40);
63: buffer1.append(HTTPConstants.HEADER_SET_COOKIE);
64: buffer1.append(": ");
65: buffer1.append(Constants.SESSION_COOKIE_JSESSIONID);
66: buffer1.append("=");
67: buffer1.append(sessionCookie);
68: response.addHeader(new BufferedHeader(buffer1));
69:
70: // Generate RFC2965 cookie2 header
71: CharArrayBuffer buffer2 = new CharArrayBuffer(sessionCookie
72: .length() + 50);
73: buffer2.append(HTTPConstants.HEADER_SET_COOKIE2);
74: buffer2.append(": ");
75: buffer2.append(Constants.SESSION_COOKIE_JSESSIONID);
76: buffer2.append("=");
77: buffer2.append(sessionCookie);
78: buffer2.append("; ");
79: int port = response.getParams().getIntParameter(
80: AxisParams.LISTENER_PORT, 0);
81: if (port > 0) {
82: buffer2.append("Port=\"");
83: buffer2.append(Integer.toString(port));
84: buffer2.append("\"; ");
85: }
86: buffer2.append("Version=1");
87: response.addHeader(new BufferedHeader(buffer2));
88: }
89: }
90:
91: }
|