Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hamnis
Last active December 11, 2016 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hamnis/5642915 to your computer and use it in GitHub Desktop.
Save hamnis/5642915 to your computer and use it in GitHub Desktop.
import net.hamnaberg.json.parser.CollectionParser;
import net.hamnaberg.funclite.Function;
import net.hamnaberg.funclite.FunctionalList;
import net.hamnaberg.funclite.Optional;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
public class Example {
public static void main(String[] args) throws Exception {
DefaultHttpClient client = new DefaultHttpClient();
Collection collection = client.execute(new HttpGet(URI.create("http://example.com/events")), new CollectionResponseHandler());
Optional<Link> alternate = collection.linkByRel("alternate");
for (Link link : alternate) {
Collection secondCollection = client.execute(new HttpGet(link.getHref()), new CollectionResponseHandler());
FunctionalList<SomeType> transformed = FunctionalList.create(secondCollection.getItems()).map(new Function<Item, SomeType>() {
@Override
public SomeType apply(Item input) {
return new SomeType(input);
}
});
System.out.println("transformed = " + transformed);
}
client.getConnectionManager().shutdown();
}
private static class CollectionResponseHandler implements ResponseHandler<Collection> {
@Override
public Collection handleResponse(HttpResponse response) throws IOException {
if (response.getStatusLine().getStatusCode() == 200) {
if (isCollectionJSON(response)) {
InputStream content = null;
try {
content = response.getEntity().getContent();
return new CollectionParser().parse(content);
} finally {
if (content != null) {
content.close();
}
}
}
}
throw new RuntimeException("No usable status here");
}
private boolean isCollectionJSON(HttpResponse response) {
return response.getEntity() != null && "application/vnd.collection+json".equals(response.getEntity().getContentType().getValue());
}
}
private static class SomeType {
private final Item item;
public SomeType(Item input) {
item = input;
}
public Optional<String> getName() {
return item.propertyByName("name").flatMap(propertyToValueString);
}
private Item getItem() {
return item;
}
}
private static final Function<Value,String> valueStringFunction = new Function<Value, String>() {
@Override
public String apply(Value input) {
return input.asString();
}
};
private static final Function<Property,Optional<String>> propertyToValueString = new Function<Property, Optional<String>>() {
@Override
public Optional<String> apply(Property input) {
return input.getValue().map(valueStringFunction);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment