org.exparity.io.internet.HttpHeaders Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of exparity-data Show documentation
Show all versions of exparity-data Show documentation
Data scraping and manipulation utilities for Java
The newest version!
/*
*
*/
package org.exparity.io.internet;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.Validate;
/**
* @author Stewart Bissett
*/
public class HttpHeaders {
private List headers = new ArrayList();
public HttpHeaders(final Collection headers) {
Validate.notNull(headers, "Headers collection cannot be null");
this.headers.addAll(headers);
}
HttpHeaders() {}
public String getHeaderValue(final String entryName) {
String[] found = getHeaderValues(entryName);
return found.length > 0 ? found[0] : null;
}
public String[] getHeaderValues(final String entryName) {
Set values = new HashSet();
for (HttpHeader entry : headers) {
if (entryName.equals(entry.getKey())) {
values.add(entry.getValue());
}
}
return values.toArray(new String[] {});
}
public static HttpHeaders getFrom(final HttpURLConnection con) {
Collection responseHeaders = new ArrayList();
for (String key : con.getHeaderFields().keySet()) {
for (String value : con.getHeaderFields().get(key)) {
responseHeaders.add(new HttpHeader(key, value));
}
}
return new HttpHeaders(responseHeaders);
}
public void setOn(final HttpURLConnection con) {
for (HttpHeader header : headers) {
con.setRequestProperty(header.getKey(), header.getValue());
}
}
public List getHeaders() {
return headers;
}
}