View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.ws.transport.http;
18  
19  import java.io.UnsupportedEncodingException;
20  import java.net.URLDecoder;
21  import java.net.URLEncoder;
22  
23  /**
24   * Utilities for working with HTTP transports.
25   */
26  public class HTTPTransportUtils {
27  
28      /** Constructor. */
29      protected HTTPTransportUtils() {
30      }
31  
32      /**
33       * Adds Cache-Control and Pragma headers meant to disable caching.
34       * 
35       * @param transport transport to add headers to
36       */
37      public static void addNoCacheHeaders(HTTPOutTransport transport) {
38          transport.setHeader("Cache-control", "no-cache, no-store");
39          transport.setHeader("Pragma", "no-cache");
40      }
41  
42      /**
43       * Sets the character encoding of the transport to UTF-8.
44       * 
45       * @param transport transport to set character encoding type
46       */
47      public static void setUTF8Encoding(HTTPOutTransport transport) {
48          transport.setCharacterEncoding("UTF-8");
49      }
50  
51      /**
52       * Sets the MIME content type of the transport.
53       * 
54       * @param transport the transport to set content type on
55       * @param contentType the content type to set
56       */
57      public static void setContentType(HTTPOutTransport transport, String contentType) {
58          transport.setHeader("Content-Type", contentType);
59      }
60  
61      /**
62       * URL Decode the given string.
63       * 
64       * @param value the string to decode
65       * @return the decoded string
66       */
67      public static String urlDecode(String value) {
68          try {
69              return URLDecoder.decode(value, "UTF-8");
70          } catch (UnsupportedEncodingException e) {
71              // UTF-8 encoding is required to be supported by all JVMs
72              return null;
73          }
74      }
75  
76      /**
77       * URL Encode the given string.
78       * 
79       * @param value the string to encode
80       * @return the encoded string
81       */
82      public static String urlEncode(String value) {
83          try {
84              return URLEncoder.encode(value, "UTF-8");
85          } catch (UnsupportedEncodingException e) {
86              // UTF-8 encoding is required to be supported by all JVMs
87              return null;
88          }
89      }
90      
91      /**
92       * Get the first raw (i.e. non URL-decoded) query string component with the specified parameter name.
93       * 
94       * The component will be returned as a string in the form 'paramName=paramValue' (minus the quotes).
95       * 
96       * @param queryString the raw HTTP URL query string
97       * @param paramName the name of the parameter to find
98       * @return the found component, or null if not found
99       */
100     public static String getRawQueryStringParameter(String queryString, String paramName) {
101         if (queryString == null) {
102             return null;
103         }
104         
105         String paramPrefix = paramName + "=";
106         int start = queryString.indexOf(paramPrefix);
107         if (start == -1) {
108             return null;
109         }
110         
111         int end = queryString.indexOf('&', start);
112         if (end == -1) {
113             return queryString.substring(start);
114         } else {
115             return queryString.substring(start, end);
116         }
117     }
118 }