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.ldpath;
019
020import static org.apache.camel.builder.PredicateBuilder.and;
021import static org.apache.camel.model.dataformat.JsonLibrary.Jackson;
022import static org.apache.camel.Exchange.CONTENT_TYPE;
023import static org.apache.camel.Exchange.HTTP_METHOD;
024import static org.apache.camel.Exchange.HTTP_URI;
025import static org.slf4j.LoggerFactory.getLogger;
026
027import org.apache.camel.builder.RouteBuilder;
028import org.slf4j.Logger;
029
030/**
031 * A content router for an LDPath service.
032 *
033 * @author Aaron Coburn
034 * @since Aug 5, 2016
035 */
036public class LDPathRouter extends RouteBuilder {
037
038    private static final Logger LOGGER = getLogger(LDPathRouter.class);
039
040    public static final String FEDORA_URI = "CamelFedoraUri";
041
042    /**
043     * Configure the message route workflow.
044     */
045    public void configure() throws Exception {
046
047        /**
048         * Expose a RESTful endpoint for LDPath processing
049         */
050        from("jetty:http://{{rest.host}}:{{rest.port}}{{rest.prefix}}" +
051                "?matchOnUriPrefix=true" +
052                "&httpMethodRestrict=GET,POST,OPTIONS" +
053                "&sendServerVersion=false")
054            .routeId("FcrepoLDPathRest")
055            .routeDescription("Expose the ldpath endpoint over HTTP")
056            .setHeader(FEDORA_URI).simple("{{fcrepo.baseUrl}}${headers.CamelHttpPath}")
057            .choice()
058                .when(header(HTTP_METHOD).isEqualTo("GET"))
059                    .to("direct:get")
060                .when(header(HTTP_METHOD).isEqualTo("POST"))
061                    .to("direct:ldpathPrepare")
062                .when(header(HTTP_METHOD).isEqualTo("OPTIONS"))
063                    .setHeader(CONTENT_TYPE).constant("text/turtle")
064                    .setHeader("Allow").constant("GET,POST,OPTIONS")
065                    .to("language:simple:resource:classpath:options.ttl");
066
067        from("direct:get")
068            .routeId("FcrepoLDPathGet")
069            .choice()
070                .when(and(header("ldpath").isNotNull(), header("ldpath").regex("^https?://.*")))
071                    .removeHeaders("CamelHttp*")
072                    .setHeader(HTTP_URI).header("ldpath")
073                    .to("http4://localhost?useSystemProperties=true")
074                    .to("direct:ldpathPrepare")
075                .otherwise()
076                    .to("language:simple:resource:classpath:default.ldpath")
077                    .to("direct:ldpathPrepare");
078
079        from("direct:ldpathPrepare").routeId("FcrepoLDPathPrepare")
080            .to("direct:ldpath")
081            .to("direct:format");
082
083        from("direct:format").routeId("FcrepoLDPathFormat")
084            .marshal().json(Jackson)
085            .removeHeaders("*")
086            .setHeader(CONTENT_TYPE).constant("application/json");
087    }
088}