Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Simple fixes for type warnings
Resolves: https://www.pivotaltracker.com/story/show/60810250

Using non-deprecated HTTP client machinery
Using non-deprecated HTTP multipart machinery
Simpler config for IT HTTP client
  • Loading branch information
ajs6f authored and Andrew Woods committed Nov 15, 2013
1 parent 1c94c0c commit 26c4fbe
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 62 deletions.
Expand Up @@ -22,7 +22,6 @@
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.responses.HtmlTemplate;
import org.fcrepo.http.commons.session.InjectedSession;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

Expand All @@ -48,7 +47,6 @@
import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_JSON;
import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_XML;
import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
import static org.slf4j.LoggerFactory.getLogger;

/**
* Expose node types at a REST endpoint
Expand All @@ -59,8 +57,6 @@
@Path("/fcr:nodetypes")
public class FedoraRepositoryNodeTypes extends AbstractResource {

private final Logger LOGGER = getLogger(FedoraRepositoryNodeTypes.class);

@InjectedSession
protected Session session;

Expand Down
Expand Up @@ -18,7 +18,6 @@

import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.parseInt;
import static java.util.concurrent.TimeUnit.SECONDS;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.OK;
import static org.fcrepo.http.commons.test.util.TestHelpers.parseTriples;
Expand All @@ -36,8 +35,7 @@
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Before;
import org.junit.runner.RunWith;
Expand All @@ -64,16 +62,13 @@ public void setLogger() {
protected static final String serverAddress = "http://" + HOSTNAME + ":" +
SERVER_PORT + "/";

protected final PoolingClientConnectionManager connectionManager =
new PoolingClientConnectionManager();

protected static HttpClient client;

public AbstractResourceIT() {
connectionManager.setMaxTotal(MAX_VALUE);
connectionManager.setDefaultMaxPerRoute(5);
connectionManager.closeIdleConnections(3, SECONDS);
client = new DefaultHttpClient(connectionManager);
final HttpClientBuilder b =
HttpClientBuilder.create().setMaxConnPerRoute(MAX_VALUE)
.setMaxConnTotal(MAX_VALUE);
client = b.build();
}

protected static HttpPost postObjMethod(final String pid) {
Expand Down Expand Up @@ -131,7 +126,7 @@ protected GraphStore getGraphStore(final HttpClient client, final HttpUriRequest
method.addHeader("Accept", "text/n3");
}

HttpResponse response = client.execute(method);
final HttpResponse response = client.execute(method);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
return parseTriples(response.getEntity());
Expand All @@ -149,7 +144,11 @@ protected HttpResponse createObject(final String pid) throws IOException {
}

protected HttpResponse createDatastream(final String pid, final String dsid, final String content) throws IOException {
final HttpResponse response = client.execute(postDSMethod(pid, dsid, content));
logger.trace(
"Attempting to create datastream for object: {} at datastream ID: {}",
pid, dsid);
final HttpResponse response =
client.execute(postDSMethod(pid, dsid, content));
assertEquals(CREATED.getStatusCode(), response.getStatusLine().getStatusCode());
return response;
}
Expand Down
Expand Up @@ -21,14 +21,15 @@
import static java.util.regex.Pattern.DOTALL;
import static java.util.regex.Pattern.compile;
import static junit.framework.TestCase.assertFalse;
import static org.apache.http.entity.ContentType.TEXT_PLAIN;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
Expand Down Expand Up @@ -66,12 +67,10 @@ public void testModifyMultipleDatastreams() throws Exception {
final HttpPost post =
new HttpPost(serverAddress
+ "FedoraDatastreamsTest8/fcr:datastreams?delete=ds_void");

final MultipartEntity multiPartEntity = new MultipartEntity();
multiPartEntity.addPart("ds1", new StringBody("asdfg"));
multiPartEntity.addPart("ds2", new StringBody("qwerty"));

post.setEntity(multiPartEntity);
final MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder.create();
multiPartEntityBuilder.addPart("ds1", new StringBody("asdfg", TEXT_PLAIN));
multiPartEntityBuilder.addPart("ds2", new StringBody("qwerty", TEXT_PLAIN));
post.setEntity(multiPartEntityBuilder.build());

final HttpResponse postResponse = client.execute(post);

Expand All @@ -98,12 +97,12 @@ public void testRetrieveMultipartDatastreams() throws Exception {
final HttpPost post =
new HttpPost(serverAddress
+ "FedoraDatastreamsTest9/fcr:datastreams/");
final MultipartEntityBuilder multiPartEntityBuilder =
MultipartEntityBuilder.create().addPart("ds1",
new StringBody("asdfg", TEXT_PLAIN)).addPart("ds2",
new StringBody("qwerty", TEXT_PLAIN));

final MultipartEntity multiPartEntity = new MultipartEntity();
multiPartEntity.addPart("ds1", new StringBody("asdfg"));
multiPartEntity.addPart("ds2", new StringBody("qwerty"));

post.setEntity(multiPartEntity);
post.setEntity(multiPartEntityBuilder.build());

final HttpResponse postResponse = client.execute(post);
assertEquals(201, postResponse.getStatusLine().getStatusCode());
Expand Down Expand Up @@ -132,11 +131,12 @@ public void testRetrieveFIlteredMultipartDatastreams() throws Exception {
new HttpPost(serverAddress
+ "FedoraDatastreamsTest10/fcr:datastreams");

final MultipartEntity multiPartEntity = new MultipartEntity();
multiPartEntity.addPart("ds1", new StringBody("asdfg"));
multiPartEntity.addPart("ds2", new StringBody("qwerty"));
final MultipartEntityBuilder multiPartEntityBuilder =
MultipartEntityBuilder.create().addPart("ds1",
new StringBody("asdfg", TEXT_PLAIN)).addPart("ds2",
new StringBody("qwerty", TEXT_PLAIN));

post.setEntity(multiPartEntity);
post.setEntity(multiPartEntityBuilder.build());

final HttpResponse postResponse = client.execute(post);
assertEquals(201, postResponse.getStatusLine().getStatusCode());
Expand Down
Expand Up @@ -17,7 +17,6 @@

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.update.GraphStore;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
Expand Down
Expand Up @@ -33,6 +33,7 @@
import static javax.ws.rs.core.Response.Status.OK;
import static nu.validator.htmlparser.common.DoctypeExpectation.NO_DOCTYPE_ERRORS;
import static nu.validator.htmlparser.common.XmlViolationPolicy.ALLOW;
import static org.apache.http.impl.client.cache.CacheConfig.DEFAULT;
import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
import static org.fcrepo.jcr.FedoraJcrTypes.ROOT;
import static org.fcrepo.kernel.RdfLexicon.DC_TITLE;
Expand Down Expand Up @@ -70,7 +71,8 @@
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.impl.client.cache.CachingHttpClient;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.cache.CachingHttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -273,6 +275,7 @@ public void testGetObjectGraphHtml() throws Exception {
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
logger.debug("Retrieved: {}", content);
}

@Test
Expand Down Expand Up @@ -636,8 +639,8 @@ public void testGetProjectedNode() throws Exception {

@Test
public void testDescribeRdfCached() throws RepositoryException, IOException {
final CachingHttpClient specialClient = new CachingHttpClient(client);

final CloseableHttpClient specialClient =
CachingHttpClientBuilder.create().setCacheConfig(DEFAULT).build();
final String pid = "FedoraObjectsRdfTest2";
final String path = "" + pid;
specialClient.execute(new HttpPost(serverAddress + path));
Expand Down
Expand Up @@ -17,7 +17,6 @@

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlFileInput;
Expand All @@ -35,8 +34,7 @@
import java.io.IOException;

import static java.util.UUID.randomUUID;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -136,7 +134,8 @@ public void testCreateNewObjectAndDeleteIt() throws IOException {


final HtmlPage page2 = webClient.getPage(serverAddress + pid);
assertEquals(404, page2.getWebResponse().getStatusCode());
assertEquals("Didn't get a 404!", 404, page2.getWebResponse()
.getStatusCode());

webClient.getOptions().setThrowExceptionOnFailingStatusCode(throwExceptionOnFailingStatusCode);
}
Expand Down Expand Up @@ -164,7 +163,7 @@ public void testCreateNewObjectAndSetProperties() throws IOException {
assertTrue(page1.getElementById("metadata").asText().contains("some-predicate"));
}

private void checkForHeaderSearch(HtmlPage page) {
private void checkForHeaderSearch(final HtmlPage page) {
final HtmlForm form = page.getFirstByXPath("//form[@role='search']");
assertNotNull(form);
assertEquals(serverAddress + "fcr:search", form.getActionAttribute());
Expand Down Expand Up @@ -194,7 +193,7 @@ private String createNewObject() throws IOException {

private WebClient getDefaultWebClient() {

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
webClient.addRequestHeader("Accept", "text/html");

webClient.waitForBackgroundJavaScript(1000);
Expand Down
Expand Up @@ -146,7 +146,7 @@ public void stop() {
} catch (final Exception e) {
logger.warn(e.getMessage(), e);
} finally {
server.stop();
server.shutdownNow();
}
}

Expand Down
Expand Up @@ -18,8 +18,7 @@

import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static java.net.URI.create;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.assertNotNull;
import static org.apache.jena.riot.WebContent.contentTypeToLang;
import static org.fcrepo.kernel.utils.ContentDigest.asURI;
import static org.mockito.Matchers.anyString;
Expand Down Expand Up @@ -58,7 +57,6 @@
import org.apache.http.util.EntityUtils;
import org.apache.jena.riot.Lang;
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.domain.RDFMediaType;
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.FedoraObject;
import org.fcrepo.kernel.identifiers.UUIDPidMinter;
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.fcrepo.kernel.utils;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.ImmutableSet.copyOf;
Expand Down Expand Up @@ -187,8 +188,7 @@ public Iterator<Value> apply(final Property p) {

@Override
public boolean apply(final Property p) {
checkArgument(p != null,
"null is neither multiple nor not multiple!");
checkNotNull(p, "null is neither multiple nor not multiple!");
try {
return p.isMultiple();
} catch (final RepositoryException e) {
Expand All @@ -205,8 +205,7 @@ public boolean apply(final Property p) {

@Override
public boolean apply(final Property p) {
checkArgument(p != null,
"null is neither binary nor not binary!");
checkNotNull(p, "null is neither binary nor not binary!");
try {
return p.getType() == BINARY && p.getName().equals(JCR_DATA);
} catch (final RepositoryException e) {
Expand All @@ -223,8 +222,7 @@ public boolean apply(final Property p) {

@Override
public boolean apply(final Node n) {
checkArgument(n != null,
"null is neither internal nor not internal!");
checkNotNull(n, "null is neither internal nor not internal!");
try {
final NodeType primaryNodeType = n.getPrimaryNodeType();
return primaryNodeType != null
Expand Down
Expand Up @@ -15,7 +15,7 @@
*/
package org.fcrepo.kernel.utils;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
Expand All @@ -41,9 +41,7 @@ public abstract class NamespaceTools {
@Override
public NamespaceRegistry apply(final Node n) {
try {
checkArgument(n != null,
"null has no Namespace Registry associated " +
"with it!");
checkNotNull(n, "null has no Namespace Registry associated with it!");
return (org.modeshape.jcr.api.NamespaceRegistry)n.getSession().getWorkspace().getNamespaceRegistry();
} catch (final RepositoryException e) {
throw new IllegalStateException(e);
Expand Down
Expand Up @@ -55,7 +55,6 @@
import org.fcrepo.kernel.services.DatastreamService;
import org.fcrepo.kernel.services.NodeService;
import org.fcrepo.kernel.services.ObjectService;
import org.fcrepo.kernel.utils.JcrRdfTools;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
Expand Down
Expand Up @@ -24,6 +24,7 @@
import static com.hp.hpl.jena.vocabulary.RDF.type;
import static com.hp.hpl.jena.vocabulary.RDFS.domain;
import static com.hp.hpl.jena.vocabulary.RDFS.label;
import static org.fcrepo.kernel.rdf.impl.mappings.ItemDefinitionToTriples.getResource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -80,8 +81,7 @@ public void testGoodDefinition() throws IOException, RepositoryException {
}

final Node subject =
testMapper.getResource((ItemDefinition) mockItemDefinition)
.asNode();
getResource((ItemDefinition) mockItemDefinition).asNode();
assertTrue(results.contains(create(subject, type.asNode(), Property
.asNode())));
assertTrue(results.contains(create(subject, label.asNode(),
Expand All @@ -101,7 +101,7 @@ public void testBadDefinition() throws RepositoryException {
public void testGetResourceFromNodeType() throws RepositoryException {
when(mockNodeType.getNamespaceURI()).thenReturn("namespace#");
when(mockNodeType.getLocalName()).thenReturn("localname");
final Resource answer = testMapper.getResource((NodeType) mockNodeType);
final Resource answer = getResource((NodeType) mockNodeType);
assertEquals("namespace#", answer.getNameSpace());
assertEquals("localname", answer.getLocalName());
}
Expand Down
Expand Up @@ -163,8 +163,8 @@ public void testIsMultipleValuedProperty() throws RepositoryException {
final Predicate<Property> test = isMultipleValuedProperty;
try {
test.apply(null);
fail("Null values should throw an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
fail("Null values should throw a NullPointerException");
} catch (final NullPointerException e) {
}
boolean actual = test.apply(mockYes);
assertEquals(true, actual);
Expand Down

0 comments on commit 26c4fbe

Please sign in to comment.