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.processor;
019
020import static java.net.URLEncoder.encode;
021import static org.apache.camel.Exchange.CONTENT_TYPE;
022import static org.apache.camel.Exchange.HTTP_METHOD;
023import static org.fcrepo.camel.processor.ProcessorUtils.getSubjectUri;
024
025import java.io.IOException;
026
027import org.apache.camel.Exchange;
028import org.apache.camel.NoSuchHeaderException;
029import org.apache.camel.Processor;
030
031/**
032 * Represents a Processor class that formulates a Sparql DESCRIBE query
033 * that is ready to be POSTed to a Sparql endpoint.
034 *
035 * The processor expects the following headers:
036 *      FCREPO_URI
037 *      * or *
038 *      FCREPO_BASE_URL
039 *      FCREPO_IDENTIFIER
040 *
041 * @author Aaron Coburn
042 * @since November 6, 2014
043 */
044public class SparqlDescribeProcessor implements Processor {
045    /**
046     *  Define how this message should be processed
047     *
048     *  @param exchange the current camel message exchange
049     */
050    public void process(final Exchange exchange) throws IOException, NoSuchHeaderException {
051        final String subject = getSubjectUri(exchange);
052
053        exchange.getIn().setBody("query=" + encode("DESCRIBE <" + subject + ">", "UTF-8"));
054        exchange.getIn().setHeader(HTTP_METHOD, "POST");
055        exchange.getIn().setHeader(CONTENT_TYPE, "application/x-www-form-urlencoded; charset=utf-8");
056    }
057}