org.apache.jena.riot.WebContent Java Examples

The following examples show how to use org.apache.jena.riot.WebContent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TripleIndexCreatorContext.java    From AGDISTIS with GNU Affero General Public License v3.0 6 votes vote down vote up
public String sparql(String subject) {

		// First query takes the most specific class from a given resource.
		String ontology_service = endpoint;

		String endpointsSparql = "select ?label where {<" + subject
				+ "> <http://www.w3.org/2000/01/rdf-schema#label> ?label FILTER (lang(?label) = 'en')} LIMIT 100";

		Query sparqlQuery = QueryFactory.create(endpointsSparql, Syntax.syntaxARQ);
		QueryEngineHTTP qexec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(ontology_service, sparqlQuery);
		qexec.setModelContentType(WebContent.contentTypeRDFXML);
		ResultSet results = qexec.execSelect();
		String property = null;
		while (results.hasNext()) {
			QuerySolution qs = results.next();
			property = qs.getLiteral("?label").getLexicalForm();
		}
		return property;

	}
 
Example #2
Source File: Sparql11SearchHandlerTestCase.java    From SolRDF with Apache License 2.0 6 votes vote down vote up
@Test 
public void queryUsingPOSTDirectly() throws Exception {
	when(httpRequest.getContentType()).thenReturn(WebContent.contentTypeSPARQLQuery);
	when(httpRequest.getMethod()).thenReturn("POST");
	
	final List<ContentStream> dummyStream = new ArrayList<ContentStream>(1);
	dummyStream.add(new ContentStreamBase.ByteArrayStream(new byte[0], "dummy") {
		@Override
		public String getContentType() {
			return WebContent.contentTypeSPARQLUpdate;
		}
	});
	when(request.getContentStreams()).thenReturn(dummyStream);
	when(request.getParams()).thenReturn(new ModifiableSolrParams());
	
	cut.handleRequestBody(request, response);
	
	verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SEARCH_HANDLER_NAME);
}
 
Example #3
Source File: PatchWriteServlet.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
protected void validate(HttpAction action) {
    String method = action.getRequest().getMethod();
    switch(method) {
        case HttpNames.METHOD_POST:
            break;
        default:
            ServletOps.errorMethodNotAllowed(method+" : Patch must use POST");
    }
    String ctStr = action.request.getContentType();
    // Must be UTF-8 or unset. But this is wrong so often,
    // it is less trouble to just force UTF-8.
    String charset = action.request.getCharacterEncoding();
    if ( charset != null && ! WebContent.charsetUTF8.equals(charset) )
        ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Charset must be omitted or must be UTF-8, not "+charset);

    // If no header Content-type - assume patch-text.
    ContentType contentType = ( ctStr != null ) ? ContentType.create(ctStr) : ctPatchText;
    if ( ! ctPatchText.equals(contentType) && ! ctPatchBinary.equals(contentType) )
        ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Allowed Content-types are "+ctPatchText+" or "+ctPatchBinary+", not "+ctStr);
    if ( ctPatchBinary.equals(contentType) )
        ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, contentTypePatchBinary+" not supported yet");
}
 
Example #4
Source File: S_JSON.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
public static void json(HttpServletRequest req, HttpServletResponse resp, JsonValue responseContent) {
    try { 
        resp.setHeader(HttpNames.hCacheControl, "no-cache");
        resp.setHeader(HttpNames.hContentType,  WebContent.contentTypeJSON);
        resp.setStatus(HttpSC.OK_200);
        try(ServletOutputStream out = resp.getOutputStream(); IndentedWriter b = new IndentedWriter(out); ) {
            b.setFlatMode(true);
            JSON.write(b, responseContent);
            b.ensureStartOfLine();
            b.flush();
            out.write('\n');
        }
    } catch (IOException ex) {
        LOG.warn("json: IOException", ex);
        try { 
            resp.sendError(HttpSC.INTERNAL_SERVER_ERROR_500, "Internal server error");
        } catch (IOException ex2) {}
    }
}
 
Example #5
Source File: HttpErrorHandler.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
    if ( request.getMethod().equals(METHOD_POST)) {
        response.setContentType(WebContent.contentTypeTextPlain);
        response.setCharacterEncoding(WebContent.charsetUTF8) ;
        
        String reason=(response instanceof Response)?((Response)response).getReason():null;
        String msg = String.format("%03d %s\n", response.getStatus(), reason) ;
        response.getOutputStream().write(msg.getBytes(StandardCharsets.UTF_8)) ;
        
        response.getOutputStream().flush() ;
        baseRequest.setHandled(true);
        return;
    }
    super.handle(target, baseRequest, request, response); 
}
 
