1095136
[tomcat7.0] /
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18
19 package org.apache.catalina.connector;
20
21 import java.io.IOException;
22
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.apache.catalina.comet.CometEvent;
28 import org.apache.tomcat.util.res.StringManager;
29
30 public class CometEventImpl implements CometEvent {
31
32
33     /**
34      * The string manager for this package.
35      */
36     protected static final StringManager sm =
37         StringManager.getManager(Constants.Package);
38
39
40     public CometEventImpl(Request request, Response response) {
41         this.request = request;
42         this.response = response;
43     }
44
45
46     // ----------------------------------------------------- Instance Variables
47
48     
49     /**
50      * Associated request.
51      */
52     protected Request request = null;
53
54
55     /**
56      * Associated response.
57      */
58     protected Response response = null;
59
60     
61     /**
62      * Event type.
63      */
64     protected EventType eventType = EventType.BEGIN;
65     
66
67     /**
68      * Event sub type.
69      */
70     protected EventSubType eventSubType = null;
71     
72
73     // --------------------------------------------------------- Public Methods
74
75     /**
76      * Clear the event.
77      */
78     public void clear() {
79         request = null;
80         response = null;
81     }
82
83     public void setEventType(EventType eventType) {
84         this.eventType = eventType;
85     }
86     
87     public void setEventSubType(EventSubType eventSubType) {
88         this.eventSubType = eventSubType;
89     }
90     
91     public void close() throws IOException {
92         if (request == null) {
93             throw new IllegalStateException(sm.getString("cometEvent.nullRequest"));
94         }
95         boolean iscomet = request.isComet();
96         request.setComet(false);
97         response.finishResponse();
98         if (iscomet) request.cometClose();
99     }
100
101     public EventSubType getEventSubType() {
102         return eventSubType;
103     }
104
105     public EventType getEventType() {
106         return eventType;
107     }
108
109     public HttpServletRequest getHttpServletRequest() {
110         return request.getRequest();
111     }
112
113     public HttpServletResponse getHttpServletResponse() {
114         return response.getResponse();
115     }
116
117     public void setTimeout(int timeout) throws IOException, ServletException,
118             UnsupportedOperationException {
119         if (request.getAttribute("org.apache.tomcat.comet.timeout.support") == Boolean.TRUE) {
120             request.setAttribute("org.apache.tomcat.comet.timeout", new Integer(timeout));
121             if (request.isComet()) request.setCometTimeout(timeout);
122         } else {
123             throw new UnsupportedOperationException();
124         }
125     }
126     
127     @Override
128     public String toString() {
129         StringBuilder buf = new StringBuilder();
130         buf.append(super.toString());
131         buf.append("[EventType:");
132         buf.append(eventType);
133         buf.append(", EventSubType:");
134         buf.append(eventSubType);
135         buf.append("]");
136         return buf.toString();
137     }
138
139 }