org.apache.jena.atlas.web.HttpException Java Examples

The following examples show how to use org.apache.jena.atlas.web.HttpException. 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: JenaIOService.java    From trellis with Apache License 2.0 6 votes vote down vote up
private void writeJsonLd(final OutputStream output, final DatasetGraph graph, final IRI... profiles) {
    final String profile = getCustomJsonLdProfile(profiles);
    final RDFFormat format = canUseCustomJsonLdProfile(profile) ? JSONLD_COMPACT_FLAT : getJsonLdProfile(profiles);
    final JsonLDWriteContext ctx = new JsonLDWriteContext();
    if (canUseCustomJsonLdProfile(profile)) {
        LOGGER.debug("Setting JSON-LD context with profile: {}", profile);
        final String c = cache.get(profile, p -> {
            try (final TypedInputStream res = HttpOp.execHttpGet(profile)) {
                return IOUtils.toString(res.getInputStream(), UTF_8);
            } catch (final IOException | HttpException ex) {
                LOGGER.warn("Error fetching profile {}: {}", p, ex.getMessage());
                return null;
            }
        });
        if (c != null) {
            ctx.setJsonLDContext(c);
            ctx.setJsonLDContextSubstitution("\"" + profile + "\"");
        }
    }
    RDFWriter.create().format(format).context(ctx).source(graph).output(output);
}
 
Example #2
Source File: ASTest.java    From trellis with Apache License 2.0 5 votes vote down vote up
@Override
Graph getVocabulary(final String url) {
    final Graph graph = createDefaultGraph();
    try {
        RDFParser.source(url).httpAccept("application/ld+json").parse(graph);
    } catch (final HttpException ex) {
        LOGGER.warn("Could not fetch {}: {}", url, ex.getMessage());
        assumeTrue(false, "Error fetching the URL (" + url + "): skip the test");
    }
    return graph;
}
 
Example #3
Source File: TestDeltaFusekiBad.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test
public void patchserver_stop_start() {
    DeltaServer deltaServer = deltaServer();
    FusekiServer server1 = fuseki1();
    try {
        deltaServer.stop();
        deltaServer = null;

        // Should fail
        try (RDFConnection conn0 = RDFConnectionFactory.connect("http://localhost:"+F1_PORT+ds1) ) {
            conn0.update(PREFIX+"INSERT DATA { :s :p 'update_patchserver_stop_start' }");
            Assert.fail("Should not be able to update at the moment");
        } catch (HttpException ex) {
            assertEquals(503, ex.getStatusCode());
            // Expected - ignore.
            //assertTrue(ex.getResponseCode()>= 500);
        }

        deltaServer = deltaServer(Start.RESTART);

        try (RDFConnection conn1 = RDFConnectionFactory.connect("http://localhost:"+F1_PORT+ds1)) {
            conn1.query("ASK{}").execAsk();
        }

        // Should be able to update.
        try (RDFConnection conn0 = RDFConnectionFactory.connect("http://localhost:"+F1_PORT+ds1) ) {
            conn0.update(PREFIX+"INSERT DATA { :s :p 'update_patchserver_stop_start' }");
        }
    } finally {
        server1.stop();
        if ( deltaServer != null )
            deltaServer.stop();
    }
}
 
Example #4
Source File: DeltaCmd.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Override
protected void exec() {
    try {
        execCmd();
    }
    catch (HttpException ex) { throw new CmdException(messageFromHttpException(ex)) ; }
}
 
Example #5
Source File: TestCmdServerZkS3.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test(expected=HttpException.class)
public void deltaZkS3_3() {
    runtest(
        (endpoint)-> {
            // Would cause a 404 log message.
            // Hidden by the logging configuration.
            HttpOp.execHttpGet(endpoint+"$/noSuch");
        },
        endpointURL);
}
 
