Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Set the Accept header to application/json when using the transform op…
…tion

- Remove explicit Accept headers in transform routes
- Update documentation for using the transform endpoint

Resolves: https://www.pivotaltracker.com/story/show/83405036
  • Loading branch information
acoburn authored and Andrew Woods committed Nov 25, 2014
1 parent 5bf5a35 commit 04247cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
10 changes: 3 additions & 7 deletions README.md
Expand Up @@ -24,7 +24,7 @@ FcrepoEndpoint options
| `contentType` | `null` | Set the `Content-Type` header |
| `accept` | `null` | Set the `Accept` header for content negotiation |
| `metadata` | `true` | Whether GET requests should retrieve RDF descriptions of non-RDF content |
| `transform` | `null` | If set, this defines the transform used for the given object. This should be used in the context of GET or POST. For GET requests, the value should be the name of the transform (e.g. `default`). For POST requests, the value can simply be `true`. |
| `transform` | `null` | If set, this defines the transform used for the given object. This should be used in the context of GET or POST. For GET requests, the value should be the name of the transform (e.g. `default`). For POST requests, the value can simply be `true`. Using this causes the `Accept` header to be set as `application/json`. |
| `throwExceptionOnFailure` | `true` | Option to disable throwing the HttpOperationFailedException in case of failed responses from the remote server. This allows you to get all responses regardless of the HTTP status code. |


Expand All @@ -39,8 +39,7 @@ A simple example for sending messages to an external Solr service:
from("activemq:topic:fedora")
.to("fcrepo:localhost:8080/rest")
.filter(xpath)
.to("fcrepo:localhost:8080/rest?accept=application/json&transform=mytransform")
.setHeader(Exchange.CONTENT_TYPE).constant("application/json")
.to("fcrepo:localhost:8080/rest?transform=mytransform")
.to("http4:solr-host:8080/solr/core/update")

Or, using the Spring DSL:
Expand All @@ -50,10 +49,7 @@ Or, using the Spring DSL:
<to uri="fcrepo:localhost:8080/rest"/>
<filter>
<xpath>/rdf:RDF/rdf:Description/rdf:type[@rdf:resource='http://fedora.info/definitions/v4/repository#Indexable']</xpath>
<to uri="fcrepo:localhost:8080/rest?accept=application/json&amp;transform=mytransform"/>
<setHeader headerName="Exchange.CONTENT_TYPE">
<constant>application/json</constant>
</setHeader>
<to uri="fcrepo:localhost:8080/rest?transform=mytransform"/>
<to uri="http4:solr-host:8080/solr/core/update"/>
</filter>
</route>
Expand Down
36 changes: 19 additions & 17 deletions src/main/java/org/fcrepo/camel/FedoraProducer.java
Expand Up @@ -140,11 +140,12 @@ protected URI getMetadataUri(final FedoraClient client, final String url)
* @param exchange the incoming message exchange
*/
protected HttpMethods getMethod(final Exchange exchange) {
HttpMethods method = exchange.getIn().getHeader(HTTP_METHOD, HttpMethods.class);
final HttpMethods method = exchange.getIn().getHeader(HTTP_METHOD, HttpMethods.class);
if (method == null) {
method = HttpMethods.GET;
return HttpMethods.GET;
} else {
return method;
}
return method;
}

/**
Expand All @@ -155,35 +156,36 @@ protected HttpMethods getMethod(final Exchange exchange) {
*/
protected String getContentType(final Exchange exchange) {
final String contentTypeString = ExchangeHelper.getContentType(exchange);
String contentType = null;
if (endpoint.getContentType() != null) {
contentType = endpoint.getContentType();
return endpoint.getContentType();
} else if (contentTypeString != null) {
contentType = contentTypeString;
return contentTypeString;
} else {
return null;
}
return contentType;
}

/**
* Given an exchange, extract the accept value for use with an Accept header. The order of preference is: 1) an
* accept value set on the endpoint 2) a value set on the Exchange.ACCEPT_CONTENT_TYPE header 3) a value set on an
* "Accept" header 4) the endpoint DEFAULT_CONTENT_TYPE (i.e. application/rdf+xml)
* Given an exchange, extract the accept value for use with an Accept header. The order of preference is:
* 1) whether a transform is being requested 2) an accept value is set on the endpoint 3) a value set on
* the Exchange.ACCEPT_CONTENT_TYPE header 4) a value set on an "Accept" header 5) the endpoint
* DEFAULT_CONTENT_TYPE (i.e. application/rdf+xml)
*
* @param exchange the incoming message exchange
*/
protected String getAccept(final Exchange exchange) {
String accept;
final Message in = exchange.getIn();
if (endpoint.getAccept() != null) {
accept = endpoint.getAccept();
if (endpoint.getTransform() != null) {
return "application/json";
} else if (endpoint.getAccept() != null) {
return endpoint.getAccept();
} else if (in.getHeader(ACCEPT_CONTENT_TYPE, String.class) != null) {
accept = in.getHeader(ACCEPT_CONTENT_TYPE, String.class);
return in.getHeader(ACCEPT_CONTENT_TYPE, String.class);
} else if (in.getHeader("Accept", String.class) != null) {
accept = in.getHeader("Accept", String.class);
return in.getHeader("Accept", String.class);
} else {
accept = DEFAULT_CONTENT_TYPE;
return DEFAULT_CONTENT_TYPE;
}
return accept;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/test/java/org/fcrepo/camel/FedoraTransformTest.java
Expand Up @@ -17,7 +17,6 @@

import static org.apache.camel.Exchange.HTTP_METHOD;
import static org.apache.camel.Exchange.CONTENT_TYPE;
import static org.apache.camel.Exchange.ACCEPT_CONTENT_TYPE;
import static org.fcrepo.camel.FedoraEndpoint.FCREPO_IDENTIFIER;
import static org.fcrepo.camel.integration.FedoraTestUtils.getFcrepoBaseUrl;
import static org.fcrepo.camel.integration.FedoraTestUtils.getFcrepoEndpointUri;
Expand Down Expand Up @@ -78,13 +77,11 @@ public void testTransform() throws InterruptedException {
headers.put(FCREPO_IDENTIFIER, identifier);
headers.put(CONTENT_TYPE, "application/rdf+ldpath");
headers.put(HTTP_METHOD, "POST");
headers.put(ACCEPT_CONTENT_TYPE, "application/json");
template.sendBodyAndHeaders("direct:post", ldpath, headers);

headers.clear();
headers.put(FCREPO_IDENTIFIER, identifier);
headers.put(HTTP_METHOD, "GET");
headers.put(ACCEPT_CONTENT_TYPE, "application/json");
template.sendBodyAndHeaders("direct:get", null, headers);


Expand Down Expand Up @@ -112,7 +109,7 @@ public void configure() {
.to(fcrepo_uri);

from("direct:start")
.to(fcrepo_uri + "?accept=application/json&transform=default")
.to(fcrepo_uri + "?accept=application/foo&transform=default")
.to("mock:result");

from("direct:get")
Expand Down

0 comments on commit 04247cf

Please sign in to comment.