001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.camel.reindexing;
017
018import static org.slf4j.LoggerFactory.getLogger;
019
020import java.net.URL;
021import java.util.HashSet;
022import java.util.Iterator;
023import java.util.Set;
024
025import com.fasterxml.jackson.core.JsonProcessingException;
026import com.fasterxml.jackson.databind.JsonNode;
027import com.fasterxml.jackson.databind.ObjectMapper;
028
029import org.apache.camel.Exchange;
030import org.apache.camel.Message;
031import org.apache.camel.Processor;
032import org.fcrepo.camel.FcrepoHeaders;
033import org.slf4j.Logger;
034
035/**
036 * A processor that converts the REST uri into the
037 * identifying path for an fcrepo node.
038 *
039 * This assumes that the `rest.prefix` value is stored
040 * in the CamelFcrepoRestPrefix header.
041 *
042 * @author Aaron Coburn
043 */
044public class RestProcessor implements Processor {
045
046    private static final Logger LOGGER = getLogger(RestProcessor.class);
047
048    private static final int BAD_REQUEST = 400;
049
050    /**
051     * Convert the incoming REST request into the correct
052     * Fcrepo header fields.
053     *
054     * @param exchange the current message exchange
055     */
056    public void process(final Exchange exchange) throws Exception {
057        final Message in = exchange.getIn();
058
059        final URL url = new URL(in.getHeader(Exchange.HTTP_URI, String.class));
060        final String prefix = in.getHeader(ReindexingHeaders.REST_PREFIX, "", String.class);
061        final String contentType = in.getHeader(Exchange.CONTENT_TYPE, "", String.class);
062        final String body = in.getBody(String.class);
063        final Set<String> endpoints = new HashSet<>();
064
065        for (final String s : in.getHeader(ReindexingHeaders.RECIPIENTS, "", String.class).split(",")) {
066            endpoints.add(s.trim());
067        }
068
069        in.removeHeaders("CamelHttp*");
070        in.removeHeaders("CamelRestlet*");
071        in.removeHeaders("org.restlet*");
072        in.removeHeader("JMSCorrelationID");
073        in.setBody(null);
074
075        if (contentType.equals("application/json") && body != null && !body.trim().isEmpty()) {
076            final ObjectMapper mapper = new ObjectMapper();
077            try {
078                final JsonNode root = mapper.readTree(body);
079                final Iterator<JsonNode> ite = root.elements();
080                while (ite.hasNext()) {
081                    final JsonNode n = ite.next();
082                    endpoints.add(n.asText());
083                }
084            } catch (JsonProcessingException e) {
085                LOGGER.debug("Invalid JSON", e);
086                in.setHeader(Exchange.HTTP_RESPONSE_CODE, BAD_REQUEST);
087                in.setBody("Invalid JSON");
088            }
089        }
090
091        in.setHeader(FcrepoHeaders.FCREPO_IDENTIFIER, url.getPath().substring(prefix.length()));
092        in.setHeader(ReindexingHeaders.RECIPIENTS, String.join(",", endpoints));
093    }
094}