org.apache.commons.httpclient.HeaderElement Java Examples

The following examples show how to use org.apache.commons.httpclient.HeaderElement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FlexGetMethod.java    From flex-blazeds with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: FlexPostMethod.java    From flex-blazeds with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: HttpFileContentInfoFactory.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
public FileContentInfo create(final FileContent fileContent) throws FileSystemException {

    String contentType = null;
    String contentEncoding = null;

    HeadMethod headMethod;
    try (final HttpFileObject<HttpFileSystem> httpFile = (HttpFileObject<HttpFileSystem>) FileObjectUtils
            .getAbstractFileObject(fileContent.getFile())) {
        headMethod = httpFile.getHeadMethod();
    } catch (final IOException e) {
        throw new FileSystemException(e);
    }
    final Header header = headMethod.getResponseHeader("content-type");
    if (header != null) {
        final HeaderElement[] element = header.getElements();
        if (element != null && element.length > 0) {
            contentType = element[0].getName();
        }
    }

    contentEncoding = headMethod.getResponseCharSet();

    return new DefaultFileContentInfo(contentType, contentEncoding);
}
 
Example #4
Source File: StringRequestEntity.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example #5
Source File: DigitalAssetStoreSOAPClient.java    From webcurator with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example #6
Source File: RFC2965Spec.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @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()]);
}
 
Example #7
Source File: NetscapeDraftSpec.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
  * Parses the Set-Cookie value into an array of <tt>Cookie</tt>s.
  *
  * <p>Syntax of the Set-Cookie HTTP Response Header:</p>
  * 
  * <p>This is the format a CGI script would use to add to 
  * the HTTP headers a new piece of data which is to be stored by 
  * the client for later retrieval.</p>
  *  
  * <PRE>
  *  Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
  * </PRE>
  *
  * <p>Please note that Netscape draft specification does not fully 
  * conform to the HTTP header format. Netscape draft does not specify 
  * whether multiple cookies may be sent in one header. Hence, comma 
  * character may be present in unquoted cookie value or unquoted 
  * parameter value.</p>
  * 
  * @link http://wp.netscape.com/newsref/std/cookie_spec.html
  * 
  * @param host the host from which the <tt>Set-Cookie</tt> value was
  * received
  * @param port the port from which the <tt>Set-Cookie</tt> value was
  * received
  * @param path the path from which the <tt>Set-Cookie</tt> value was
  * received
  * @param secure <tt>true</tt> when the <tt>Set-Cookie</tt> value was
  * received over secure conection
  * @param header the <tt>Set-Cookie</tt> received from the server
  * @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie value
  * @throws MalformedCookieException if an exception occurs during parsing
  * 
  * @since 3.0
  */
public Cookie[] parse(String host, int port, String path, 
    boolean secure, final String header) 
    throws MalformedCookieException {
        
    LOG.trace("enter NetscapeDraftSpec.parse(String, port, path, boolean, Header)");

    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 = host.toLowerCase();

    String defaultPath = path;    
    int lastSlashIndex = defaultPath.lastIndexOf(PATH_DELIM);
    if (lastSlashIndex >= 0) {
        if (lastSlashIndex == 0) {
            //Do not remove the very first slash
            lastSlashIndex = 1;
        }
        defaultPath = defaultPath.substring(0, lastSlashIndex);
    }
    HeaderElement headerelement = new HeaderElement(header.toCharArray());
    Cookie cookie = new Cookie(host,
                   headerelement.getName(),
                   headerelement.getValue(),
                   defaultPath, 
                   null,
                   false);
    // cycle through the parameters
    NameValuePair[] parameters = headerelement.getParameters();
    // could be null. In case only a header element and no parameters.
    if (parameters != null) {
        for (int j = 0; j < parameters.length; j++) {
            parseAttribute(parameters[j], cookie);
        }
    }
    return new Cookie[] {cookie};
}