public String getCharset() { String charset = null; Header contenttype = this.headers.getFirstHeader("Content-Type"); if (contenttype != null) { HeaderElement values[] = contenttype.getElements(); if (values.length == 1) { NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { charset = param.getValue(); } } } if (charset != null) { return charset; } else { return DEFAULT_CONTENT_CHARSET; } }
public static Reader getResponseContentAsReader(@NotNull HttpMethod response) throws IOException { //if (!response.hasBeenUsed()) { // return new StringReader(""); //} InputStream stream = response.getResponseBodyAsStream(); String charsetName = null; org.apache.commons.httpclient.Header header = response.getResponseHeader(HTTP.CONTENT_TYPE); if (header != null) { // find out encoding for (HeaderElement part : header.getElements()) { NameValuePair pair = part.getParameterByName("charset"); if (pair != null) { charsetName = pair.getValue(); } } } return new InputStreamReader(stream, charsetName == null ? DEFAULT_CHARSET_NAME : charsetName); }
public FileContentInfo create(FileContent fileContent) throws FileSystemException { HttpFileObject httpFile = (HttpFileObject) (FileObjectUtils .getAbstractFileObject(fileContent.getFile())); String contentType = null; String contentEncoding = null; Header header = httpFile.getHeadMethod().getResponseHeader("content-type"); if (header != null) { HeaderElement[] element = header.getElements(); if (element != null && element.length > 0) { contentType = element[0].getName(); } } contentEncoding = httpFile.getHeadMethod().getResponseCharSet(); return new DefaultFileContentInfo(contentType, contentEncoding); }
protected String getContentCharSet(Header contentheader) { String charset = null; if (contentheader != null) { HeaderElement values[] = contentheader.getElements(); if (values.length == 1) { NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { charset = param.getValue(); } } } if (charset == null) { charset = "UTF-8"; } return charset; }
/** * Returns the character set from the <tt>Content-Type</tt> header. * * @param contentheader * The content header. * @return String The character set. */ protected String getContentCharSet(final Header contentheader) { String charset = null; if (contentheader != null) { final HeaderElement values[] = contentheader.getElements(); // I expect only one header element to be there // No more. no less if (values.length == 1) { final NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { // If I get anything "funny" // UnsupportedEncondingException will result charset = param.getValue(); } } } if (charset == null) { charset = getParams().getContentCharset(); } return charset; }
public static Reader getResponseContentAsReader(@NotNull HttpMethod response) throws IOException { //if (!response.hasBeenUsed()) { // return new StringReader(""); //} InputStream stream = response.getResponseBodyAsStream(); String charsetName = null; org.apache.commons.httpclient.Header header = response.getResponseHeader(HTTP.CONTENT_TYPE); if(header != null) { // find out encoding for(HeaderElement part : header.getElements()) { NameValuePair pair = part.getParameterByName("charset"); if(pair != null) { charsetName = pair.getValue(); } } } return new InputStreamReader(stream, charsetName == null ? DEFAULT_CHARSET_NAME : charsetName); }
/** * Creates a new entity with the given content, content type, and charset. * * @param content The content to set. * @param contentType The type of the content, or <code>null</code>. The value retured * by {@link #getContentType()}. If this content type contains a charset and the charset * parameter is null, the content's type charset will be used. * @param charset The charset of the content, or <code>null</code>. Used to convert the * content to bytes. If the content type does not contain a charset and charset is not null, * then the charset will be appended to the content type. */ public StringRequestEntity(String content, String contentType, String charset) throws UnsupportedEncodingException { super(); if (content == null) { throw new IllegalArgumentException("The content cannot be null"); } this.contentType = contentType; this.charset = charset; // resolve the content type and the charset if (contentType != null) { HeaderElement[] values = HeaderElement.parseElements(contentType); NameValuePair charsetPair = null; for (int i = 0; i < values.length; i++) { if ((charsetPair = values[i].getParameterByName("charset")) != null) { // charset found break; } } if (charset == null && charsetPair != null) { // use the charset from the content type this.charset = charsetPair.getValue(); } else if (charset != null && charsetPair == null) { // append the charset to the content type this.contentType = contentType + "; charset=" + charset; } } if (this.charset != null) { this.content = content.getBytes(this.charset); } else { this.content = content.getBytes(); } }
public String getCharset() { String charset = DEFAULT_CONTENT_CHARSET; Header contenttype = this.headers.getFirstHeader("Content-Type"); if (contenttype != null) { HeaderElement values[] = contenttype.getElements(); if (values.length == 1) { NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { charset = param.getValue(); } } } return charset; }
public Header[] getHeaders(String targetInstanceName, int harvestResultNumber, HarvestResourceDTO resource) throws DigitalAssetStoreException { try { WCTSoapCall call = new WCTSoapCall(host, port, service, "getHeaders"); call.regTypes(Header.class, HarvestResourceDTO.class, ArcHarvestResourceDTO.class, HeaderElement.class, ArcHarvestResultDTO.class, HarvestResultDTO.class, NameValuePair.class); Header[] headers = (Header[]) call.invoke(targetInstanceName, harvestResultNumber, resource); return headers; } catch(Exception ex) { throw new DigitalAssetStoreException("Failed to get headers for " + targetInstanceName + " " + harvestResultNumber + ": " + ex.getMessage(), ex); } }
private String processCookieHeader(HeaderElement element) { String cookie = element.getName() + "=" + element.getValue(); NameValuePair[] parameters = element.getParameters(); for (int j = 0; parameters != null && j < parameters.length; j++) { NameValuePair parameter = parameters[j]; cookie = cookie + "; " + parameter.getName() + "=" + parameter.getValue(); } return cookie; }
public FileContentInfo create(FileContent fileContent) throws FileSystemException { HttpFileObject httpFile = (HttpFileObject) (FileObjectUtils .getAbstractFileObject(fileContent.getFile())); String contentType = null; String contentEncoding = null; Header header = httpFile.getHeadMethod().getResponseHeader("content-type"); if (header != null) { HeaderElement[] element; try { element = header.getValues(); } catch (HttpException e) { throw new FileSystemException(e); } if (element != null && element.length > 0) { contentType = element[0].getName(); } } contentEncoding = httpFile.getHeadMethod().getResponseCharSet(); return new DefaultFileContentInfo(contentType, contentEncoding); }
/** * @see #parse(String, int, String, boolean, org.apache.commons.httpclient.Header) */ public Cookie[] parse(String host, int port, String path, boolean secure, final String header) throws MalformedCookieException { LOG.trace("enter RFC2965Spec.parse(" + "String, int, String, boolean, String)"); // before we do anything, lets check validity of arguments if (host == null) { throw new IllegalArgumentException( "Host of origin may not be null"); } if (host.trim().equals("")) { throw new IllegalArgumentException( "Host of origin may not be blank"); } if (port < 0) { throw new IllegalArgumentException("Invalid port: " + port); } if (path == null) { throw new IllegalArgumentException( "Path of origin may not be null."); } if (header == null) { throw new IllegalArgumentException("Header may not be null."); } if (path.trim().equals("")) { path = PATH_DELIM; } host = getEffectiveHost(host); HeaderElement[] headerElements = HeaderElement.parseElements(header.toCharArray()); List cookies = new LinkedList(); for (int i = 0; i < headerElements.length; i++) { HeaderElement headerelement = headerElements[i]; Cookie2 cookie = null; try { cookie = new Cookie2(host, headerelement.getName(), headerelement.getValue(), path, null, false, new int[] {port}); } catch (IllegalArgumentException ex) { throw new MalformedCookieException(ex.getMessage()); } NameValuePair[] parameters = headerelement.getParameters(); // could be null. In case only a header element and no parameters. if (parameters != null) { // Eliminate duplicate attribues. The first occurence takes precedence Map attribmap = new HashMap(parameters.length); for (int j = parameters.length - 1; j >= 0; j--) { NameValuePair param = parameters[j]; attribmap.put(param.getName().toLowerCase(), param); } for (Iterator it = attribmap.entrySet().iterator(); it.hasNext(); ) { Map.Entry entry = (Map.Entry) it.next(); parseAttribute((NameValuePair) entry.getValue(), cookie); } } cookies.add(cookie); // cycle through the parameters } return (Cookie[]) cookies.toArray(new Cookie[cookies.size()]); }
public String authenticate(String url, String host, Integer port, String realm, String userName, String password) { Validate.notEmpty(url, "url must not be empty"); Validate.notEmpty(host, "host must not be empty"); Validate.notNull(port, "port must not be empty"); Validate.notEmpty(userName, "userName must not be empty"); Validate.notEmpty(password, "password must not be empty"); try { HttpClient client = new HttpClient(); GetMethod getMethod = new GetMethod(url); int status = client.executeMethod(getMethod); if (LOG.isDebugEnabled()) LOG.debug("status: {}", status); String responseBody = getMethod.getResponseBodyAsString(); if (LOG.isDebugEnabled()) LOG.debug("responseBody: {}", responseBody); Header wwAuthHeader = getMethod.getResponseHeader(WWW_AUTHENTICATE); for (HeaderElement element : wwAuthHeader.getElements()) { if (LOG.isDebugEnabled()) LOG.debug(element.getName() + " : " + element.getValue()); } UsernamePasswordCredentials upc = new UsernamePasswordCredentials(userName, password); AuthScope as = new AuthScope(host, port, realm); client.getState().setCredentials(as, upc); status = client.executeMethod(getMethod); if (LOG.isDebugEnabled()) LOG.debug("status: {}", status); responseBody = getMethod.getResponseBodyAsString(); if (LOG.isDebugEnabled()) LOG.debug("responseBody: {}", responseBody); getMethod.releaseConnection(); return responseBody; } catch (Exception e) { LOG.error("Error:", e); } return null; }
/** * Collect the HTTP header information and set them in the message context * * @param method HttpMethodBase from which to get information * @param msgContext the MessageContext in which to place the information... OR NOT! * @throws AxisFault if problems occur */ protected void obtainHTTPHeaderInformation(HttpMethodBase method, MessageContext msgContext) throws AxisFault { // Set RESPONSE properties onto the REQUEST message context. They will need to be copied off the request context onto // the response context elsewhere, for example in the OutInOperationClient. Map transportHeaders = new CommonsTransportHeaders(method.getResponseHeaders()); msgContext.setProperty(MessageContext.TRANSPORT_HEADERS, transportHeaders); msgContext.setProperty(HTTPConstants.MC_HTTP_STATUS_CODE, new Integer(method.getStatusCode())); Header header = method.getResponseHeader(HTTPConstants.HEADER_CONTENT_TYPE); if (header != null) { HeaderElement[] headers = header.getElements(); MessageContext inMessageContext = msgContext.getOperationContext().getMessageContext( WSDLConstants.MESSAGE_LABEL_IN_VALUE); Object contentType = header.getValue(); Object charSetEnc = null; for (int i = 0; i < headers.length; i++) { NameValuePair charsetEnc = headers[i].getParameterByName( HTTPConstants.CHAR_SET_ENCODING); if (charsetEnc != null) { charSetEnc = charsetEnc.getValue(); } } if (inMessageContext != null) { inMessageContext .setProperty(Constants.Configuration.CONTENT_TYPE, contentType); inMessageContext .setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc); } else { // Transport details will be stored in a HashMap so that anybody interested can // retrieve them HashMap transportInfoMap = new HashMap(); transportInfoMap.put(Constants.Configuration.CONTENT_TYPE, contentType); transportInfoMap.put(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc); //the HashMap is stored in the outgoing message. msgContext.setProperty(Constants.Configuration.TRANSPORT_INFO_MAP, transportInfoMap); } } String sessionCookie = null; // Process old style headers first Header[] cookieHeaders = method.getResponseHeaders(HTTPConstants.HEADER_SET_COOKIE); String customCoookiId = (String) msgContext.getProperty(Constants.CUSTOM_COOKIE_ID); // process all the cookieHeaders, when load balancer is fronted it may require some cookies to function. sessionCookie = processCookieHeaders(cookieHeaders); // Overwrite old style cookies with new style ones if present cookieHeaders = method.getResponseHeaders(HTTPConstants.HEADER_SET_COOKIE2); for (int i = 0; i < cookieHeaders.length; i++) { HeaderElement[] elements = cookieHeaders[i].getElements(); for (int e = 0; e < elements.length; e++) { HeaderElement element = elements[e]; if (Constants.SESSION_COOKIE.equalsIgnoreCase(element.getName()) || Constants.SESSION_COOKIE_JSESSIONID.equalsIgnoreCase(element.getName())) { sessionCookie = processCookieHeader(element); } if(customCoookiId!=null&&customCoookiId.equalsIgnoreCase(element.getName())){ sessionCookie = processCookieHeader(element); } } } if (sessionCookie != null && !sessionCookie.isEmpty()) { msgContext.getServiceContext().setProperty(HTTPConstants.COOKIE_STRING, sessionCookie); } }