Example #6
Source File: Sparql11SearchHandlerTestCase.java    From SolRDF with Apache License 2.0 6 votes vote down vote up
@Test 
public void updateUsingPOSTDirectly() throws Exception {
	when(httpRequest.getContentType()).thenReturn(WebContent.contentTypeSPARQLUpdate);
	when(httpRequest.getMethod()).thenReturn("POST");
	
	final List<ContentStream> dummyStream = new ArrayList<ContentStream>(1);
	dummyStream.add(new ContentStreamBase.ByteArrayStream(new byte[0], "dummy") {
		@Override
		public String getContentType() {
			return WebContent.contentTypeSPARQLUpdate;
		}
	});
	when(request.getContentStreams()).thenReturn(dummyStream);
	when(request.getParams()).thenReturn(new ModifiableSolrParams());
	
	cut.handleRequestBody(request, response);
	
	verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_UPDATE_HANDLER_NAME);
}
 
Example #7
Source File: RdfBulkUpdateRequestHandler.java    From SolRDF with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
protected Map<String, ContentStreamLoader> createDefaultLoaders(final NamedList parameters) {
	final Map<String, ContentStreamLoader> registry = new HashMap<String, ContentStreamLoader>();
	final ContentStreamLoader loader = new RdfDataLoader();
	for (final Lang language : RDFLanguages.getRegisteredLanguages()) {
		registry.put(language.getContentType().toHeaderString(), loader);
	}
	registry.put(WebContent.contentTypeSPARQLUpdate, new Sparql11UpdateRdfDataLoader());

	if (log.isDebugEnabled()) {
		prettyPrint(registry);
	}
	
	return registry;
}
 
Example #8
Source File: PHandlerSPARQLUpdate.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Patch patch) { 
    IndentedLineBuffer x = new IndentedLineBuffer() ;
    RDFChanges scData = new RDFChangesWriteUpdate(x) ;
    patch.play(scData);
    x.flush();
    String reqStr = x.asString() ;
    updateEndpoints.forEach((ep)->{
        try { HttpOp.execHttpPost(ep, WebContent.contentTypeSPARQLUpdate, reqStr) ; }
        catch (HttpException ex) { DPS.LOG.warn("Failed to send to "+ep) ; }
    }) ;
}
 
Example #9
Source File: Sparql11SearchHandlerTestCase.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Test 
public void queryUsingPOSTWithURLEncodedParametersWithoutUpdateCommand() throws Exception {
	when(httpRequest.getMethod()).thenReturn("POST");
	when(httpRequest.getContentType()).thenReturn(WebContent.contentTypeHTMLForm);
	when(request.getParams()).thenReturn(new ModifiableSolrParams());
	
	try {
		cut.handleRequestBody(request, response);
		fail();
	} catch (final Exception expected) {
		final SolrException exception = (SolrException) expected;
		assertEquals(ErrorCode.BAD_REQUEST.code, exception.code());
		assertEquals(Sparql11SearchHandler.MISSING_QUERY_OR_UPDATE_IN_POST_MESSAGE, exception.getMessage());
	}
}
 
Example #10
Source File: Sparql11SearchHandlerTestCase.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Test 
public void queryUsingPOSTWithURLEncodedParameters_II() throws Exception {
	final ModifiableSolrParams parameters = new ModifiableSolrParams();
	parameters.set(Names.QUERY, randomString());

	when(httpRequest.getMethod()).thenReturn("POST");
	when(httpRequest.getContentType()).thenReturn(WebContent.contentTypeHTMLForm + ";charset=UTF-8");
	when(request.getParams()).thenReturn(parameters);
	
	cut.handleRequestBody(request, response);
	
	verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SEARCH_HANDLER_NAME);
}
 
Example #11
Source File: Sparql11SearchHandlerTestCase.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Test 
public void queryUsingPOSTWithURLEncodedParameters_I() throws Exception {
	final ModifiableSolrParams parameters = new ModifiableSolrParams();
	parameters.set(Names.QUERY, randomString());

	when(httpRequest.getMethod()).thenReturn("POST");
	when(httpRequest.getContentType()).thenReturn(WebContent.contentTypeHTMLForm);
	when(request.getParams()).thenReturn(parameters);
	
	cut.handleRequestBody(request, response);
	
	verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SEARCH_HANDLER_NAME);
}
 
