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