/******************************************************************
 * File:        BaseEndpoint.java
 * Created by:  Dave Reynolds
 * Created on:  29 Nov 2012
 *
 * (c) Copyright 2012, Epimorphics Limited
 *
 *****************************************************************/

package com.epimorphics.server.webapi;

import static com.epimorphics.webapi.marshalling.RDFXMLMarshaller.MIME_RDFXML;

import java.io.InputStream;

import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import com.epimorphics.server.core.Service;
import com.epimorphics.server.core.ServiceConfig;
import com.epimorphics.server.core.Store;
import com.epimorphics.server.indexers.LuceneIndex;
import com.epimorphics.util.EpiException;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileUtils;

/**
 * Useful base class from which Jersey RDF endpoints can inherit.
 *
 * @author <a href="mailto:dave@epimorphics.com">Dave Reynolds</a>
 */
public class BaseEndpoint {

    public static final String MIME_TURTLE = "text/turtle";

    public static final String DUMMY_BASE_URI = "http://dummy.com";

    protected @Context ServletContext context;
    protected @Context UriInfo uriInfo;

    /**
     * Load an RDF payload from a POST/PUT request. Returns null if it doesn't understand
     * the MIME type so that subclasses can extend it.
     */
    public Model getBodyModel(HttpHeaders hh, InputStream body) {
        if (hh.getMediaType() == null) return null;
        String mime = hh.getMediaType().toString();
        String lang = null;
        if ( MIME_RDFXML.equals( mime ) ) {
            lang = FileUtils.langXML;
        } else if ( MIME_TURTLE.equals( mime ) ) {
            lang = FileUtils.langTurtle;
        } else {
            return null;
        }
        Model m = ModelFactory.createDefaultModel();
        m.read(body, DUMMY_BASE_URI, lang);
        return m;
    }


    /**
     * Load an RDF payload from a POST/PUT request.
     * Throw an error if no acceptable RDF payload
     */
    public Model getSafeBodyModel(HttpHeaders hh, InputStream body) {
        Model m = getBodyModel(hh, body);
        if (m == null) {
            throw new WebApiException(Response.Status.UNSUPPORTED_MEDIA_TYPE, "Non-RDF mime type found in request");
        } else {
            return m;
        }
    }

    /**
     * Return the default store, error if there is no such store configured
     */
    public Store getDefaultStore() {
        Store store = ServiceConfig.get().getDefaultStore();
        if (store == null) {
            throw new WebApiException(Response.Status.INTERNAL_SERVER_ERROR, "No default store configured");
        } else {
            return store;
        }
    }

    /**
     * Utility which treats the request body as an RDF model
     * and adds it to the a named graph within the default store.
     */
    public void putToDefaultStore(HttpHeaders hh, InputStream body, String graphname) {
        getDefaultStore().updateGraph(graphname, getSafeBodyModel(hh, body));
    }

    /**
     * Find the given lucene index
     */
    public LuceneIndex getIndex(String name) {
        Service s = ServiceConfig.get().getService(name);
        if (s != null && s instanceof LuceneIndex) {
            return (LuceneIndex)s;
        } else {
            throw new EpiException("Can't find indexer");
        }
    }

}

@@ -449,32 +449,117 @@
 ServletContext;%0A
+import javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpSession;%0A
 import javax.ws.
@@ -1336,24 +1336,156 @@
 ummy.com%22;%0A%0A
+    public static final String SESSION_USER_KEY = %22user%22;%0A    public static final String FORWARDED_FOR_HEADER = %22X-Forwarded-For%22;%0A%0A
     protecte
@@ -1558,16 +1558,67 @@
 uriInfo;
+%0A    protected @Context HttpServletRequest request;
 %0A%0A    /*
@@ -3788,10 +3788,559 @@
 %0A    %7D%0A%0A
+    /**%0A     * FInd the name or IP address of the originator of this request%0A     */%0A    public String getRequestor() %7B%0A        HttpSession session = request.getSession(false);%0A        if (session != null) %7B%0A            Object user = session.getAttribute(SESSION_USER_KEY);%0A            if (user != null) %7B%0A                return (String)user;%0A            %7D%0A        %7D%0A%0A        if (request.getHeader(FORWARDED_FOR_HEADER) != null) %7B%0A            return request.getHeader(FORWARDED_FOR_HEADER);%0A        %7D%0A%0A        return request.getRemoteAddr();%0A    %7D%0A%0A
 %7D%0A

Support for access to requestor/user for logging purposes