Example #12
Source File: Sparql11SearchHandlerTestCase.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Test 
public void updateUsingPOSTWithURLEncodedParametersWithoutUpdateCommand() throws Exception {
	when(httpRequest.getMethod()).thenReturn("POST");
	when(httpRequest.getContentType()).thenReturn(WebContent.contentTypeHTMLForm);
	when(request.getParams()).thenReturn(new ModifiableSolrParams());
	
	try {
		cut.handleRequestBody(request, response);
		fail();
	} catch (final Exception expected) {
		final SolrException exception = (SolrException) expected;
		assertEquals(ErrorCode.BAD_REQUEST.code, exception.code());
		assertEquals(Sparql11SearchHandler.MISSING_QUERY_OR_UPDATE_IN_POST_MESSAGE, exception.getMessage());
	}
}
 
Example #13
Source File: Sparql11SearchHandlerTestCase.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Test 
public void updateUsingPOSTWithURLEncodedParameters() throws Exception {
	final ModifiableSolrParams parameters = new ModifiableSolrParams();
	parameters.set(Names.UPDATE_PARAMETER_NAME, randomString());

	when(httpRequest.getMethod()).thenReturn("POST");
	when(httpRequest.getContentType()).thenReturn(WebContent.contentTypeHTMLForm);
	when(request.getParams()).thenReturn(parameters);
	
	cut.handleRequestBody(request, response);
	
	verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_UPDATE_HANDLER_NAME);
}
 
Example #14
Source File: ResponseContentTypeChoiceTestCase.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Test
public void getContentType_ConstructQueryWithIncomingMediaType() {
	assertIncomingRequestWithMediaType(
			constructQuery, 
			new String[] {
				WebContent.contentTypeRDFXML, 
				WebContent.contentTypeNTriples,
				WebContent.contentTypeTurtle });
}
 
Example #15
Source File: ResponseContentTypeChoiceTestCase.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Test
public void getContentType_DescribeQueryWithIncomingMediaType() {
	assertIncomingRequestWithMediaType(
			describeQuery, 
			new String[] {
				WebContent.contentTypeRDFXML, 
				WebContent.contentTypeNTriples,
				WebContent.contentTypeTurtle });
}
 
Example #16
Source File: ResponseContentTypeChoiceTestCase.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Test
public void getContentType_AskQueryWithIncomingMediaType() {
	assertIncomingRequestWithMediaType(
			askQuery, 
			new String[] {
				WebContent.contentTypeTextCSV, 
				WebContent.contentTypeTextPlain,
				WebContent.contentTypeTextTSV,
				ResultSetLang.SPARQLResultSetJSON.getHeaderString(),
				ResultSetLang.SPARQLResultSetXML.getHeaderString() });
}
 
Example #17
Source File: RdfUpdateRequestHandler.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
protected Map<String, ContentStreamLoader> createDefaultLoaders(final NamedList parameters) {
	final Map<String, ContentStreamLoader> registry = new HashMap<String, ContentStreamLoader>();
	registry.put(WebContent.contentTypeHTMLForm, new Sparql11UpdateRdfDataLoader());
	registry.put("application/rdf+xml", new Sparql11UpdateRdfDataLoader());
	registry.put(WebContent.contentTypeSPARQLUpdate, new Sparql11UpdateRdfDataLoader());
	
	if (log.isDebugEnabled()) { 
		prettyPrint(registry);
	}
	
	return registry;
}
 
Example #18
Source File: S_ReplyText.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
private void text(HttpServletRequest req, HttpServletResponse resp) {
    try { 
        resp.setHeader(HttpNames.hContentType,  WebContent.contentTypeTextPlain);
        resp.setStatus(HttpSC.OK_200);
        try(ServletOutputStream out = resp.getOutputStream(); ) {
            output.actionEx(out);
        }
    } catch (IOException ex) {
        LOG.warn("text out: IOException", ex);
        try { 
            resp.sendError(HttpSC.INTERNAL_SERVER_ERROR_500, "Internal server error");
        } catch (IOException ex2) {}
    }
}
 
