KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > web > servlet > ServletInterface


1 package com.teamkonzept.web.servlet;
2
3 import java.util.*;
4 import java.io.*;
5 import javax.servlet.http.*;
6
7 import com.teamkonzept.lib.*;
8 import com.teamkonzept.web.*;
9
10 import org.apache.log4j.Category;
11
12 public class ServletInterface implements TKHttpInterface {
13
14     protected TKHashtable env = null;
15     protected TKHashtable params = null;
16     protected TKHashtable headers = null;
17     protected String JavaDoc gateway = null;
18
19     protected HttpServletRequest req;
20     protected HttpServletResponse resp;
21     protected HttpServlet serv;
22
23     // wir sind ja jetzt Tomcat, wie ist das zu aendern ?
24
static protected String JavaDoc attrPrefJServ = "org.apache.jserv.";
25     
26     private static Category cat = Category.getInstance(ServletInterface.class);
27     
28     public ServletInterface( HttpServletRequest req, HttpServletResponse resp, HttpServlet serv )
29     {
30         this.req = req;
31         this.resp = resp;
32         this.serv = serv;
33     }
34     
35     public HttpServletRequest getRequest()
36     {
37         return req;
38     }
39
40     /** returns the http session */
41     public HttpSession getSession(boolean create)
42     {
43         return req.getSession(create);
44     }
45     
46     /** returns the htp session */
47     public HttpSession getSession()
48     {
49         return req.getSession();
50     }
51
52     protected String JavaDoc getGateway()
53     {
54         if( gateway != null ) return gateway;
55         gateway = (String JavaDoc)req.getAttribute( attrPrefJServ+"GATEWAY_INTERFACE" );
56         if( gateway != null )
57             return gateway;
58
59         return (gateway = "unknown");
60     }
61
62     public TKHashtable getParams()
63     {
64         if( params != null ) return params;
65
66         Hashtable tmpHash = null;
67         if( req.getMethod().equals("POST") ) {
68             String JavaDoc contentType = req.getContentType();
69             if( contentType.equals("application/x-www-form-urlencoded") ) {
70                 try {
71                     tmpHash = HttpUtils.parsePostData(
72                         req.getContentLength(), req.getInputStream()
73                     ) ;
74                 }
75                 catch( IOException e ) {
76                     e.printStackTrace( new PrintStream ( getLogStream() ) );
77                 }
78             }
79             else if( contentType.startsWith("multipart/form-data") ) {
80                 int boudaryStart = contentType.indexOf("boundary=")+9;
81                 String JavaDoc boundary = (boudaryStart<=9?null:contentType.substring( boudaryStart ));
82                 try {
83                     TKHttpMultipartBuffer buf = new TKHttpMultipartBuffer(
84                         req.getInputStream(),
85                         req.getContentLength(),
86                         boundary
87                     );
88                     tmpHash = buf.getParams();
89                 }
90                 catch( IOException e ) {
91                     cat.error("getParams", e);
92                 }
93
94             }
95         }
96         else {
97             String JavaDoc queryString = req.getQueryString();
98             if( queryString != null && queryString.length() > 0 ) {
99                 try {
100                     tmpHash = HttpUtils.parseQueryString( req.getQueryString() );
101                 }
102                 catch( Throwable JavaDoc t ) {
103                     tmpHash = new Hashtable(1);
104                     tmpHash.put( "QUERY_STRING", queryString );
105                 }
106             }
107         }
108
109         if( tmpHash != null && tmpHash.size() > 0 ) {
110             params = new TKHashtable( tmpHash.size() );
111             Enumeration keys = tmpHash.keys();
112
113             while( keys.hasMoreElements() ) {
114                 Object JavaDoc key = keys.nextElement();
115                 Object JavaDoc val = tmpHash.get( key );
116                 if( val instanceof Object JavaDoc[] ) {
117                     Object JavaDoc[] valVec = (Object JavaDoc[])val;
118                     if( valVec.length > 1 ) {
119                         params.put( key, new TKVector( valVec ) );
120                     }
121                     else {
122                         params.put( key, valVec[0] );
123                     }
124                 }
125                 else {
126                     params.put( key, val );
127                 }
128             }
129         }
130         else {
131             params = new TKHashtable();
132         }
133
134         return params;
135     }
136
137     public TKHashtable getEnvironment()
138     {
139         if( env != null ) return env;
140
141         // Unterschiedlich je nach Gateway
142
env = new TKHashtable();
143
144         Object JavaDoc attrsObj = req.getAttribute(attrPrefJServ+"attribute_names");
145         if ( attrsObj != null && attrsObj instanceof Enumeration ) {
146             Enumeration attrs = (Enumeration) attrsObj;
147             while ( attrs.hasMoreElements()) {
148                 String JavaDoc attr = attrs.nextElement().toString();
149                 if ( req.getAttribute(attrPrefJServ + attr) != null ) {
150                     env.put( attr, req.getAttribute(attrPrefJServ + attr).toString() );
151                 }
152                 else {
153                     env.put( attr, "" );
154                 }
155             }
156         }
157
158         // das auf jeden Fall
159
String JavaDoc s;
160         int i;
161         i = req.getContentLength();
162         if( i >= 0 ) env.put( "CONTENT_LENGTH", String.valueOf(i) );
163         s = req.getContentType();
164         if( s != null ) env.put( "CONTENT_TYPE", s );
165         s = req.getScheme();
166         if( s != null ) env.put( "SCHEME", s );
167         s = req.getProtocol();
168         if( s != null ) env.put( "SERVER_PROTOCOL", s );
169         i = req.getServerPort();
170         if( i >= 0 ) env.put( "SERVER_PORT", String.valueOf(i) );
171         s = req.getRemoteAddr();
172         if( s != null ) env.put( "REMOTE_ADDR", s );
173         s = req.getRemoteHost();
174         if( s != null ) env.put( "REMOTE_HOST", s );
175         s = req.getAuthType();
176         if( s != null ) env.put( "AUTH_TYPE", s );
177         s = req.getPathInfo();
178         if( s != null ) env.put( "PATH_INFO", s );
179         s = req.getPathTranslated();
180         if( s != null ) env.put( "PATH_TRANSLATED", s );
181         s = req.getMethod();
182         if( s != null ) env.put( "REQUEST_METHOD", s );
183         s = req.getRemoteUser();
184         if( s != null ) env.put( "REMOTE_USER", s );
185
186         return env;
187     }
188     
189     public HttpServletResponse getResponse()
190     {
191         return resp;
192     }
193     
194     public javax.servlet.ServletContext JavaDoc getServletContext()
195     {
196         return serv.getServletContext();
197     }
198     
199     public OutputStream getOutputStream()
200     {
201         try {
202             return resp.getOutputStream();
203         }
204         catch ( IOException e ) {
205             e.printStackTrace( new PrintStream( getLogStream() ) );
206         }
207         return System.out;
208     }
209
210     public OutputStream getLogStream()
211     {
212         return new ServletLog( serv.getServletContext() );
213     }
214
215     public String JavaDoc getOwnName()
216     {
217         String JavaDoc path = req.getServletPath();
218         int l = path.lastIndexOf( "/" );
219         if( l != -1 )
220             return path.substring( l+1 );
221         return path;
222     }
223
224     public String JavaDoc getOwnURL()
225     {
226         String JavaDoc s=req.getRequestURI();
227         int i=s.indexOf('?');
228         return ( i >= 0 ? s.substring(0,i) : s);
229     }
230
231     public String JavaDoc getOwnPath()
232     {
233         String JavaDoc path = req.getServletPath();
234         int l = path.lastIndexOf( "/" );
235         if( l != -1 )
236             return path.substring( 0, l+1 );
237         return "";
238     }
239
240     public String JavaDoc getContextPath()
241     {
242         return req.getContextPath();
243     }
244     
245     public String JavaDoc getDocumentRoot()
246     {
247         return req.getRealPath("/");
248     }
249
250     public String JavaDoc getServerName()
251     {
252         return req.getServerName();
253     }
254
255     public String JavaDoc getRemoteUser()
256     {
257         return req.getRemoteUser();
258     }
259
260     public TKHashtable getHeaders()
261     {
262         if( headers != null ) return headers;
263
264         TKHashtable headers = new TKHashtable();
265         Enumeration e = req.getHeaderNames();
266         while( e.hasMoreElements() ) {
267             Object JavaDoc k = e.nextElement();
268             Object JavaDoc v = req.getHeader( (String JavaDoc)k );
269             headers.put( k, (v!=null?v:"") );
270         }
271         return headers;
272     }
273
274     public void setStatus( int code, String JavaDoc msg )
275     {
276         resp.setStatus( code, msg );
277     }
278
279     public void addHeader( String JavaDoc name, String JavaDoc value )
280     {
281         resp.setHeader( name, value );
282     }
283
284     public String JavaDoc getPathInfo()
285     {
286         return req.getPathInfo();
287     }
288
289     public String JavaDoc getPathTranslated()
290     {
291         return req.getPathTranslated();
292     }
293
294     public Cookie[] getCookies()
295     {
296         return req.getCookies();
297     }
298
299
300     /** liefert absolute URL - inkl. Servername + Port */
301     public String JavaDoc getAbsoluteURL()
302     {
303         return (HttpUtils.getRequestURL(req)).toString();
304     }
305     
306     public void addCookie( final Cookie cookie )
307     {
308         resp.addCookie( cookie );
309     }
310
311     public void addDateHeader( final String JavaDoc name, final long value )
312     {
313         resp.setDateHeader( name, value );
314     }
315 }
316
317
Popular Tags