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 java.io.StringWriter;
021import java.net.InetAddress;
022import java.util.HashMap;
023import java.util.Map;
024
025import com.github.mustachejava.DefaultMustacheFactory;
026import com.github.mustachejava.Mustache;
027import com.github.mustachejava.MustacheFactory;
028
029import org.apache.camel.Exchange;
030import org.apache.camel.Message;
031import org.apache.camel.Processor;
032import org.fcrepo.camel.FcrepoHeaders;
033
034/**
035 * A processor to generate some useful documentation on the usage
036 * of this service.
037 *
038 * @author Aaron Coburn
039 */
040public class UsageProcessor implements Processor {
041
042    /**
043     * Convert the incoming REST request into some useful
044     * documentation.
045     *
046     * @param exchange the current message exchange
047     */
048    public void process(final Exchange exchange) throws Exception {
049        final Message in = exchange.getIn();
050        final MustacheFactory mf = new DefaultMustacheFactory();
051        final Map<String, Object> scopes = new HashMap<>();
052
053        scopes.put("fedora", in.getHeader(FcrepoHeaders.FCREPO_BASE_URL, "", String.class));
054        scopes.put("reindexing", InetAddress.getLocalHost().getHostName() + ":" +
055                in.getHeader(ReindexingHeaders.REST_PORT, "", String.class) +
056                in.getHeader(ReindexingHeaders.REST_PREFIX, "", String.class));
057
058        final Mustache mustache = mf.compile("usage.mustache");
059        final StringWriter sw = new StringWriter();
060
061        mustache.execute(sw, scopes).close();
062        sw.flush();
063        in.setBody(sw.toString());
064
065    }
066}