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