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.processor;
017
018import static com.hp.hpl.jena.util.URIref.encode;
019import java.io.IOException;
020
021import org.apache.camel.Message;
022import org.apache.commons.lang3.StringUtils;
023import org.fcrepo.camel.FcrepoHeaders;
024import org.fcrepo.camel.JmsHeaders;
025
026/**
027 * Utility functions for fcrepo processor classes
028 * @author Aaron Coburn
029 * @since November 14, 2014
030 */
031
032public final class ProcessorUtils {
033
034    /**
035     * This is a utility class; the constructor is off-limits.
036     */
037    private ProcessorUtils() {
038    }
039
040    private static String trimTrailingSlash(final String path) {
041        String trimmed = path;
042        while (trimmed.endsWith("/")) {
043            trimmed = trimmed.substring(0, trimmed.length() - 1);
044        }
045        return trimmed;
046    }
047
048    /**
049     * Extract a language string suitable for Jena from a MimeType value
050     * @param contentType a MIMEType string
051     * @return a Jena-compatible language string
052     */
053    public static String langFromMimeType(final String contentType) {
054        if (contentType.equals("application/n-triples")) {
055            return "N-TRIPLE";
056        } else if (contentType.equals("text/turtle")) {
057            return "TURTLE";
058        } else if (contentType.equals("application/rdf+xml")) {
059            return "RDF/XML";
060        } else if (contentType.equals("text/rdf+n3") || contentType.equals("text/n3")) {
061            return "N3";
062        } else {
063            return null;
064        }
065    }
066
067    /**
068     * Extract the subject URI from the incoming message headers.
069     * @param in the incoming Message
070     * @return the subject URI
071     * @throws IOException when no baseURL header is present
072     */
073    public static String getSubjectUri(final Message in) throws IOException {
074        final StringBuilder base = new StringBuilder("");
075
076        if (in.getHeader(FcrepoHeaders.FCREPO_BASE_URL) != null) {
077            base.append(trimTrailingSlash(in.getHeader(FcrepoHeaders.FCREPO_BASE_URL, String.class)));
078        } else if (in.getHeader(JmsHeaders.BASE_URL) != null) {
079            base.append(trimTrailingSlash(in.getHeader(JmsHeaders.BASE_URL, String.class)));
080        } else {
081            throw new IOException("No baseURL header available!");
082        }
083
084        if (in.getHeader(FcrepoHeaders.FCREPO_IDENTIFIER) != null) {
085           base.append(in.getHeader(FcrepoHeaders.FCREPO_IDENTIFIER, String.class));
086        } else if (in.getHeader(JmsHeaders.IDENTIFIER) != null) {
087           base.append(in.getHeader(JmsHeaders.IDENTIFIER, String.class));
088        }
089        return base.toString();
090    }
091
092    /**
093     * Create a DELETE WHERE { ... } statement from the provided subject
094     *
095     * @param subject the subject of the triples to delete.
096     * @param namedGraph an optional named graph
097     * @return the delete statement
098     */
099    public static String deleteWhere(final String subject, final String namedGraph) {
100        final StringBuilder stmt = new StringBuilder("DELETE WHERE { ");
101
102        if (!StringUtils.isBlank(namedGraph)) {
103            stmt.append("GRAPH ");
104            stmt.append("<" + encode(namedGraph) + ">");
105            stmt.append(" { ");
106        }
107
108        stmt.append("<" + encode(subject) + ">");
109        stmt.append(" ?p ?o ");
110
111        if (!StringUtils.isBlank(namedGraph)) {
112            stmt.append("} ");
113        }
114
115        stmt.append("}");
116        return stmt.toString();
117    }
118
119    /**
120     *  Create an INSERT DATA { ... } update query with the provided ntriples
121     *
122     *  @param serializedGraph the triples to insert
123     *  @param namedGraph an optional named graph
124     *  @return the insert statement
125     */
126    public static String insertData(final String serializedGraph, final String namedGraph) {
127        final StringBuilder query = new StringBuilder("INSERT DATA { ");
128
129        if (!StringUtils.isBlank(namedGraph)) {
130            query.append("GRAPH <");
131            query.append(encode(namedGraph));
132            query.append("> { ");
133        }
134
135        query.append(serializedGraph);
136
137        if (!StringUtils.isBlank(namedGraph)) {
138            query.append("} ");
139        }
140
141        query.append("}");
142        return query.toString();
143    }
144}
145