Example #19
Source File: PatchApplyService.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(HttpAction action) {
    String method = action.getRequest().getMethod();
    switch(method) {
        case HttpNames.METHOD_POST:
        case HttpNames.METHOD_PATCH:
            break;
        default:
            ServletOps.errorMethodNotAllowed(method+" : Patch must use POST or PATCH");
    }
    String ctStr = action.request.getContentType();
    // Must be UTF-8 or unset. But this is wrong so often.
    // It is less trouble to just force UTF-8.
    String charset = action.request.getCharacterEncoding();
    if ( charset != null && ! WebContent.charsetUTF8.equals(charset) )
        ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Charset must be omitted or UTF-8, not "+charset);

    if ( WebContent.contentTypeHTMLForm.equals(ctStr) ) {
        // Both "curl --data" and "wget --post-file"
        // without also using "--header" will set the Content-type to "application/x-www-form-urlencoded".
        // Treat this as "unset".
        ctStr = null;
    }

    // If no header Content-type - assume patch-text.
    ContentType contentType = ( ctStr != null ) ? ContentType.create(ctStr) : ctPatchText;

    if ( ! ctPatchText.equals(contentType) && ! ctPatchBinary.equals(contentType) )
        ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Allowed Content-types are "+ctPatchText+" or "+ctPatchBinary+", not "+ctStr);
    if ( ctPatchBinary.equals(contentType) )
        ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, contentTypePatchBinary+" not supported yet");
}
 
Example #20
Source File: DatasetGenerator.java    From NLIWOD with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
	// DBpedia as SPARQL endpoint
	long timeToLive = TimeUnit.DAYS.toMillis(30);
	CacheFrontend cacheFrontend = CacheUtilsH2.createCacheFrontend("/tmp/qald/sparql", true, timeToLive);
	  final QueryExecutionFactory qef  = FluentQueryExecutionFactory
			                                 .http("http://dbpedia.org/sparql", Lists.newArrayList("http://dbpedia.org"))
			                                 .config().withPostProcessor(qe -> ((QueryEngineHTTP) ((QueryExecutionHttpWrapper) qe).getDecoratee())
			                                                 .setModelContentType(WebContent.contentTypeRDFXML))
			                                 .withCache(cacheFrontend)
			                                 .end()
			                                 .create();
	List<IQuestion> questions = LoaderController.load(Dataset.QALD9_Train_Multilingual);
	// List<IQuestion> questions =
	// LoaderController.load(Dataset.QALD6_Train_Multilingual);
	questions.stream().filter(q -> q.getAnswerType().equals("resource")).collect(Collectors.toList());
	questions.forEach(q -> updateGoldenAnswers(qef, q));

	IRIShortFormProvider sfp = new SimpleIRIShortFormProvider();
	questions.forEach(q -> q.setGoldenAnswers(q.getGoldenAnswers().stream().map(a -> sfp.getShortForm(IRI.create(a))).collect(Collectors.toSet())));
	// questions = questions.stream()
	// .filter(q -> q.getLanguageToQuestion().get("en").equals("Where did
	// Super Bowl 50 take place?"))
	// .collect(Collectors.toList());

	DatasetGenerator stan = new DatasetGenerator(qef);
	stan.generate(questions);

	stan.writeCSVEvaluation();
}
 
Example #21
Source File: Sparql11SearchHandler.java    From SolRDF with Apache License 2.0 4 votes vote down vote up
@Override
public String getContentType() {
	return WebContent.contentTypeHTMLForm;
}
 
Example #22
Source File: Sparql11SearchHandlerTestCase.java    From SolRDF with Apache License 2.0 4 votes vote down vote up
@Test
public void isURLEncodedParameters() {
	when(httpRequest.getContentType()).thenReturn(WebContent.contentTypeHTMLForm);
	assertTrue(cut.isUsingURLEncodedParameters(request));
}
 
Example #23
Source File: Sparql11SearchHandler.java    From SolRDF with Apache License 2.0 2 votes vote down vote up
/**
 * Returns true if the current request has a "application/sparql-query" content type.
 * 
 * @param request the current Solr request.
 * @return true if the current request has a "application/sparql-query" content type.
 */
boolean isSparqlQueryContentType(final SolrQueryRequest request) {
	return contentType(request).startsWith(WebContent.contentTypeSPARQLQuery); 
}
 
Example #24
Source File: Sparql11SearchHandler.java    From SolRDF with Apache License 2.0 2 votes vote down vote up
/**
 * Returns true if the current request has a "application/sparql-update" content type.
 * 
 * @param request the current Solr request.
 * @return true if the current request has a "application/sparql-update" content type.
 */
boolean isSparqlUpdateContentType(final SolrQueryRequest request) {
	return contentType(request).startsWith(WebContent.contentTypeSPARQLUpdate);
}
 
Example #25
Source File: Sparql11SearchHandler.java    From SolRDF with Apache License 2.0 2 votes vote down vote up
/**
 * Returns true if the current request is using POST method and URL-encoded parameters.
 * 
 * @param request the current Solr request.
 * @return true if the current request is using POST method and URL-encoded parameters.
 */
boolean isUsingURLEncodedParameters(final SolrQueryRequest request) {
	return contentType(request).startsWith(WebContent.contentTypeHTMLForm);
}