Coverage Report - org.apache.tapestry.json.HTTP
 
Classes in this File Line Coverage Branch Coverage Complexity
HTTP
0%
0/47
0%
0/26
5.667
 
 1  
 package org.apache.tapestry.json;
 2  
 
 3  
 /*
 4  
  Copyright (c) 2002 JSON.org
 5  
 
 6  
  Permission is hereby granted, free of charge, to any person obtaining a copy
 7  
  of this software and associated documentation files (the "Software"), to deal
 8  
  in the Software without restriction, including without limitation the rights
 9  
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10  
  copies of the Software, and to permit persons to whom the Software is
 11  
  furnished to do so, subject to the following conditions:
 12  
 
 13  
  The above copyright notice and this permission notice shall be included in all
 14  
  copies or substantial portions of the Software.
 15  
 
 16  
  The Software shall be used for Good, not Evil.
 17  
 
 18  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 24  
  SOFTWARE.
 25  
  */
 26  
 
 27  
 import java.text.ParseException;
 28  
 import java.util.Iterator;
 29  
 import java.util.NoSuchElementException;
 30  
 
 31  
 /**
 32  
  * Convert an HTTP header to a JSONObject and back.
 33  
  * 
 34  
  * @author JSON.org
 35  
  * @version 0.1
 36  
  */
 37  
 public final class HTTP
 38  
 {
 39  
 
 40  
     /** Carriage return/line feed. */
 41  
     public static final String CRLF = "\r\n";
 42  
 
 43  
     /* defeat instantiation */
 44  
     private HTTP()
 45  0
     {
 46  0
     }
 47  
 
 48  
     /**
 49  
      * Convert an HTTP header string into a JSONObject. It can be a request
 50  
      * header or a response header. A request header will contain
 51  
      * 
 52  
      * <pre>
 53  
      * {
 54  
      *     Method: &quot;POST&quot; (for example),
 55  
      *     &quot;Request-URI&quot;: &quot;/&quot; (for example),
 56  
      *     &quot;HTTP-Version&quot;: &quot;HTTP/1.1&quot; (for example)
 57  
      *  }
 58  
      * </pre>
 59  
      * 
 60  
      * A response header will contain
 61  
      * 
 62  
      * <pre>
 63  
      * {
 64  
      *     &quot;HTTP-Version&quot;: &quot;HTTP/1.1&quot; (for example),
 65  
      *     &quot;Status-Code&quot;: &quot;200&quot; (for example),
 66  
      *     &quot;Reason-Phrase&quot;: &quot;OK&quot; (for example)
 67  
      *  }
 68  
      * </pre>
 69  
      * 
 70  
      * In addition, the other parameters in the header will be captured, using
 71  
      * the HTTP field names as JSON names, so that
 72  
      * 
 73  
      * <pre>
 74  
      *     Date: Sun, 26 May 2002 18:06:04 GMT
 75  
      *     Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&amp;b=2&amp;f=s
 76  
      *     Cache-Control: no-cache
 77  
      * </pre>
 78  
      * 
 79  
      * become
 80  
      * 
 81  
      * <pre>
 82  
      * {...
 83  
      *     Date: &quot;Sun, 26 May 2002 18:06:04 GMT&quot;,
 84  
      *     Cookie: &quot;Q=q2=PPEAsg--; B=677gi6ouf29bn&amp;b=2&amp;f=s&quot;,
 85  
      *     &quot;Cache-Control&quot;: &quot;no-cache&quot;,
 86  
      *  ...}
 87  
      * </pre>
 88  
      * 
 89  
      * It does no further checking or conversion. It does not parse dates. It
 90  
      * does not do '%' transforms on URLs.
 91  
      * 
 92  
      * @param string
 93  
      *            An HTTP header string.
 94  
      * @return A JSONObject containing the elements and attributes of the XML
 95  
      *         string.
 96  
      * @throws ParseException
 97  
      */
 98  
     public static JSONObject toJSONObject(String string)
 99  
         throws ParseException
 100  
     {
 101  0
         JSONObject o = new JSONObject();
 102  0
         HTTPTokener x = new HTTPTokener(string);
 103  
         String t;
 104  
 
 105  0
         t = x.nextToken();
 106  0
         if (t.toUpperCase().startsWith("HTTP"))
 107  
         {
 108  
 
 109  
             // Response
 110  
 
 111  0
             o.put("HTTP-Version", t);
 112  0
             o.put("Status-Code", x.nextToken());
 113  0
             o.put("Reason-Phrase", x.nextTo('\0'));
 114  0
             x.next();
 115  
 
 116  
         }
 117  
         else
 118  
         {
 119  
 
 120  
             // Request
 121  
 
 122  0
             o.put("Method", t);
 123  0
             o.put("Request-URI", x.nextToken());
 124  0
             o.put("HTTP-Version", x.nextToken());
 125  
         }
 126  
 
 127  
         // Fields
 128  
 
 129  0
         while(x.more())
 130  
         {
 131  0
             String name = x.nextTo(':');
 132  0
             x.next(':');
 133  0
             o.put(name, x.nextTo('\0'));
 134  0
             x.next();
 135  0
         }
 136  0
         return o;
 137  
     }
 138  
 
 139  
     /**
 140  
      * Convert a JSONObject into an HTTP header. A request header must contain
 141  
      * 
 142  
      * <pre>
 143  
      * {
 144  
      *     Method: &quot;POST&quot; (for example),
 145  
      *     &quot;Request-URI&quot;: &quot;/&quot; (for example),
 146  
      *     &quot;HTTP-Version&quot;: &quot;HTTP/1.1&quot; (for example)
 147  
      *  }
 148  
      * </pre>
 149  
      * 
 150  
      * A response header must contain
 151  
      * 
 152  
      * <pre>
 153  
      * {
 154  
      *     &quot;HTTP-Version&quot;: &quot;HTTP/1.1&quot; (for example),
 155  
      *     &quot;Status-Code&quot;: &quot;200&quot; (for example),
 156  
      *     &quot;Reason-Phrase&quot;: &quot;OK&quot; (for example)
 157  
      *  }
 158  
      * </pre>
 159  
      * 
 160  
      * Any other members of the JSONObject will be output as HTTP fields. The
 161  
      * result will end with two CRLF pairs.
 162  
      * 
 163  
      * @param o
 164  
      *            A JSONObject
 165  
      * @return An HTTP header string.
 166  
      * @throws NoSuchElementException
 167  
      *             if the object does not contain enough information.
 168  
      */
 169  
     public static String toString(JSONObject o)
 170  
     {
 171  0
         Iterator keys = o.keys();
 172  
         String s;
 173  0
         StringBuffer sb = new StringBuffer();
 174  0
         if (o.has("Status-Code") && o.has("Reason-Phrase"))
 175  
         {
 176  0
             sb.append(o.getString("HTTP-Version"));
 177  0
             sb.append(' ');
 178  0
             sb.append(o.getString("Status-Code"));
 179  0
             sb.append(' ');
 180  0
             sb.append(o.getString("Reason-Phrase"));
 181  
         }
 182  0
         else if (o.has("Method") && o.has("Request-URI"))
 183  
         {
 184  0
             sb.append(o.getString("Method"));
 185  0
             sb.append(' ');
 186  0
             sb.append('"');
 187  0
             sb.append(o.getString("Request-URI"));
 188  0
             sb.append('"');
 189  0
             sb.append(' ');
 190  0
             sb.append(o.getString("HTTP-Version"));
 191  
         }
 192  
         else
 193  
         {
 194  0
             throw new NoSuchElementException(
 195  
                     "Not enough material for an HTTP header.");
 196  
         }
 197  0
         sb.append(CRLF);
 198  0
         while(keys.hasNext())
 199  
         {
 200  0
             s = keys.next().toString();
 201  0
             if (!s.equals("HTTP-Version") && !s.equals("Status-Code")
 202  
                     && !s.equals("Reason-Phrase") && !s.equals("Method")
 203  
                     && !s.equals("Request-URI") && !o.isNull(s))
 204  
             {
 205  0
                 sb.append(s);
 206  0
                 sb.append(": ");
 207  0
                 sb.append(o.getString(s));
 208  0
                 sb.append(CRLF);
 209  
             }
 210  
         }
 211  0
         sb.append(CRLF);
 212  0
         return sb.toString();
 213  
     }
 214  
 }