Search in sources :

Example 16 with MediaType

use of org.apache.jena.atlas.web.MediaType in project jena by apache.

the class TestQuery method query_construct_quad_conneg.

@Test
public void query_construct_quad_conneg() throws IOException {
    try (CloseableHttpClient client = HttpOp.createPoolingHttpClient()) {
        String queryString = " CONSTRUCT { GRAPH ?g {?s ?p ?o} } WHERE { GRAPH ?g {?s ?p ?o}}";
        Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
        for (MediaType type : quadsOfferTest.entries()) {
            String contentType = type.toHeaderString();
            try (QueryEngineHTTP qExec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(serviceQuery(), query)) {
                qExec.setDatasetContentType(contentType);
                qExec.setClient(client);
                Iterator<Quad> iter = qExec.execConstructQuads();
                assertTrue(iter.hasNext());
                String x = qExec.getHttpResponseContentType();
                assertEquals(contentType, x);
            }
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Quad(org.apache.jena.sparql.core.Quad) QueryEngineHTTP(org.apache.jena.sparql.engine.http.QueryEngineHTTP) ServerCtl.serviceQuery(org.apache.jena.fuseki.ServerCtl.serviceQuery) MediaType(org.apache.jena.atlas.web.MediaType) ServerTest(org.apache.jena.fuseki.ServerTest) Test(org.junit.Test)

Example 17 with MediaType

use of org.apache.jena.atlas.web.MediaType in project jena by apache.

the class TestQuery method query_construct_conneg.

// Conneg tests:
// These use independent connection pooling.
// Sharing pooling too much leads to lock up if the list is long (contentTypeTriXxml seems significant)
// Hence: try (CloseableHttpClient client = HttpOp.createPoolingHttpClient()) { ... qExec.setClient(client); ... } 
@Test
public void query_construct_conneg() throws IOException {
    try (CloseableHttpClient client = HttpOp.createPoolingHttpClient()) {
        String query = " CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}";
        for (MediaType type : rdfOfferTest.entries()) {
            String contentType = type.toHeaderString();
            try (QueryEngineHTTP qExec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(serviceQuery(), query)) {
                qExec.setModelContentType(contentType);
                qExec.setClient(client);
                Iterator<Triple> iter = qExec.execConstructTriples();
                assertTrue(iter.hasNext());
                String x = qExec.getHttpResponseContentType();
                assertEquals(contentType, x);
            }
        }
    }
}
Also used : Triple(org.apache.jena.graph.Triple) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) QueryEngineHTTP(org.apache.jena.sparql.engine.http.QueryEngineHTTP) MediaType(org.apache.jena.atlas.web.MediaType) ServerTest(org.apache.jena.fuseki.ServerTest) Test(org.junit.Test)

Example 18 with MediaType

use of org.apache.jena.atlas.web.MediaType in project jena by apache.

the class TestContentNegotiation method testMatch.

// HTTP: RDF
//See WebContent.defaultGraphAcceptHeader, defaultDatasetAcceptHeader, defaultRDFAcceptHeader
private void testMatch(String header, String offer, String result) {
    AcceptList list1 = new AcceptList(header);
    AcceptList list2 = new AcceptList(offer);
    MediaType matchItem = AcceptList.match(list1, list2);
    if (result == null) {
        assertNull("Match not null: from " + q(header) + " :: " + q(offer), matchItem);
        return;
    }
    assertNotNull("Match is null: expected " + q(result), matchItem);
    assertEquals("Match different", result, matchItem.toHeaderString());
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType) AcceptList(org.apache.jena.atlas.web.AcceptList)

Example 19 with MediaType

use of org.apache.jena.atlas.web.MediaType in project jena by apache.

the class SPARQL_UberServlet method executeAction.

/** Intercept the processing cycle at the point where the action has been set up,
     *  the dataset target decided but no validation or execution has been done,
     *  nor any stats have been done.
     */
@Override
protected void executeAction(HttpAction action) {
    // DEBUG: DataAccessPointRegistry.print("UberServlet ");
    long id = action.id;
    HttpServletRequest request = action.request;
    HttpServletResponse response = action.response;
    // No context path
    String actionURI = action.getActionURI();
    String method = request.getMethod();
    DataAccessPoint desc = action.getDataAccessPoint();
    DataService dSrv = action.getDataService();
    //        if ( ! dSrv.isActive() )
    //            ServletOps.error(HttpSC.SERVICE_UNAVAILABLE_503, "Dataset not currently active");
    // Part after the DataAccessPoint (dataset) name.
    String trailing = findTrailing(actionURI, desc.getName());
    String qs = request.getQueryString();
    boolean hasParams = request.getParameterMap().size() > 0;
    // Is it a query or update because of a ?query= , ?request= parameter? 
    // Test for parameters - includes HTML forms.
    boolean isQuery = request.getParameter(HttpNames.paramQuery) != null;
    // Include old name "request="
    boolean isUpdate = request.getParameter(HttpNames.paramUpdate) != null || request.getParameter(HttpNames.paramRequest) != null;
    boolean hasParamGraph = request.getParameter(HttpNames.paramGraph) != null;
    boolean hasParamGraphDefault = request.getParameter(HttpNames.paramGraphDefault) != null;
    boolean hasTrailing = (trailing.length() != 0);
    String ct = request.getContentType();
    String charset = request.getCharacterEncoding();
    MediaType mt = null;
    if (ct != null) {
        // Parse it.
        mt = MediaType.create(ct, charset);
        // Another way to send queries and updates is with the content-type. 
        if (contentTypeSPARQLQuery.equalsIgnoreCase(ct))
            isQuery = true;
        else if (contentTypeSPARQLUpdate.equalsIgnoreCase(ct))
            isUpdate = true;
    }
    if (action.log.isInfoEnabled()) {
        //String cxt = action.getContextPath() ;
        action.log.info(format("[%d] %s %s :: '%s' :: %s ? %s", id, method, desc.getName(), trailing, (mt == null ? "<none>" : mt), (qs == null ? "" : qs)));
    }
    if (!hasTrailing) {
        //   http://localhost:3030/ds   REST quads action on the dataset itself.
        if (isQuery) {
            if (!allowQuery(action))
                ServletOps.errorMethodNotAllowed("SPARQL query : " + method);
            executeRequest(action, queryServlet);
            return;
        }
        if (isUpdate) {
            // SPARQL Update
            if (!allowUpdate(action))
                ServletOps.errorMethodNotAllowed("SPARQL update : " + method);
            // This will deal with using GET.
            executeRequest(action, updateServlet);
            return;
        }
        // ?graph=, ?default
        if (hasParamGraph || hasParamGraphDefault) {
            doGraphStoreProtocol(action);
            return;
        }
        if (hasParams) {
            // Unrecognized ?key=value
            ServletOps.errorBadRequest("Malformed request");
        }
        // REST dataset.
        boolean isGET = method.equals(HttpNames.METHOD_GET);
        boolean isHEAD = method.equals(HttpNames.METHOD_HEAD);
        // Check enabled.
        if (isGET || isHEAD) {
            if (allowQuadsR(action))
                restQuads_R.executeLifecycle(action);
            else
                ServletOps.errorMethodNotAllowed(method);
            return;
        }
        if (allowQuadsW(action))
            restQuads_RW.executeLifecycle(action);
        else
            ServletOps.errorMethodNotAllowed("Read-only dataset : " + method);
        return;
    }
    // Has trailing path name => service or direct naming GSP.
    final boolean checkForPossibleService = true;
    if (checkForPossibleService && action.getEndpoint() != null) {
        // If so, dispatch to that service.
        if (serviceDispatch(action, OperationName.Query, queryServlet))
            return;
        if (serviceDispatch(action, OperationName.Update, updateServlet))
            return;
        if (serviceDispatch(action, OperationName.Upload, uploadServlet))
            return;
        if (hasParams) {
            if (serviceDispatch(action, OperationName.GSP_R, gspServlet_R))
                return;
            if (serviceDispatch(action, OperationName.GSP_RW, gspServlet_RW))
                return;
        } else {
            // No parameters - do as a quads operation on the dataset.
            if (serviceDispatch(action, OperationName.GSP_R, restQuads_R))
                return;
            if (serviceDispatch(action, OperationName.GSP_RW, restQuads_RW))
                return;
        }
        if (serviceDispatch(action, OperationName.Quads_RW, restQuads_RW))
            return;
        if (serviceDispatch(action, OperationName.Quads_R, restQuads_R))
            return;
    }
    // There is a trailing part - params are illegal by this point.
    if (hasParams)
        // ?? Revisit to include query-on-one-graph
        //errorBadRequest("Can't invoke a query-string service on a direct named graph") ;
        ServletOps.errorNotFound("Not found: dataset='" + printName(desc.getName()) + "' service='" + printName(trailing) + "' query string=?" + qs);
    // There is a trailing part - not a service, no params ==> GSP direct naming.
    if (!Fuseki.GSP_DIRECT_NAMING)
        ServletOps.errorNotFound("Not found: dataset='" + printName(desc.getName()) + "' service='" + printName(trailing) + "'");
    doGraphStoreProtocol(action);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) MediaType(org.apache.jena.atlas.web.MediaType)

Example 20 with MediaType

use of org.apache.jena.atlas.web.MediaType in project jena by apache.

the class SPARQL_REST_R method doHead.

@Override
protected void doHead(HttpAction action) {
    setCommonHeaders(action.response);
    action.beginRead();
    try {
        Target target = determineTarget(action);
        if (log.isDebugEnabled())
            log.debug("HEAD->" + target);
        if (!target.exists()) {
            successNotFound(action);
            return;
        }
        MediaType mediaType = HttpAction.contentNegotationRDF(action);
        success(action);
    } finally {
        action.endRead();
    }
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType)

Aggregations

MediaType (org.apache.jena.atlas.web.MediaType)23 ServletOutputStream (javax.servlet.ServletOutputStream)6 IOException (java.io.IOException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 TypedOutputStream (org.apache.jena.atlas.web.TypedOutputStream)4 Lang (org.apache.jena.riot.Lang)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)3 AcceptList (org.apache.jena.atlas.web.AcceptList)3 ServerTest (org.apache.jena.fuseki.ServerTest)3 JenaException (org.apache.jena.shared.JenaException)3 QueryEngineHTTP (org.apache.jena.sparql.engine.http.QueryEngineHTTP)3 Test (org.junit.Test)3 Graph (org.apache.jena.graph.Graph)2 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)2 ContentType (org.apache.jena.atlas.web.ContentType)1 MediaRange (org.apache.jena.atlas.web.MediaRange)1 FusekiException (org.apache.jena.fuseki.FusekiException)1 ServerCtl.serviceQuery (org.apache.jena.fuseki.ServerCtl.serviceQuery)1 Triple (org.apache.jena.graph.Triple)1