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