<commit_before>/**
 * Copyright 2015 DuraSpace, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fcrepo.camel.audit.triplestore;

import static org.fcrepo.camel.RdfNamespaces.REPOSITORY;
import static org.fcrepo.camel.RdfNamespaces.RDF;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;

import org.apache.camel.Processor;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.clerezza.rdf.core.Triple;
import org.apache.clerezza.rdf.core.UriRef;
import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
import org.apache.clerezza.rdf.core.impl.TripleImpl;
import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;
import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
import org.apache.clerezza.rdf.core.serializedform.SerializingProvider;
import org.apache.clerezza.rdf.jena.parser.JenaParserProvider;
import org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider;

import org.fcrepo.camel.JmsHeaders;
import org.fcrepo.camel.processor.ProcessorUtils;


/**
 * A processor that converts an audit message into a sparql-update
 * statement for an external triplestore.
 *
 * @author <NAME>
 * @author escowles
 * @since 2015-04-09
 */

public class AuditSparqlProcessor implements Processor {

    /**
     * Define how a message should be processed.
     *
     * @param exchange the current camel message exchange
     */
    public void process(final Exchange exchange) throws Exception {
        final Message in = exchange.getIn();
        final UriRef eventURI = new UriRef("XXX");
        final Set<Triple> triples = triplesForMessage(in, eventURI);

        // serialize triples
        final SerializingProvider serializer = new JenaSerializerProvider();
        final ByteArrayOutputStream serializedGraph = new ByteArrayOutputStream();
        serializer.serialize(serializedGraph, new SimpleMGraph(triples), "text/rdf+nt");

        // generate SPARQL Update
        final StringBuilder query = new StringBuilder("update=");
        query.append(ProcessorUtils.insertData(serializedGraph.toString("UTF-8"), null));

        // update exchange
        in.setBody(query.toString());
        in.setHeader(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded");
        in.setHeader(Exchange.HTTP_METHOD, "POST");
    }

    // namespaces and properties
    private static final String AUDIT = "http://fedora.info/definitions/v4/audit#";
    private static final String PREMIS = "http://www.loc.gov/premis/rdf/v1#";
    private static final String EVENT_TYPE = "http://id.loc.gov/vocabulary/preservation/eventType/";
    private static final String PROV = "http://www.w3.org/ns/prov#";
    private static final String XSD = "http://www.w3.org/2001/XMLSchema#";

    private static final UriRef INTERNAL_EVENT = new UriRef(AUDIT + "InternalEvent");
    private static final UriRef PREMIS_EVENT = new UriRef(PREMIS + "Event");
    private static final UriRef PROV_EVENT = new UriRef(PROV + "InstantaneousEvent");

    private static final UriRef CONTENT_MOD = new UriRef(AUDIT + "contentModification");
    private static final UriRef CONTENT_REM = new UriRef(AUDIT + "contentRemoval");
    private static final UriRef METADATA_MOD = new UriRef(AUDIT + "metadataModification");

    private static final UriRef CONTENT_ADD = new UriRef(EVENT_TYPE + "ing");
    private static final UriRef OBJECT_ADD = new UriRef(EVENT_TYPE + "cre");
    private static final UriRef OBJECT_REM = new UriRef(EVENT_TYPE + "del");

    private static final UriRef PREMIS_TIME = new UriRef(PREMIS + "hasEventDateTime");
    private static final UriRef PREMIS_OBJ = new UriRef(PREMIS + "hasEventRelatedObject");
    private static final UriRef PREMIS_AGENT = new UriRef(PREMIS + "hasEventRelatedAgent");
    private static final UriRef PREMIS_TYPE = new UriRef(PREMIS + "hasEventType");

    private static final UriRef RDF_TYPE = new UriRef(RDF + "type");
    private static final UriRef XSD_DATE = new UriRef(XSD + "dateTime");
    private static final UriRef XSD_STRING = new UriRef(XSD + "string");

    private static final String HAS_CONTENT = REPOSITORY + "hasContent";
    private static final String LAST_MODIFIED = REPOSITORY + "lastModified";
    private static final String NODE_ADDED = REPOSITORY + "NODE_ADDED";
    private static final String NODE_REMOVED = REPOSITORY + "NODE_REMOVED";
    private static final String PROPERTY_CHANGED = REPOSITORY + "PROPERTY_CHANGED";

    /**
     * Convert a Camel message to audit event description.
     * @param m JMS message produced by an audit event
     * @param subject RDF subject of the audit description
     */
    private static Set<Triple> triplesForMessage(Message message, UriRef subject) throws IOException {

        // get info from jms message headers
        final String eventType = message.getHeader(JmsHeaders.EVENT_TYPE, String.class);
        final Long timestamp = Long.parseLong(message.getHeader(JmsHeaders.TIMESTAMP, String.class), 10);
        final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        final String date = df.format(new Date(timestamp));
        final String user = message.getHeader(JmsHeaders.USER, String.class);
        final String agent = message.getHeader(JmsHeaders.USER_AGENT, String.class);
        final String properties = message.getHeader(JmsHeaders.PROPERTIES, String.class);
        final String identifier = ProcessorUtils.getSubjectUri(message);

        // types
        Set<Triple> triples = new HashSet<>();
        triples.add( new TripleImpl(subject, RDF_TYPE, INTERNAL_EVENT) );
        triples.add( new TripleImpl(subject, RDF_TYPE, PREMIS_EVENT) );
        triples.add( new TripleImpl(subject, RDF_TYPE, PROV_EVENT) );

        // basic event info<commit_msg>FCREPO-1451. Set header values to empty string instead of null.<commit_after>/**
 * Copyright 2015 DuraSpace, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fcrepo.camel.audit.triplestore;

import static org.fcrepo.camel.RdfNamespaces.REPOSITORY;
import static org.fcrepo.camel.RdfNamespaces.RDF;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;

import org.apache.camel.Processor;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.clerezza.rdf.core.Triple;
import org.apache.clerezza.rdf.core.UriRef;
import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
import org.apache.clerezza.rdf.core.impl.TripleImpl;
import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;
import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
import org.apache.clerezza.rdf.core.serializedform.SerializingProvider;
import org.apache.clerezza.rdf.jena.parser.JenaParserProvider;
import org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider;

import org.fcrepo.camel.JmsHeaders;
import org.fcrepo.camel.processor.ProcessorUtils;


/**
 * A processor that converts an audit message into a sparql-update
 * statement for an external triplestore.
 *
 * @author <NAME>
 * @author escowles
 * @since 2015-04-09
 */

public class AuditSparqlProcessor implements Processor {

    /**
     * Define how a message should be processed.
     *
     * @param exchange the current camel message exchange
     */
    public void process(final Exchange exchange) throws Exception {
        final Message in = exchange.getIn();
        final UriRef eventURI = new UriRef("XXX");
        final Set<Triple> triples = triplesForMessage(in, eventURI);

        // serialize triples
        final SerializingProvider serializer = new JenaSerializerProvider();
        final ByteArrayOutputStream serializedGraph = new ByteArrayOutputStream();
        serializer.serialize(serializedGraph, new SimpleMGraph(triples), "text/rdf+nt");

        // generate SPARQL Update
        final StringBuilder query = new StringBuilder("update=");
        query.append(ProcessorUtils.insertData(serializedGraph.toString("UTF-8"), null));

        // update exchange
        in.setBody(query.toString());
        in.setHeader(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded");
        in.setHeader(Exchange.HTTP_METHOD, "POST");
    }

    // namespaces and properties
    private static final String AUDIT = "http://fedora.info/definitions/v4/audit#";
    private static final String PREMIS = "http://www.loc.gov/premis/rdf/v1#";
    private static final String EVENT_TYPE = "http://id.loc.gov/vocabulary/preservation/eventType/";
    private static final String PROV = "http://www.w3.org/ns/prov#";
    private static final String XSD = "http://www.w3.org/2001/XMLSchema#";

    private static final UriRef INTERNAL_EVENT = new UriRef(AUDIT + "InternalEvent");
    private static final UriRef PREMIS_EVENT = new UriRef(PREMIS + "Event");
    private static final UriRef PROV_EVENT = new UriRef(PROV + "InstantaneousEvent");

    private static final UriRef CONTENT_MOD = new UriRef(AUDIT + "contentModification");
    private static final UriRef CONTENT_REM = new UriRef(AUDIT + "contentRemoval");
    private static final UriRef METADATA_MOD = new UriRef(AUDIT + "metadataModification");

    private static final UriRef CONTENT_ADD = new UriRef(EVENT_TYPE + "ing");
    private static final UriRef OBJECT_ADD = new UriRef(EVENT_TYPE + "cre");
    private static final UriRef OBJECT_REM = new UriRef(EVENT_TYPE + "del");

    private static final UriRef PREMIS_TIME = new UriRef(PREMIS + "hasEventDateTime");
    private static final UriRef PREMIS_OBJ = new UriRef(PREMIS + "hasEventRelatedObject");
    private static final UriRef PREMIS_AGENT = new UriRef(PREMIS + "hasEventRelatedAgent");
    private static final UriRef PREMIS_TYPE = new UriRef(PREMIS + "hasEventType");

    private static final UriRef RDF_TYPE = new UriRef(RDF + "type");
    private static final UriRef XSD_DATE = new UriRef(XSD + "dateTime");
    private static final UriRef XSD_STRING = new UriRef(XSD + "string");

    private static final String HAS_CONTENT = REPOSITORY + "hasContent";
    private static final String LAST_MODIFIED = REPOSITORY + "lastModified";
    private static final String NODE_ADDED = REPOSITORY + "NODE_ADDED";
    private static final String NODE_REMOVED = REPOSITORY + "NODE_REMOVED";
    private static final String PROPERTY_CHANGED = REPOSITORY + "PROPERTY_CHANGED";

    private static final String EMPTY_STRING = "";

    /**
     * Convert a Camel message to audit event description.
     * @param message JMS message produced by an audit event
     * @param subject RDF subject of the audit description
     */
    private static Set<Triple> triplesForMessage(Message message, UriRef subject) throws IOException {

        // get info from jms message headers
        final String eventType = (String) message.getHeader(JmsHeaders.EVENT_TYPE, EMPTY_STRING);
        final Long timestamp = Long.parseLong((String) message.getHeader(JmsHeaders.TIMESTAMP, EMPTY_STRING), 10);
        final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        final String date = df.format(new Date(timestamp));
        final String user = (String) message.getHeader(JmsHeaders.USER, EMPTY_STRING);
        final String agent = (String) message.getHeader(JmsHeaders.USER_AGENT, EMPTY_STRING);
        final String properties = (String) message.getHeader(JmsHeaders.PROPERTIES, EMPTY_STRING);
        final String identifier = ProcessorUtils.getSubjectUri(message);

        // types
        Set<Triple> triples = new HashSet<>();
         triples.add( new TripleImpl(subject, RDF_TYPE, INTERNAL_EVENT) );
        triples.add( new TripleImpl(subject, RDF_TYPE, PREMIS_EVENT) );
        triples.add( new TripleImpl(subject, RDF_TYPE, PROV_EVENT) );

        // basic event info
{'fraction_non_alphanumeric': 0.09655381812661303, 'fraction_numerical': 0.004402611203886443, 'mean_word_length': 4.005699088145897, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1895779', 'n_tokens_mistral': 4048, 'n_tokens_neox': 3846, 'n_words': 1076}