KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > transport > http > SimpleAxisWorker


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.transport.http;
57
58 import org.jboss.axis.AxisFault;
59 import org.jboss.axis.Constants;
60 import org.jboss.axis.Message;
61 import org.jboss.axis.MessageContext;
62 import org.jboss.axis.description.OperationDesc;
63 import org.jboss.axis.description.ServiceDesc;
64 import org.jboss.axis.encoding.Base64;
65 import org.jboss.axis.message.SOAPEnvelopeAxisImpl;
66 import org.jboss.axis.message.SOAPFaultImpl;
67 import org.jboss.axis.server.AxisServer;
68 import org.jboss.axis.utils.Messages;
69 import org.jboss.axis.utils.XMLUtils;
70 import org.jboss.logging.Logger;
71 import org.w3c.dom.Document JavaDoc;
72
73 import javax.xml.namespace.QName JavaDoc;
74 import java.io.ByteArrayInputStream JavaDoc;
75 import java.io.OutputStream JavaDoc;
76 import java.net.InetAddress JavaDoc;
77 import java.net.Socket JavaDoc;
78 import java.net.UnknownHostException JavaDoc;
79 import java.util.ArrayList JavaDoc;
80 import java.util.Iterator JavaDoc;
81
82
83 public class SimpleAxisWorker implements Runnable JavaDoc
84 {
85    private static Logger log = Logger.getLogger(SimpleAxisWorker.class.getName());
86
87    private SimpleAxisServer server;
88    private Socket JavaDoc socket;
89
90    // Axis specific constants
91
private static String JavaDoc transportName = "SimpleHTTP";
92
93    // HTTP status codes
94
private static byte OK[] = ("200 " + Messages.getMessage("ok00")).getBytes();
95    private static byte UNAUTH[] = ("401 " + Messages.getMessage("unauth00")).getBytes();
96    private static byte SENDER[] = "400".getBytes();
97    private static byte ISE[] = ("500 " + Messages.getMessage("internalError01")).getBytes();
98
99    // HTTP prefix
100
private static byte HTTP[] = "HTTP/1.0 ".getBytes();
101
102    // Standard MIME headers for XML payload
103
private static byte XML_MIME_STUFF[] =
104            ("\r\nContent-Type: text/xml; charset=utf-8\r\n" +
105            "Content-Length: ").getBytes();
106
107    // Standard MIME headers for HTML payload
108
private static byte HTML_MIME_STUFF[] =
109            ("\r\nContent-Type: text/html; charset=utf-8\r\n" +
110            "Content-Length: ").getBytes();
111
112    // Mime/Content separator
113
private static byte SEPARATOR[] = "\r\n\r\n".getBytes();
114
115    // Tiddly little response
116
// private static final String responseStr =
117
// "<html><head><title>SimpleAxisServer</title></head>" +
118
// "<body><h1>SimpleAxisServer</h1>" +
119
// Messages.getMessage("reachedServer00") +
120
// "</html>";
121
// private static byte cannedHTMLResponse[] = responseStr.getBytes();
122

123    // ASCII character mapping to lower case
124
private static final byte[] toLower = new byte[256];
125
126    static
127    {
128       for (int i = 0; i < 256; i++)
129       {
130          toLower[i] = (byte)i;
131       }
132
133       for (int lc = 'a'; lc <= 'z'; lc++)
134       {
135          toLower[lc + 'A' - 'a'] = (byte)lc;
136       }
137    }
138
139    // buffer for IO
140
private static final int BUFSIZ = 4096;
141
142    // mime header for content length
143
private static final byte lenHeader[] = "content-length: ".getBytes();
144    private static final int lenLen = lenHeader.length;
145
146    // mime header for content type
147
private static final byte typeHeader[] = (HTTPConstants.HEADER_CONTENT_TYPE.toLowerCase() + ": ").getBytes();
148    private static final int typeLen = typeHeader.length;
149
150    // mime header for content location
151
private static final byte locationHeader[] = (HTTPConstants.HEADER_CONTENT_LOCATION.toLowerCase() + ": ").getBytes();
152    private static final int locationLen = locationHeader.length;
153
154    // mime header for soap action
155
private static final byte actionHeader[] = "soapaction: ".getBytes();
156    private static final int actionLen = actionHeader.length;
157
158    // mime header for cookie
159
private static final byte cookieHeader[] = "cookie: ".getBytes();
160    private static final int cookieLen = cookieHeader.length;
161
162    // mime header for cookie2
163
private static final byte cookie2Header[] = "cookie2: ".getBytes();
164    private static final int cookie2Len = cookie2Header.length;
165
166    // HTTP header for authentication
167
private static final byte authHeader[] = "authorization: ".getBytes();
168    private static final int authLen = authHeader.length;
169
170    // mime header for GET
171
private static final byte getHeader[] = "GET".getBytes();
172
173    // mime header for POST
174
private static final byte postHeader[] = "POST".getBytes();
175
176    // header ender
177
private static final byte headerEnder[] = ": ".getBytes();
178
179    // "Basic" auth string
180
private static final byte basicAuth[] = "basic ".getBytes();
181
182    public SimpleAxisWorker(SimpleAxisServer server, Socket JavaDoc socket)
183    {
184       this.server = server;
185       this.socket = socket;
186    }
187
188    /**
189     * The main workhorse method.
190     */

191    public void run()
192    {
193       byte buf[] = new byte[BUFSIZ];
194       // create an Axis server
195
AxisServer engine = server.getAxisServer();
196
197       // create and initialize a message context
198
MessageContext msgContext = new MessageContext(engine);
199       Message requestMsg = null;
200
201       // Reusuable, buffered, content length controlled, InputStream
202
NonBlockingBufferedInputStream is =
203               new NonBlockingBufferedInputStream();
204
205       // buffers for the headers we care about
206
StringBuffer JavaDoc soapAction = new StringBuffer JavaDoc();
207       StringBuffer JavaDoc httpRequest = new StringBuffer JavaDoc();
208       StringBuffer JavaDoc fileName = new StringBuffer JavaDoc();
209       StringBuffer JavaDoc cookie = new StringBuffer JavaDoc();
210       StringBuffer JavaDoc cookie2 = new StringBuffer JavaDoc();
211       StringBuffer JavaDoc authInfo = new StringBuffer JavaDoc();
212       StringBuffer JavaDoc contentType = new StringBuffer JavaDoc();
213       StringBuffer JavaDoc contentLocation = new StringBuffer JavaDoc();
214
215       Message responseMsg = null;
216
217       // prepare request (do as much as possible while waiting for the
218
// next connection). Note the next two statements are commented
219
// out. Uncomment them if you experience any problems with not
220
// resetting state between requests:
221
// msgContext = new MessageContext();
222
// requestMsg = new Message("", "String");
223
//msgContext.setProperty("transport", "HTTPTransport");
224
msgContext.setTransportName(transportName);
225
226       responseMsg = null;
227
228       try
229       {
230          // assume the best
231
byte[] status = OK;
232
233          // assume we're not getting WSDL
234
boolean doWsdl = false;
235
236          // cookie for this session, if any
237
String JavaDoc cooky = null;
238
239          String JavaDoc methodName = null;
240
241          try
242          {
243             // wipe cookies if we're doing sessions
244
if (server.isSessionUsed())
245             {
246                cookie.delete(0, cookie.length());
247                cookie2.delete(0, cookie2.length());
248             }
249             authInfo.delete(0, authInfo.length());
250
251             // read headers
252
is.setInputStream(socket.getInputStream());
253             // parse all headers into hashtable
254
int contentLength = parseHeaders(is, buf, contentType,
255                     contentLocation, soapAction,
256                     httpRequest, fileName,
257                     cookie, cookie2, authInfo);
258             is.setContentLength(contentLength);
259
260             int paramIdx = fileName.toString().indexOf('?');
261             if (paramIdx != -1)
262             {
263                // Got params
264
String JavaDoc params = fileName.substring(paramIdx + 1);
265                fileName.setLength(paramIdx);
266
267                log.debug(Messages.getMessage("filename00",
268                        fileName.toString()));
269                log.debug(Messages.getMessage("params00",
270                        params));
271
272                if ("wsdl".equalsIgnoreCase(params))
273                   doWsdl = true;
274
275                if (params.startsWith("method="))
276                {
277                   methodName = params.substring(7);
278                }
279             }
280
281             // Real and relative paths are the same for the
282
// SimpleAxisServer
283
msgContext.setProperty(Constants.MC_REALPATH,
284                     fileName.toString());
285             msgContext.setProperty(Constants.MC_RELATIVE_PATH,
286                     fileName.toString());
287             msgContext.setProperty(Constants.MC_JWS_CLASSDIR,
288                     "jwsClasses");
289
290             // !!! Fix string concatenation
291
String JavaDoc url = "http://" + getLocalHost() + ":" +
292                     server.getServerSocket().getLocalPort() + "/" +
293                     fileName.toString();
294             msgContext.setProperty(MessageContext.TRANS_URL, url);
295
296             String JavaDoc filePart = fileName.toString();
297             if (filePart.startsWith("axis/services/"))
298             {
299                msgContext.setTargetService(filePart.substring(14));
300             }
301
302             if (authInfo.length() > 0)
303             {
304                // Process authentication info
305
//authInfo = new StringBuffer("dXNlcjE6cGFzczE=");
306
byte[] decoded = Base64.decode(authInfo.toString());
307                StringBuffer JavaDoc userBuf = new StringBuffer JavaDoc();
308                StringBuffer JavaDoc pwBuf = new StringBuffer JavaDoc();
309                StringBuffer JavaDoc authBuf = userBuf;
310                for (int i = 0; i < decoded.length; i++)
311                {
312                   if ((char)(decoded[i] & 0x7f) == ':')
313                   {
314                      authBuf = pwBuf;
315                      continue;
316                   }
317                   authBuf.append((char)(decoded[i] & 0x7f));
318                }
319
320                if (log.isDebugEnabled())
321                {
322                   log.debug(Messages.getMessage("user00",
323                           userBuf.toString()));
324                }
325
326                msgContext.setUsername(userBuf.toString());
327                msgContext.setPassword(pwBuf.toString());
328             }
329
330             // if get, then return simpleton document as response
331
if (httpRequest.toString().equals("GET"))
332             {
333                OutputStream JavaDoc out = socket.getOutputStream();
334                out.write(HTTP);
335                out.write(status);
336
337                if (methodName != null)
338                {
339                   String JavaDoc body =
340                           "<" + methodName + ">" +
341 // args +
342
"</" + methodName + ">";
343                   String JavaDoc msgtxt =
344                           "<SOAP-ENV:Envelope" +
345                           " xmlns:SOAP-ENV=\"" + Constants.URI_SOAP12_ENV + "\">" +
346                           "<SOAP-ENV:Body>" + body + "</SOAP-ENV:Body>" +
347                           "</SOAP-ENV:Envelope>";
348
349                   ByteArrayInputStream JavaDoc istream =
350                           new ByteArrayInputStream JavaDoc(msgtxt.getBytes());
351                   requestMsg = new Message(istream);
352                }
353                else if (doWsdl)
354                {
355                   engine.generateWSDL(msgContext);
356
357                   Document JavaDoc doc = (Document JavaDoc)msgContext.getProperty("WSDL");
358                   if (doc != null)
359                   {
360                      String JavaDoc response = XMLUtils.DocumentToString(doc);
361                      byte[] respBytes = response.getBytes();
362
363                      out.write(XML_MIME_STUFF);
364                      putInt(buf, out, respBytes.length);
365                      out.write(SEPARATOR);
366                      out.write(respBytes);
367                      out.flush();
368                      return;
369                   }
370                }
371                else
372                {
373                   StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
374                   sb.append("<h2>And now... Some Services</h2>\n");
375                   Iterator JavaDoc i = engine.getConfig().getDeployedServices();
376                   out.write("<ul>\n".getBytes());
377                   while (i.hasNext())
378                   {
379                      ServiceDesc sd = (ServiceDesc)i.next();
380                      sb.append("<li>\n");
381                      sb.append(sd.getName());
382                      sb.append(" <a HREF=\"../services/");
383                      sb.append(sd.getName());
384                      sb.append("?wsdl\"><i>(wsdl)</i></a></li>\n");
385                      ArrayList JavaDoc operations = sd.getOperations();
386                      if (!operations.isEmpty())
387                      {
388                         sb.append("<ul>\n");
389                         for (Iterator JavaDoc it = operations.iterator(); it.hasNext();)
390                         {
391                            OperationDesc desc = (OperationDesc)it.next();
392                            sb.append("<li>" + desc.getName());
393                         }
394                         sb.append("</ul>\n");
395                      }
396                   }
397                   sb.append("</ul>\n");
398
399                   byte[] bytes = sb.toString().getBytes();
400
401                   out.write(HTML_MIME_STUFF);
402                   putInt(buf, out, bytes.length);
403                   out.write(SEPARATOR);
404                   out.write(bytes);
405                   out.flush();
406                   return;
407                }
408             }
409             else
410             {
411
412                // this may be "" if either SOAPAction: "" or if no SOAPAction at all.
413
// for now, do not complain if no SOAPAction at all
414
String JavaDoc soapActionString = soapAction.toString();
415                if (soapActionString != null)
416                {
417                   msgContext.setUseSOAPAction(true);
418                   msgContext.setSOAPActionURI(soapActionString);
419                }
420                requestMsg = new Message(is,
421                        false,
422                        contentType.toString(),
423                        contentLocation.toString());
424             }
425
426             msgContext.setRequestMessage(requestMsg);
427
428             // set up session, if any
429
if (server.isSessionUsed())
430             {
431                // did we get a cookie?
432
if (cookie.length() > 0)
433                {
434                   cooky = cookie.toString().trim();
435                }
436                else if (cookie2.length() > 0)
437                {
438                   cooky = cookie2.toString().trim();
439                }
440
441                // if cooky is null, cook up a cooky
442
if (cooky == null)
443                {
444                   // fake one up!
445
// make it be an arbitrarily increasing number
446
// (no this is not thread safe because ++ isn't atomic)
447
int i = server.sessionIndex++;
448                   cooky = "" + i;
449                }
450
451                msgContext.setSession(server.createSession(cooky));
452             }
453
454             // invoke the Axis engine
455
engine.invoke(msgContext);
456
457             // Retrieve the response from Axis
458
responseMsg = msgContext.getResponseMessage();
459             if (responseMsg == null)
460             {
461                throw new AxisFault(Messages.getMessage("nullResponse00"));
462             }
463
464          }
465          catch (Exception JavaDoc e)
466          {
467             AxisFault af;
468             if (e instanceof AxisFault)
469             {
470                af = (AxisFault)e;
471                log.debug(Messages.getMessage("serverFault00"), af);
472                QName JavaDoc faultCode = af.getFaultCode();
473                if (Constants.FAULT_SOAP12_SENDER.equals(faultCode))
474                {
475                   status = SENDER;
476                }
477                else if ("Server.Unauthorized".equals(af.getFaultCode().getLocalPart()))
478                {
479                   status = UNAUTH; // SC_UNAUTHORIZED
480
}
481                else
482                {
483                   status = ISE; // SC_INTERNAL_SERVER_ERROR
484
}
485             }
486             else
487             {
488                status = ISE; // SC_INTERNAL_SERVER_ERROR
489
af = AxisFault.makeFault(e);
490             }
491
492             // There may be headers we want to preserve in the
493
// response message - so if it's there, just add the
494
// FaultElement to it. Otherwise, make a new one.
495
responseMsg = msgContext.getResponseMessage();
496             if (responseMsg == null)
497             {
498                responseMsg = new Message(af);
499                responseMsg.setMessageContext(msgContext);
500             }
501             else
502             {
503                try
504                {
505                   SOAPEnvelopeAxisImpl env = responseMsg.getSOAPEnvelope();
506                   env.clearBody();
507                   env.addBodyElement(new SOAPFaultImpl((AxisFault)e));
508                }
509                catch (AxisFault fault)
510                {
511                   // Should never reach here!
512
}
513             }
514          }
515
516          // Send it on its way...
517
OutputStream JavaDoc out = socket.getOutputStream();
518          out.write(HTTP);
519          out.write(status);
520          //out.write(XML_MIME_STUFF);
521
out.write(("\r\n" + HTTPConstants.HEADER_CONTENT_TYPE + ": " + responseMsg.getContentType(msgContext.getSOAPConstants())).getBytes());
522          // Writing the length causes the entire message to be decoded twice.
523
//out.write(("\r\n" + HTTPConstants.HEADER_CONTENT_LENGTH + ": " + responseMsg.getContentLength()).getBytes());
524
// putInt(out, response.length);
525

526          if (server.isSessionUsed() && null != cooky && 0 != cooky.trim().length())
527          {
528             // write cookie headers, if any
529
// don't sweat efficiency *too* badly
530
// optimize at will
531
StringBuffer JavaDoc cookieOut = new StringBuffer JavaDoc();
532             cookieOut.append("\r\nSet-Cookie: ")
533                     .append(cooky)
534                     .append("\r\nSet-Cookie2: ")
535                     .append(cooky);
536             // OH, THE HUMILITY! yes this is inefficient.
537
out.write(cookieOut.toString().getBytes());
538          }
539
540          out.write(SEPARATOR);
541          responseMsg.writeTo(out);
542          // out.write(response);
543
out.flush();
544       }
545       catch (Exception JavaDoc e)
546       {
547          log.debug(Messages.getMessage("exception00"), e);
548       }
549       finally
550       {
551          try
552          {
553             if (socket != null) socket.close();
554          }
555          catch (Exception JavaDoc e)
556          {
557          }
558       }
559       if (msgContext.getProperty(msgContext.QUIT_REQUESTED) != null)
560       {
561          // why then, quit!
562
try
563          {
564             server.stop();
565          }
566          catch (Exception JavaDoc e)
567          {
568          }
569       }
570
571    }
572
573    protected void invokeMethodFromGet(String JavaDoc methodName, String JavaDoc args) throws Exception JavaDoc
574    {
575
576    }
577
578    /**
579     * Read all mime headers, returning the value of Content-Length and
580     * SOAPAction.
581     *
582     * @param is InputStream to read from
583     * @param contentType The content type.
584     * @param contentLocation The content location
585     * @param soapAction StringBuffer to return the soapAction into
586     * @param httpRequest StringBuffer for GET / POST
587     * @param cookie first cookie header (if doSessions)
588     * @param cookie2 second cookie header (if doSessions)
589     * @return Content-Length
590     */

591    private int parseHeaders(NonBlockingBufferedInputStream is,
592                             byte buf[],
593                             StringBuffer JavaDoc contentType,
594                             StringBuffer JavaDoc contentLocation,
595                             StringBuffer JavaDoc soapAction,
596                             StringBuffer JavaDoc httpRequest,
597                             StringBuffer JavaDoc fileName,
598                             StringBuffer JavaDoc cookie,
599                             StringBuffer JavaDoc cookie2,
600                             StringBuffer JavaDoc authInfo)
601            throws java.io.IOException JavaDoc
602    {
603       int n;
604       int len = 0;
605
606       // parse first line as GET or POST
607
n = this.readLine(is, buf, 0, buf.length);
608       if (n < 0)
609       {
610          // nothing!
611
throw new java.io.IOException JavaDoc(Messages.getMessage("unexpectedEOS00"));
612       }
613
614       // which does it begin with?
615
httpRequest.delete(0, httpRequest.length());
616       fileName.delete(0, fileName.length());
617       contentType.delete(0, contentType.length());
618       contentLocation.delete(0, contentLocation.length());
619
620       if (buf[0] == getHeader[0])
621       {
622          httpRequest.append("GET");
623          for (int i = 0; i < n - 5; i++)
624          {
625             char c = (char)(buf[i + 5] & 0x7f);
626             if (c == ' ')
627                break;
628             fileName.append(c);
629          }
630          log.debug(Messages.getMessage("filename01", "SimpleAxisServer", fileName.toString()));
631          return 0;
632       }
633       else if (buf[0] == postHeader[0])
634       {
635          httpRequest.append("POST");
636          for (int i = 0; i < n - 6; i++)
637          {
638             char c = (char)(buf[i + 6] & 0x7f);
639             if (c == ' ')
640                break;
641             fileName.append(c);
642          }
643          log.debug(Messages.getMessage("filename01", "SimpleAxisServer", fileName.toString()));
644       }
645       else
646       {
647          throw new java.io.IOException JavaDoc(Messages.getMessage("badRequest00"));
648       }
649
650       while ((n = readLine(is, buf, 0, buf.length)) > 0)
651       {
652
653          if ((n <= 2) && (buf[0] == '\n' || buf[0] == '\r') && (len > 0)) break;
654
655          // RobJ gutted the previous logic; it was too hard to extend for more headers.
656
// Now, all it does is search forwards for ": " in the buf,
657
// then do a length / byte compare.
658
// Hopefully this is still somewhat efficient (Sam is watching!).
659

660          // First, search forwards for ": "
661
int endHeaderIndex = 0;
662          while (endHeaderIndex < n && toLower[buf[endHeaderIndex]] != headerEnder[0])
663          {
664             endHeaderIndex++;
665          }
666          endHeaderIndex += 2;
667          // endHeaderIndex now points _just past_ the ": ", and is
668
// comparable to the various lenLen, actionLen, etc. values
669

670          // convenience; i gets pre-incremented, so initialize it to one less
671
int i = endHeaderIndex - 1;
672
673          // which header did we find?
674
if (endHeaderIndex == lenLen && matches(buf, lenHeader))
675          {
676             // parse content length
677

678             while ((++i < n) && (buf[i] >= '0') && (buf[i] <= '9'))
679             {
680                len = (len * 10) + (buf[i] - '0');
681             }
682
683          }
684          else if (endHeaderIndex == actionLen
685                  && matches(buf, actionHeader))
686          {
687
688             soapAction.delete(0, soapAction.length());
689             // skip initial '"'
690
i++;
691             while ((++i < n) && (buf[i] != '"'))
692             {
693                soapAction.append((char)(buf[i] & 0x7f));
694             }
695
696          }
697          else if (server.isSessionUsed() && endHeaderIndex == cookieLen
698                  && matches(buf, cookieHeader))
699          {
700
701             // keep everything up to first ;
702
while ((++i < n) && (buf[i] != ';') && (buf[i] != '\r') && (buf[i] != '\n'))
703             {
704                cookie.append((char)(buf[i] & 0x7f));
705             }
706
707          }
708          else if (server.isSessionUsed() && endHeaderIndex == cookie2Len
709                  && matches(buf, cookie2Header))
710          {
711
712             // keep everything up to first ;
713
while ((++i < n) && (buf[i] != ';') && (buf[i] != '\r') && (buf[i] != '\n'))
714             {
715                cookie2.append((char)(buf[i] & 0x7f));
716             }
717
718          }
719          else if (endHeaderIndex == authLen && matches(buf, authHeader))
720          {
721             if (matches(buf, endHeaderIndex, basicAuth))
722             {
723                i += basicAuth.length;
724                while (++i < n && (buf[i] != '\r') && (buf[i] != '\n'))
725                {
726                   if (buf[i] == ' ') continue;
727                   authInfo.append((char)(buf[i] & 0x7f));
728                }
729             }
730             else
731             {
732                throw new java.io.IOException JavaDoc(Messages.getMessage("badAuth00"));
733             }
734          }
735          else if (endHeaderIndex == locationLen && matches(buf, locationHeader))
736          {
737             while (++i < n && (buf[i] != '\r') && (buf[i] != '\n'))
738             {
739                if (buf[i] == ' ') continue;
740                contentLocation.append((char)(buf[i] & 0x7f));
741             }
742          }
743          else if (endHeaderIndex == typeLen && matches(buf, typeHeader))
744          {
745             while (++i < n && (buf[i] != '\r') && (buf[i] != '\n'))
746             {
747                if (buf[i] == ' ') continue;
748                contentType.append((char)(buf[i] & 0x7f));
749             }
750          }
751
752       }
753       return len;
754    }
755
756    /**
757     * does tolower[buf] match the target byte array, up to the target's length?
758     */

759    public boolean matches(byte[] buf, byte[] target)
760    {
761       for (int i = 0; i < target.length; i++)
762       {
763          if (toLower[buf[i]] != target[i])
764          {
765             return false;
766          }
767       }
768       return true;
769    }
770
771    /**
772     * Case-insensitive match of a target byte [] to a source byte [],
773     * starting from a particular offset into the source.
774     */

775    public boolean matches(byte[] buf, int bufIdx, byte[] target)
776    {
777       for (int i = 0; i < target.length; i++)
778       {
779          if (toLower[buf[bufIdx + i]] != target[i])
780          {
781             return false;
782          }
783       }
784       return true;
785    }
786
787    /**
788     * output an integer into the output stream
789     *
790     * @param out OutputStream to be written to
791     * @param value Integer value to be written.
792     */

793    private void putInt(byte buf[], OutputStream JavaDoc out, int value)
794            throws java.io.IOException JavaDoc
795    {
796       int len = 0;
797       int offset = buf.length;
798
799       // negative numbers
800
if (value < 0)
801       {
802          buf[--offset] = (byte)'-';
803          value = -value;
804          len++;
805       }
806
807       // zero
808
if (value == 0)
809       {
810          buf[--offset] = (byte)'0';
811          len++;
812       }
813
814       // positive numbers
815
while (value > 0)
816       {
817          buf[--offset] = (byte)(value % 10 + '0');
818          value = value / 10;
819          len++;
820       }
821
822       // write the result
823
out.write(buf, offset, len);
824    }
825
826    /**
827     * Read a single line from the input stream
828     *
829     * @param is inputstream to read from
830     * @param b byte array to read into
831     * @param off starting offset into the byte array
832     * @param len maximum number of bytes to read
833     */

834    private int readLine(NonBlockingBufferedInputStream is, byte[] b, int off, int len)
835            throws java.io.IOException JavaDoc
836    {
837       int count = 0, c;
838
839       while ((c = is.read()) != -1)
840       {
841          if (c != '\n' && c != '\r')
842          {
843             b[off++] = (byte)c;
844             count++;
845          }
846          if (count == len) break;
847          if ('\n' == c)
848          {
849             int peek = is.peek(); //If the next line begins with tab or space then this is a continuation.
850
if (peek != ' ' && peek != '\t') break;
851          }
852       }
853       return count > 0 ? count : -1;
854    }
855
856    /**
857     * One method for all host name lookups.
858     */

859    public static String JavaDoc getLocalHost()
860    {
861       // FIXME
862
// This doesn't return anything but 0.0.0.0
863
// String hostname = serverSocket.getInetAddress().getHostAddress();
864
// And this returns the hostname of the host on the other
865
// end of the socket:
866
// String hostname = socket.getInetAddress().getHostName();
867
// This works for 99% of the uses of SimpleAxisServer,
868
// but is very stupid
869
try
870       {
871          return InetAddress.getLocalHost().getHostAddress();
872       }
873       catch (UnknownHostException JavaDoc uhe)
874       {
875          return "localhost";
876       }
877    }
878 }
879
Popular Tags