Skip to content

Commit

Permalink
#58 support add/set Headers in RequestBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
jheintz committed Oct 30, 2012
1 parent 77b2adb commit deb0d13
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 47 deletions.
56 changes: 56 additions & 0 deletions src/main/java/com/gistlabs/mechanize/headers/Header.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (C) 2012 Gist Labs, LLC. (http://gistlabs.com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.gistlabs.mechanize.headers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* Implements a multi-value header.
*/
public class Header implements Iterable<String> {
private final String name;
private final List<String> values = new ArrayList<String>();

public Header(final String name, final String value) {
this.name = name;
this.values.add(value);
}

public String getName() {
return name;
}

public boolean isSingleValue() {
return values.size() == 1;
}

public void addValue(final String value) {
if(!values.contains(value))
values.add(value);
}

public String getValue() {
return values.get(0);
}

public List<String> getValues() {
return values;
}

@Override
public Iterator<String> iterator() {
return values.iterator();
}

@Override
public String toString() {
return String.format("%s: %s", name, values);
}
}
113 changes: 113 additions & 0 deletions src/main/java/com/gistlabs/mechanize/headers/Headers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Copyright (C) 2012 Gist Labs, LLC. (http://gistlabs.com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.gistlabs.mechanize.headers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Representation of a header list being able to use multiple String values for a single parameter.
*/
public class Headers implements Iterable<Header> {

private final LinkedHashMap<String, Header> headers = new LinkedHashMap<String, Header>();

public Headers() {
}

public Headers(final Map<String, Object> parameters) {
List<String> sortedKeys = new ArrayList<String>(parameters.keySet());
for(String name : sortedKeys) {
Object value = parameters.get(name);
if(value != null && !(value instanceof String []))
add(name, value.toString());
else if(value instanceof String [])
add(name, (String [])value);
}
}

/** Returns true if at least one value is present for the given header name. */
public boolean has(final String name) {
return this.headers.containsKey(name);
}

/** Returns the current values of the headers in natural sort order or null if none. */
public String [] get(final String name) {
if(has(name)) {
List<String> values = headers.get(name).getValues();
return values.toArray(new String [values.size()]);
}
else
return null;
}

/** Returns the header names in the order they where added. */
public String [] getNames() {
String [] result = new String[headers.size()];
headers.keySet().toArray(result);
return result;
}

public Headers set(final String name, final String ... values) {
remove(name);
add(name, values);
return this;
}

public Headers remove(final String name) {
headers.remove(name);
return this;
}

public Headers add(final String name, final String ... values) {
for(String value : values)
add(name, value);
return this;
}

public Headers set(final String name, final String value) {
remove(name);
add(name, value);
return this;
}

public Headers add(final String name, final String value) {
if(has(name))
headers.get(name).addValue(value);
else
headers.put(name, new Header(name, value));
return this;
}

public Collection<Header> getHeaderss() {
return headers.values();
}

@Override
public Iterator<Header> iterator() {
return headers.values().iterator();
}

/** Returns the sum of all values within the header collection. */
public int getValueCount() {
int count = 0;
for (Header header : this)
count += header.getValues().size();

return count;
}

@Override
public String toString() {
return headers.toString();
}
}
58 changes: 50 additions & 8 deletions src/main/java/com/gistlabs/mechanize/requestor/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
Expand All @@ -27,6 +31,8 @@
import org.apache.http.message.BasicNameValuePair;

import com.gistlabs.mechanize.exceptions.MechanizeExceptionFactory;
import com.gistlabs.mechanize.headers.Header;
import com.gistlabs.mechanize.headers.Headers;
import com.gistlabs.mechanize.parameters.Parameter;
import com.gistlabs.mechanize.parameters.Parameters;
import com.gistlabs.mechanize.util.apache.URIBuilder;
Expand All @@ -36,6 +42,8 @@ public class RequestBuilder<Resource> {
private final PageRequestor<Resource> requestor;
private String uri;
private final Parameters parameters = new Parameters();
private final Headers setHeaders = new Headers();
private final Headers addHeaders = new Headers();
private final Map<String, ContentBody> files = new HashMap<String, ContentBody>();
private boolean isMultiPart = false;

Expand Down Expand Up @@ -79,15 +87,39 @@ public RequestBuilder<Resource> set(final Parameters parameters) {
}

public RequestBuilder<Resource> add(final Parameters parameters) {
for(String name :parameters.getNames())
for(String name : parameters.getNames())
add(name, parameters.get(name));

return this;
}

public RequestBuilder<Resource> accept(final String string) {
// TODO Auto-generated method stub
return null;
public RequestBuilder<Resource> addHeader(final String name, final String ... values) {
addHeaders.add(name, values);
return this;
}

public RequestBuilder<Resource> setHeader(final String name, final String ... values) {
setHeaders.set(name, values);
return this;
}

public RequestBuilder<Resource> setHeaders(final Headers headers) {
for(String name : headers.getNames())
setHeader(name, headers.get(name));

return this;
}

public RequestBuilder<Resource> addHeaders(final Headers headers) {
for(String name :headers.getNames())
addHeader(name, headers.get(name));

return this;
}

public RequestBuilder<Resource> accept(final String contentType) {
this.setHeader("Accept", contentType);
return this;
}

/** Adds a file to the request also making the request to become a multi-part post request or removes any file registered
Expand All @@ -105,21 +137,22 @@ public RequestBuilder<Resource> set(final String name, final ContentBody content
return this;
}



public Parameters parameters() {
return parameters;
}

public <T extends Resource> T get() {
if(hasFiles())
throw new UnsupportedOperationException("Files can not be send using a get request");
return requestor.request(composeGetRequest(uri, parameters));
HttpRequestBase request = composeGetRequest(uri, parameters);
buildHeaders(request);
return requestor.request(request);
}

public <T extends Resource> T post() {
HttpPost request = (!hasFiles()) || isMultiPart ? composePostRequest(getBaseUri(), parameters) :
composeMultiPartFormRequest(getBaseUri(), parameters, files);
buildHeaders(request);
return requestor.request(request);
}

Expand All @@ -131,6 +164,15 @@ private String getBaseUri() {
return uri.contains("?") ? uri.substring(0, uri.indexOf('?')) : uri;
}

private void buildHeaders(final HttpRequestBase request) {
for(Header header : this.setHeaders)
for(String value : header)
request.setHeader(header.getName(), value);
for(Header header : this.addHeaders)
for(String value : header)
request.addHeader(header.getName(), value);
}

private HttpRequestBase composeGetRequest(String uri, final Parameters parameters) {
try {
URIBuilder builder = new URIBuilder(uri);
Expand Down
Loading

0 comments on commit deb0d13

Please sign in to comment.