HttpUtil.java
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *   http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing,
00013  * software distributed under the License is distributed on an
00014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00015  * KIND, either express or implied.  See the License for the
00016  * specific language governing permissions and limitations
00017  * under the License.    
00018  */
00019 package org.apache.xmlrpc.util;
00020 
00021 import java.io.IOException;
00022 import java.io.InputStream;
00023 import java.io.UnsupportedEncodingException;
00024 import java.util.Enumeration;
00025 import java.util.StringTokenizer;
00026 
00027 import org.apache.ws.commons.util.Base64;
00028 import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl;
00029 import org.apache.xmlrpc.common.XmlRpcStreamConfig;
00030 
00031 
00034 public class HttpUtil {
00042         public static String encodeBasicAuthentication(String pUser, String pPassword, String pEncoding) throws UnsupportedEncodingException {
00043         if (pUser == null) {
00044                         return null;
00045         }
00046         final String s = pUser + ':' + pPassword;
00047                 if (pEncoding == null) {
00048                         pEncoding = XmlRpcStreamConfig.UTF8_ENCODING;
00049                 }
00050         final byte[] bytes = s.getBytes(pEncoding);
00051                 return Base64.encode(s.getBytes(pEncoding), 0, bytes.length, 0, null);
00052     }
00053 
00061         public static boolean isUsingGzipEncoding(String pHeaderValue) {
00062                 if (pHeaderValue == null) {
00063                         return false;
00064         }
00065         for (StringTokenizer st = new StringTokenizer(pHeaderValue, ",");  st.hasMoreTokens();  ) {
00066             String encoding = st.nextToken();
00067             int offset = encoding.indexOf(';');
00068             if (offset >= 0) {
00069                 encoding = encoding.substring(0, offset);
00070             }
00071             if ("gzip".equalsIgnoreCase(encoding.trim())) {
00072                 return true;
00073             }
00074         }
00075         return false;
00076     }
00077 
00085     public static String getNonIdentityTransferEncoding(String pHeaderValue) {
00086         if (pHeaderValue == null) {
00087             return null;
00088         }
00089         for (StringTokenizer st = new StringTokenizer(pHeaderValue, ",");  st.hasMoreTokens();  ) {
00090             String encoding = st.nextToken();
00091             int offset = encoding.indexOf(';');
00092             if (offset >= 0) {
00093                 encoding = encoding.substring(0, offset);
00094             }
00095             if (!"identity".equalsIgnoreCase(encoding.trim())) {
00096                 return encoding.trim();
00097             }
00098         }
00099         return null;
00100     }
00101 
00109         public static boolean isUsingGzipEncoding(Enumeration pValues) {
00110                 if (pValues != null) {
00111                         while (pValues.hasMoreElements()) {
00112                                 if (isUsingGzipEncoding((String) pValues.nextElement())) {
00113                                         return true;
00114                                 }
00115                         }
00116                 }
00117                 return false;
00118     }
00119 
00129         public static String readLine(InputStream pIn, byte[] pBuffer) throws IOException {
00130         int next;
00131         int count = 0;
00132         while (true) {
00133             next = pIn.read();
00134             if (next < 0 || next == '\n') {
00135                 break;
00136             }
00137             if (next != '\r') {
00138                 pBuffer[count++] = (byte) next;
00139             }
00140             if (count >= pBuffer.length) {
00141                 throw new IOException ("HTTP Header too long");
00142             }
00143         }
00144         return new String(pBuffer, 0, count, "US-ASCII");
00145     }
00146 
00152         public static void parseAuthorization(XmlRpcHttpRequestConfigImpl pConfig, String pLine) {
00153                 if (pLine == null) {
00154                         return;
00155                 }
00156                 pLine = pLine.trim();
00157                 StringTokenizer st = new StringTokenizer(pLine);
00158                 if (!st.hasMoreTokens()) {
00159                         return;
00160                 }
00161                 String type = st.nextToken();
00162                 if (!"basic".equalsIgnoreCase(type)) {
00163                         return;
00164                 }
00165                 if (!st.hasMoreTokens()) {
00166                         return;
00167                 }
00168                 String auth = st.nextToken();
00169             try {
00170                 byte[] c = Base64.decode(auth.toCharArray(), 0, auth.length());
00171                 String enc = pConfig.getBasicEncoding();
00172             if (enc == null) {
00173                 enc = XmlRpcStreamConfig.UTF8_ENCODING;
00174             }
00175             String str = new String(c, enc);
00176                 int col = str.indexOf(':');
00177                         if (col >= 0) {
00178                                 pConfig.setBasicUserName(str.substring(0, col));
00179                                 pConfig.setBasicPassword(str.substring(col+1));
00180                         }
00181             } catch (Throwable ignore) {
00182             }
00183         }
00184 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49