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.builder.PredicateBuilder.not;
022import static org.apache.camel.model.dataformat.JsonLibrary.Jackson;
023import static org.apache.camel.Exchange.CONTENT_TYPE;
024import static org.apache.camel.Exchange.HTTP_METHOD;
025import static org.apache.camel.Exchange.HTTP_RESPONSE_CODE;
026import static org.apache.camel.Exchange.HTTP_URI;
027import static org.slf4j.LoggerFactory.getLogger;
028
029import org.apache.camel.builder.RouteBuilder;
030import org.slf4j.Logger;
031
032/**
033 * A content router for an LDPath service.
034 *
035 * @author Aaron Coburn
036 * @since Aug 5, 2016
037 */
038public class LDPathRouter extends RouteBuilder {
039
040    private static final Logger LOGGER = getLogger(LDPathRouter.class);
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                "&httpMethodRestrict=GET,POST,OPTIONS" +
052                "&sendServerVersion=false")
053            .routeId("FcrepoLDPathRest")
054            .routeDescription("Expose the ldpath endpoint over HTTP")
055            .choice()
056                .when(header(HTTP_METHOD).isEqualTo("OPTIONS"))
057                    .setHeader(CONTENT_TYPE).constant("text/turtle")
058                    .setHeader("Allow").constant("GET,POST,OPTIONS")
059                    .to("language:simple:resource:classpath:org/fcrepo/camel/ldpath/options.ttl")
060                // make sure the required context parameter is present
061                .when(not(and(header("context").isNotNull(), header("context").regex("^https?://.+"))))
062                    .setHeader(HTTP_RESPONSE_CODE).constant(400)
063                    .setHeader(CONTENT_TYPE).constant("text/plain")
064                    .transform(constant("Missing context parameter"))
065                .when(header(HTTP_METHOD).isEqualTo("GET"))
066                    .to("direct:get")
067                .when(header(HTTP_METHOD).isEqualTo("POST"))
068                    .to("direct:ldpathPrepare");
069
070        from("direct:get")
071            .routeId("FcrepoLDPathGet")
072            .choice()
073                .when(and(header("ldpath").isNotNull(), header("ldpath").regex("^https?://.*")))
074                    .removeHeaders("CamelHttp*")
075                    .setHeader(HTTP_URI).header("ldpath")
076                    .to("http4://localhost?useSystemProperties=true")
077                    .to("direct:ldpathPrepare")
078                .otherwise()
079                    .to("language:simple:resource:classpath:org/fcrepo/camel/ldpath/default.ldpath")
080                    .to("direct:ldpathPrepare");
081
082        from("direct:ldpathPrepare").routeId("FcrepoLDPathPrepare")
083            .to("direct:ldpath")
084            .to("direct:format");
085
086        from("direct:format").routeId("FcrepoLDPathFormat")
087            .marshal().json(Jackson)
088            .removeHeaders("*")
089            .setHeader(CONTENT_TYPE).constant("application/json");
090    }
091}