Example #6
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 #7
Source File: DeltaLinkHTTP.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
/** Perform a retryable operation. Retry only happen if HttpException happens. */
private <T> T retry(Action<T> callable, Supplier<Boolean> retryable, Supplier<String> retryMsg, Supplier<String> failureMsg) {
    for ( int i = 1 ; ; i++ ) {
        try {
            return callable.action();
        } catch (HttpException ex) {
            if ( failureMsg != null )
                // Other...
                Delta.DELTA_HTTP_LOG.warn(failureMsg.get());
            throw ex;
        }
        // Any other exception - don't retry.
    }
}
 
Example #8
Source File: DeltaLinkHTTP.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
private RDFPatch fetchCommon(Id dsRef, String param, String paramStr) {
    checkLink();

    String url = remoteReceive;
    url = createURL(url, DeltaConst.paramDatasource, dsRef.asParam());
    url = appendURL(url, paramStr);
    final String s = url;
    try {
        RDFPatch patch =  retry(()->{
            // [NET] Network point
            InputStream in = HttpOp.execHttpGet(s) ;
            if ( in == null )
                return null ;
            RDFPatchReaderText pr = new RDFPatchReaderText(in) ;
            RDFChangesCollector collector = new RDFChangesCollector();
            pr.apply(collector);
            return collector.getRDFPatch();
        }, ()->true, ()->"Retry fetch patch.", ()->"Failed to fetch patch.");
        return patch;
    }
    catch ( HttpException ex) {
        if ( ex.getStatusCode() == HttpSC.NOT_FOUND_404 ) {
            return null ; //throw new DeltaNotFoundException(ex.getMessage());
        }
        throw ex;
    }
}
 
Example #9
Source File: DeltaConnection.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
public void sync() {
    try {
        checkDeltaConnection();
        PatchLogInfo logInfo = getPatchLogInfo();
        sync(logInfo);
    } catch (HttpException ex) {
        if ( ex.getStatusCode() == -1 )
            throw new HttpException(HttpSC.SERVICE_UNAVAILABLE_503, HttpSC.getMessage(HttpSC.SERVICE_UNAVAILABLE_503), ex.getMessage());
        throw ex;
    }
}
 
Example #10
Source File: LibBuildDC.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
/** Build a Delta-backed datasets at a zone location. */
public static DatasetGraph setupDataset(String dsName, Location zoneLocation, LocalStorageType storage, DatasetGraph externalDataset, List<String> destURLs) {
    // Link to log server.
    DeltaLink deltaLink;
    if ( destURLs.size() == 1 )
        deltaLink = DeltaLinkHTTP.connect(destURLs.get(0));
    else {
        List<DeltaLink> links = new ArrayList<>(destURLs.size());
        for ( String destURL  : destURLs )
            links.add(DeltaLinkHTTP.connect(destURL));
        deltaLink = new DeltaLinkSwitchable(links);
    }

    Zone zone = Zone.connect(zoneLocation);
    DeltaClient deltaClient = DeltaClient.create(zone, deltaLink);
    SyncPolicy syncPolicy = SyncPolicy.TXN_RW;
    try { deltaLink.ping(); }
    catch (HttpException ex) {
        // rc < 0 : failed to connect - ignore?
        if ( ex.getStatusCode() > 0 )
            throw ex;
    }

    DatasetGraph dsg = ManagedDatasetBuilder.create()
        .deltaLink(deltaLink)
        .logName(dsName)
        .zone(zone)
        .syncPolicy(syncPolicy)
        .storageType(storage)
        .externalDataset(externalDataset)
        .build();
    return dsg;
 }
 
Example #11
Source File: HttpExceptionMapper.java    From Processor with Apache License 2.0 5 votes vote down vote up
@Override
public Response toResponse(HttpException ex)
{
    return getResponseBuilder(DatasetFactory.create(toResource(ex, Response.Status.INTERNAL_SERVER_ERROR,
                    ResourceFactory.createResource("http://www.w3.org/2011/http-statusCodes#InternalServerError")).
                getModel())).
            status(Response.Status.INTERNAL_SERVER_ERROR).
            build();
}
 
Example #12
Source File: DeltaCmd.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
protected String messageFromHttpException(HttpException ex) {
    Throwable ex2 = ex;
    if ( ex.getCause() != null )
        ex2 = ex.getCause();
    return ex2.getMessage();
}