@Override public String toString() { StringBuilder sb = new StringBuilder() ; sb.append("HttpException: ") ; int code = getResponseCode() ; if ( code != QueryExceptionHTTP.noResponseCode ) { sb.append(code) ; if ( getResponseMessage() != null ) { sb.append(" ") ; sb.append(getResponseMessage()) ; } } else { sb.append(getCause().toString()+": "+getMessage()) ; } return sb.toString() ; } }
public static RuntimeException makeHumanFriendly(Exception e) { RuntimeException result; if(e instanceof HttpException) { HttpException x = (HttpException)e; result = new HttpException(x.getResponse(), e); } else if(e instanceof QueryExceptionHTTP) { QueryExceptionHTTP x = (QueryExceptionHTTP)e; result = new QueryExceptionHTTP(x.getResponseMessage() + " " + x.getMessage(), e); } else { result = new RuntimeException(e); } return result; } }
public static boolean checkStatusForTimeout(QueryExceptionHTTP e) { int httpCode = e.getResponseCode(); // 408,504,524 timeout codes from http://en.wikipedia.org/wiki/List_of_HTTP_status_codes return httpCode == 408 || httpCode == 504 || httpCode == 524; } }
private QueryExceptionHTTP rewrap(HttpException httpEx) { // The historical contract of HTTP Queries has been to throw QueryExceptionHTTP however using the standard // ARQ HttpOp machinery we use these days means the internal HTTP errors come back as HttpException // Therefore we need to wrap appropriately responseCode = httpEx.getResponseCode(); if (responseCode != -1) { // Was an actual HTTP error String responseLine = httpEx.getStatusLine() != null ? httpEx.getStatusLine() : "No Status Line"; return new QueryExceptionHTTP(responseCode, "HTTP " + responseCode + " error making the query: " + responseLine, httpEx); } else if (httpEx.getMessage() != null) { // Some non-HTTP error with a valid message e.g. Socket Communications failed, IO error return new QueryExceptionHTTP(responseCode, "Unexpected error making the query: " + httpEx.getMessage(), httpEx); } else if (httpEx.getCause() != null) { // Some other error with a cause e.g. Socket Communications failed, IO error return new QueryExceptionHTTP(responseCode, "Unexpected error making the query, see cause for further details", httpEx); } else { // Some other error with no message and no further cause return new QueryExceptionHTTP(responseCode, "Unexpected error making the query", httpEx); } }
protected ResultSet executeSelectQuery(String query) { logger.trace("Sending query\n{} ...", query); QueryExecution qe = qef.createQueryExecution(query); try { ResultSet rs = qe.execSelect(); timeout = false; return rs; } catch (QueryExceptionHTTP e) { if(e.getCause() instanceof SocketTimeoutException){ if(timeout){ logger.warn("Got timeout"); throw e; } else { logger.trace("Got local timeout"); } } else { logger.error("Exception executing query", e); } return new ResultSetMem(); } }
public static boolean checkStatusForTimeout(QueryExceptionHTTP e) { int httpCode = e.getResponseCode(); // 408,504,524 timeout codes from http://en.wikipedia.org/wiki/List_of_HTTP_status_codes return httpCode == 408 || httpCode == 504 || httpCode == 524; } }
private InputStream execPost() throws QueryExceptionHTTP { URL target = null; try { target = new URL(serviceURL); } catch (MalformedURLException malEx) { throw new QueryExceptionHTTP(0, "Malformed URL: " + malEx); } log.trace("POST " + target.toExternalForm()); ARQ.getHttpRequestLogger().trace(target.toExternalForm()); try { // Get the actual response stream TypedInputStream stream = HttpOp.execHttpPostFormStream(serviceURL, this, contentTypeResult, client, getContext()); if (stream == null) throw new QueryExceptionHTTP(404); return execCommon(stream); } catch (HttpException httpEx) { throw rewrap(httpEx); } }
protected Model executeConstructQuery(String query) { logger.trace("Sending query\n{} ...", query); QueryExecution qe = qef.createQueryExecution(query); try { Model model = qe.execConstruct(); timeout = false; if(model.size() == 0){ fullDataLoaded = true; } logger.debug("Got " + model.size() + " triples."); return model; } catch (QueryExceptionHTTP e) { if(e.getCause() instanceof SocketTimeoutException){ logger.warn("Got timeout"); } else { logger.error("Exception executing query", e); } return ModelFactory.createDefaultModel(); } }
private static void expectQuery(Runnable action, int expected) { try { action.run(); throw new HttpException("action completed"); } catch (QueryExceptionHTTP ex) { if ( ex.getResponseCode() != expected ) throw ex; } }
protected boolean executeAskQuery(String queryString){ if (logger.isTraceEnabled()) logger.trace("Sending query \n {}", queryString); try(QueryExecution qe = qef.createQueryExecution(queryString)) { boolean ret = qe.execAsk(); qe.close(); return ret; } catch (QueryExceptionHTTP e) { throw new QueryExceptionHTTP("Error sending query \"" + queryString + "\" to endpoint " + qef.getId(), e); } catch (Exception e) { throw new RuntimeException("Error sending query \"" + queryString + "\" to endpoint " + qef.getId(), e); } }
@Test public void testStringTimeout2() { BasicPattern basicPattern = new BasicPattern(); basicPattern.add(Triple.ANY); Node serviceNode = NodeFactory.createURI(SERVICE); OpService opService = new OpService(serviceNode, new OpBGP(basicPattern), false); Context context = new Context(); ARQ.setNormalMode(context); context.set(Service.queryTimeout, "10,10000"); try { Service.exec(opService, context); Assert.fail("Expected QueryExceptionHTTP"); } catch (QueryExceptionHTTP expected) { Throwable thrown = expected.getCause() ; if ( thrown instanceof SocketException || thrown instanceof ConnectTimeoutException ) { // expected } else { Assert.fail(String.format("Expected SocketException or ConnectTimeoutException, instead got: %s %s", thrown.getClass().getName(), thrown.getMessage())); } } }
private static void expectQuery(Runnable action, int expected) { try { action.run(); throw new HttpException("action completed"); } catch (QueryExceptionHTTP ex) { if ( ex.getResponseCode() != expected ) throw ex; } }
protected ResultSet executeSelectQuery(String queryString, long timeout, TimeUnit timeoutUnits){ if (logger.isTraceEnabled()) { logger.trace("Sending query \n {}", queryString); } try(QueryExecution qe = qef.createQueryExecution(queryString)) { qe.setTimeout(timeout, timeoutUnits); ResultSet rs = qe.execSelect(); return ResultSetFactory.copyResults(rs); } catch (QueryExceptionHTTP e) { throw new QueryExceptionHTTP("Error sending query \"" + queryString + "\" to endpoint " + qef.getId(), e); } catch (Exception e) { throw new RuntimeException("Error sending query \"" + queryString + "\" to endpoint " + qef.getId(), e); } }
@Test public void testNumericTimeout() { BasicPattern basicPattern = new BasicPattern(); basicPattern.add(Triple.ANY); Node serviceNode = NodeFactory.createURI(SERVICE); OpService opService = new OpService(serviceNode, new OpBGP(basicPattern), false); Context context = new Context(); ARQ.setNormalMode(context); context.set(Service.queryTimeout, 10); try { Service.exec(opService, context); Assert.fail("Expected QueryExceptionHTTP"); } catch (QueryExceptionHTTP expected) { Throwable thrown = expected.getCause() ; if ( thrown instanceof SocketException || thrown instanceof ConnectTimeoutException ) { // expected } else { Assert.fail(String.format("Expected SocketException or ConnectTimeoutException, instead got: %s %s", thrown.getClass().getName(), thrown.getMessage())); } } }
private void queryHttp(int statusCode, String user, String password, String queryString) { try { query(user, password, queryString); if ( statusCode < 200 && statusCode > 299 ) fail("Should have responded with "+statusCode); } catch (QueryExceptionHTTP ex) { assertEquals(statusCode, ex.getResponseCode()); } }
private InputStream execGet() throws QueryExceptionHTTP { URL target = null; String qs = getQueryString(); ARQ.getHttpRequestLogger().trace(qs); try { if (count() == 0) target = new URL(serviceURL); else target = new URL(serviceURL + (serviceParams ? "&" : "?") + qs); } catch (MalformedURLException malEx) { throw new QueryExceptionHTTP(0, "Malformed URL: " + malEx); } log.trace("GET " + target.toExternalForm()); try { try { // Get the actual response stream TypedInputStream stream = HttpOp.execHttpGet(target.toString(), contentTypeResult, client, getContext()); if (stream == null) throw new QueryExceptionHTTP(404); return execCommon(stream); } catch (HttpException httpEx) { // Back-off and try POST if something complain about long URIs if (httpEx.getResponseCode() == 414) return execPost(); throw httpEx; } } catch (HttpException httpEx) { throw rewrap(httpEx); } }
@Test public void testStringTimeout() { BasicPattern basicPattern = new BasicPattern(); basicPattern.add(Triple.ANY); Node serviceNode = NodeFactory.createURI(SERVICE); OpService opService = new OpService(serviceNode, new OpBGP(basicPattern), false); Context context = new Context(); ARQ.setNormalMode(context); context.set(Service.queryTimeout, "10"); try { Service.exec(opService, context); Assert.fail("Expected QueryExceptionHTTP"); } catch (QueryExceptionHTTP expected) { Throwable thrown = expected.getCause() ; if ( thrown instanceof SocketException || thrown instanceof ConnectTimeoutException ) { // expected } else { Assert.fail(String.format("Expected SocketException or ConnectTimeoutException, instead got: %s %s", thrown.getClass().getName(), thrown.getMessage())); } } }
private void queryHttp(int statusCode, String user, String password, String queryString) { try { query(user, password, queryString); if ( statusCode < 200 && statusCode > 299 ) fail("Should have responded with "+statusCode); } catch (QueryExceptionHTTP ex) { assertEquals(statusCode, ex.getResponseCode()); } }
throw new RuntimeException("Didn't fail"); } catch (QueryExceptionHTTP ex) { if ( ex.getResponseCode() != HttpSC.NOT_FOUND_404 ) { throw new RuntimeException("Not a 404", ex);
throw new RuntimeException("Didn't fail"); } catch (QueryExceptionHTTP ex) { if ( ex.getResponseCode() != HttpSC.NOT_FOUND_404 ) { throw new RuntimeException("Not a 404", ex);