List of usage examples for java.net HttpURLConnection HTTP_UNAVAILABLE
int HTTP_UNAVAILABLE
To view the source code for java.net HttpURLConnection HTTP_UNAVAILABLE.
Click Source Link
From source file:com.google.apphosting.vmruntime.VmApiProxyDelegateTest.java
private void callDelegateWithConnectionError(boolean sync, ApiProxyException expectedException) throws Exception { HttpClient mockClient = createMockHttpClient(); createMockHttpResponse(new byte[0], HttpURLConnection.HTTP_UNAVAILABLE); when(mockClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(HttpContext.class))) .thenThrow(new IOException("Connection refused")); VmApiProxyDelegate delegate = new VmApiProxyDelegate(mockClient); VmApiProxyEnvironment environment = createMockEnvironment(); byte[] requestData = new byte[] { 0, 1, 2, 3, 4, 5 }; byte[] result = null; final Double timeoutInSeconds = 10.0; if (sync) {// w w w . ja v a2s .c om try { environment.getAttributes().put(VmApiProxyDelegate.API_DEADLINE_KEY, timeoutInSeconds); result = delegate.makeSyncCall(environment, TEST_PACKAGE_NAME, TEST_METHOD_NAME, requestData); fail(); } catch (ApiProxyException exception) { assertEquals(expectedException.getClass(), exception.getClass()); } } else { try { ApiConfig apiConfig = new ApiConfig(); apiConfig.setDeadlineInSeconds(timeoutInSeconds); result = delegate .makeAsyncCall(environment, TEST_PACKAGE_NAME, TEST_METHOD_NAME, requestData, apiConfig) .get(); fail(); } catch (ExecutionException exception) { // ExecutionException is expected, and make sure the cause is expected as well. assertEquals(expectedException.getClass(), exception.getCause().getClass()); } } assertNull(result); }
From source file:org.oclc.oai.harvester2.verb.HarvesterVerb.java
/** * Preforms the OAI request, recovering from typical XML error * * @author nfreire Nuno Freire / Gilberto Pedrosa * @param requestURL/*w ww . j a v a 2s. c o m*/ * @throws IOException * @throws ParserConfigurationException * @throws SAXException * @throws TransformerException */ private void harvest(String requestURL) throws IOException, ParserConfigurationException, SAXException, TransformerException { this.requestURL = requestURL; logger.debug("requestURL=" + requestURL); InputStream in; URL url = new URL(requestURL); HttpURLConnection con; int responseCode; do { con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(30000); con.setReadTimeout(600000); if (con.getAllowUserInteraction()) { con.setRequestProperty("User-Agent", "OAIHarvester/2.0"); con.setRequestProperty("Accept-Encoding", "compress, gzip, identify"); } try { responseCode = con.getResponseCode(); logger.debug("responseCode=" + responseCode); } catch (FileNotFoundException e) { // assume it's a 503 response logger.error(requestURL, e); responseCode = HttpURLConnection.HTTP_UNAVAILABLE; } if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) { long retrySeconds = con.getHeaderFieldInt("Retry-After", -1); if (retrySeconds == -1) { long now = (new Date()).getTime(); long retryDate = con.getHeaderFieldDate("Retry-After", now); retrySeconds = retryDate - now; } if (retrySeconds == 0) { // Apparently, it's a bad URL throw new FileNotFoundException("Bad URL?"); } logger.warn("Server response: Retry-After=" + retrySeconds); if (retrySeconds > 0) { try { Thread.sleep(retrySeconds * 1000); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE); String contentEncoding = con.getHeaderField("Content-Encoding"); logger.debug("contentEncoding=" + contentEncoding); if ("compress".equals(contentEncoding)) { ZipInputStream zis = new ZipInputStream(con.getInputStream()); zis.getNextEntry(); in = zis; } else if ("gzip".equals(contentEncoding)) { in = new GZIPInputStream(con.getInputStream()); } else if ("deflate".equals(contentEncoding)) { in = new InflaterInputStream(con.getInputStream()); } else { in = con.getInputStream(); } byte[] inputBytes = IOUtils.toByteArray(in); InputSource data = new InputSource(new ByteArrayInputStream(inputBytes)); Thread t = Thread.currentThread(); DocumentBuilder builder = (DocumentBuilder) builderMap.get(t); if (builder == null) { builder = factory.newDocumentBuilder(); builderMap.put(t, builder); } try { doc = builder.parse(data); } catch (SAXException e) { try { //Here we can try to recover the xml from known typical problems //Recover from invalid characters //we assume this is UTF-8... String xmlString = new String(inputBytes, "UTF-8"); xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString); data = new InputSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8"))); doc = builder.parse(data); } catch (Exception e2) { //the recovered version did not work either. Throw the original exception throw e; } } catch (IOException e3) { System.out.println("e = " + e3.getMessage()); } catch (Exception e4) { System.out.println("e = " + e4.getMessage()); } StringTokenizer tokenizer = new StringTokenizer(getSingleString("/*/@xsi:schemaLocation"), " "); StringBuffer sb = new StringBuffer(); while (tokenizer.hasMoreTokens()) { if (sb.length() > 0) sb.append(" "); sb.append(tokenizer.nextToken()); } this.schemaLocation = sb.toString(); this.defaultNamespace = getDocument().getDocumentElement().getNamespaceURI(); }
From source file:org.oclc.oai.harvester.verb.HarvesterVerb.java
/** * Performs the OAI request, recovering from typical XML error * //from w w w. j a va2 s .co m * @author nfreire Nuno Freire / Gilberto Pedrosa * @param requestURL * @throws IOException * @throws ParserConfigurationException * @throws SAXException * @throws TransformerException */ private void harvest(String requestURL) throws IOException, ParserConfigurationException, SAXException, TransformerException { this.requestURL = requestURL; logger.debug("requestURL=" + requestURL); InputStream in; URL url = new URL(requestURL); HttpURLConnection con; int responseCode; do { con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(30000); con.setReadTimeout(600000); if (con.getAllowUserInteraction()) { con.setRequestProperty("User-Agent", "OAIHarvester/2.0"); con.setRequestProperty("Accept-Encoding", "compress, gzip, identify"); } try { responseCode = con.getResponseCode(); logger.debug("responseCode=" + responseCode); } catch (FileNotFoundException e) { // assume it's a 503 response logger.error(requestURL, e); responseCode = HttpURLConnection.HTTP_UNAVAILABLE; } if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) { long retrySeconds = con.getHeaderFieldInt("Retry-After", -1); if (retrySeconds == -1) { long now = (new Date()).getTime(); long retryDate = con.getHeaderFieldDate("Retry-After", now); retrySeconds = retryDate - now; } if (retrySeconds == 0) { // Apparently, it's a bad URL throw new FileNotFoundException("Bad URL?"); } logger.warn("Server response: Retry-After=" + retrySeconds); if (retrySeconds > 0) { try { Thread.sleep(retrySeconds * 1000); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE); String contentEncoding = con.getHeaderField("Content-Encoding"); logger.debug("contentEncoding=" + contentEncoding); if ("compress".equals(contentEncoding)) { ZipInputStream zis = new ZipInputStream(con.getInputStream()); zis.getNextEntry(); in = zis; } else if ("gzip".equals(contentEncoding)) { in = new GZIPInputStream(con.getInputStream()); } else if ("deflate".equals(contentEncoding)) { in = new InflaterInputStream(con.getInputStream()); } else { in = con.getInputStream(); } byte[] inputBytes = IOUtils.toByteArray(in); InputSource data = new InputSource(new ByteArrayInputStream(inputBytes)); Thread t = Thread.currentThread(); DocumentBuilder builder = builderMap.get(t); if (builder == null) { builder = factory.newDocumentBuilder(); builderMap.put(t, builder); } try { doc = builder.parse(data); } catch (SAXException e) { try { //Here we can try to recover the xml from known typical problems //Recover from invalid characters //we assume this is UTF-8... String xmlString = new String(inputBytes, "UTF-8"); xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString); data = new InputSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8"))); doc = builder.parse(data); } catch (Exception e2) { //the recovered version did not work either. Throw the original exception throw e; } } catch (IOException e3) { System.out.println("e = " + e3.getMessage()); } catch (Exception e4) { System.out.println("e = " + e4.getMessage()); } StringTokenizer tokenizer = new StringTokenizer(getSingleString("/*/@xsi:schemaLocation"), " "); StringBuffer sb = new StringBuffer(); while (tokenizer.hasMoreTokens()) { if (sb.length() > 0) sb.append(" "); sb.append(tokenizer.nextToken()); } this.schemaLocation = sb.toString(); this.defaultNamespace = getDocument().getDocumentElement().getNamespaceURI(); }
From source file:com.scvngr.levelup.core.net.LevelUpResponse.java
/** * Maps an {@link LevelUpStatus} to an HTTP status code from the response. * * @param statusCode the HTTP status code from the response. * @param serverHeader the value of the Server HTTP header; this is used to validate whether the * response is coming from the LevelUp Platform server or a backup/reverse-proxy. * @return {@link LevelUpStatus} describing the status code. */// www. j a va 2 s . c o m @NonNull @VisibleForTesting(visibility = Visibility.PRIVATE) /* package */static LevelUpStatus mapStatus(final int statusCode, @Nullable final String serverHeader) { final boolean fromLevelUpPlatform = HTTP_HEADER_VALUE_SERVER.equals(serverHeader); final LevelUpStatus status; if (statusCode >= AbstractResponse.STATUS_CODE_SUCCESS_MIN_INCLUSIVE && statusCode < AbstractResponse.STATUS_CODE_SUCCESS_MAX_EXCLUSIVE) { // A 2xx status code is OK. status = LevelUpStatus.OK; } else if (HttpURLConnection.HTTP_NOT_IMPLEMENTED == statusCode && fromLevelUpPlatform) { /* * The API treats a 501 response as a requirement for the client to be upgraded. * However, in failover etc. this response should be ignored since it may have a * different meaning coming from other servers. */ status = LevelUpStatus.UPGRADE; } else if (HttpURLConnection.HTTP_UNAUTHORIZED == statusCode && fromLevelUpPlatform) { /* * The API treats a 401 response as a notice that the user needs a new access token, and * should be logged out. However, if a reverse-proxy or backup server is sending the * response, this should be ignored since it may have a different meaning coming from * other servers. */ status = LevelUpStatus.LOGIN_REQUIRED; } else if (HttpURLConnection.HTTP_NOT_FOUND == statusCode && fromLevelUpPlatform) { /* * Some endpoints (like PaymentToken) have special meanings for 404s, but in failover * they may just indicate a partially-functioning service and should be treated as * generic network errors. */ status = LevelUpStatus.ERROR_NOT_FOUND; } else if (HttpURLConnection.HTTP_UNAVAILABLE == statusCode) { // LevelUp is down for maintenance (applicable even if the Server header differs). status = LevelUpStatus.ERROR_MAINTENANCE; } else { // All other response codes are server errors. status = LevelUpStatus.ERROR_SERVER; } return status; }
From source file:org.eclipse.hono.client.impl.HonoClientImpl.java
private void reconnect(final Throwable connectionFailureCause, final Handler<AsyncResult<HonoClient>> connectionHandler, final Handler<ProtonConnection> disconnectHandler) { if (shuttingDown.get()) { // no need to try to re-connect connectionHandler.handle(Future.failedFuture(new IllegalStateException("client is shut down"))); } else if (clientOptions.getReconnectAttempts() - reconnectAttempts.get() == 0) { reconnectAttempts = new AtomicInteger(0); LOG.debug("max number of attempts [{}] to re-connect to peer [{}:{}] have been made, giving up", clientOptions.getReconnectAttempts(), connectionFactory.getHost(), connectionFactory.getPort()); if (connectionFailureCause == null) { connectionHandler.handle(Future.failedFuture( new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "failed to connect"))); } else {/* w w w.j a v a 2 s . c o m*/ connectionHandler .handle(Future.failedFuture(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "failed to connect", connectionFailureCause))); } } else { LOG.trace("scheduling attempt to re-connect ..."); reconnectAttempts.getAndIncrement(); // give Vert.x some time to clean up NetClient vertx.setTimer(clientOptions.getReconnectInterval(), tid -> { LOG.debug("starting attempt [#{}] to re-connect to server [{}:{}]", reconnectAttempts.get(), connectionFactory.getHost(), connectionFactory.getPort()); connect(clientOptions, connectionHandler, disconnectHandler); }); } }
From source file:org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob.java
/** * @return true if object was successfully downloaded, false, if there was a loading failure *//* ww w . j a v a 2 s .c o m*/ private boolean loadObject() { if (attributes == null) { attributes = new CacheEntryAttributes(); } try { // if we have object in cache, and host doesn't support If-Modified-Since nor If-None-Match // then just use HEAD request and check returned values if (isObjectLoadable() && Boolean.TRUE.equals(useHead.get(getServerKey())) && isCacheValidUsingHead()) { LOG.log(Level.FINE, "JCS - cache entry verified using HEAD request: {0}", getUrl()); return true; } LOG.log(Level.FINE, "JCS - starting HttpClient GET request for URL: {0}", getUrl()); final HttpClient request = getRequest("GET", true); if (isObjectLoadable() && (now - attributes.getLastModification()) <= ABSOLUTE_EXPIRE_TIME_LIMIT) { request.setIfModifiedSince(attributes.getLastModification()); } if (isObjectLoadable() && attributes.getEtag() != null) { request.setHeader("If-None-Match", attributes.getEtag()); } final HttpClient.Response urlConn = request.connect(); if (urlConn.getResponseCode() == 304) { // If isModifiedSince or If-None-Match has been set // and the server answers with a HTTP 304 = "Not Modified" LOG.log(Level.FINE, "JCS - If-Modified-Since/ETag test: local version is up to date: {0}", getUrl()); return true; } else if (isObjectLoadable() // we have an object in cache, but we haven't received 304 response code && ((attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getHeaderField("ETag"))) || attributes.getLastModification() == urlConn.getLastModified())) { // we sent ETag or If-Modified-Since, but didn't get 304 response code // for further requests - use HEAD String serverKey = getServerKey(); LOG.log(Level.INFO, "JCS - Host: {0} found not to return 304 codes for If-Modified-Since or If-None-Match headers", serverKey); useHead.put(serverKey, Boolean.TRUE); } attributes = parseHeaders(urlConn); for (int i = 0; i < 5; ++i) { if (urlConn.getResponseCode() == HttpURLConnection.HTTP_UNAVAILABLE) { Thread.sleep(5000L + new SecureRandom().nextInt(5000)); continue; } attributes.setResponseCode(urlConn.getResponseCode()); byte[] raw; if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) { raw = Utils.readBytesFromStream(urlConn.getContent()); } else { raw = new byte[] {}; } if (isResponseLoadable(urlConn.getHeaderFields(), urlConn.getResponseCode(), raw)) { // we need to check cacheEmpty, so for cases, when data is returned, but we want to store // as empty (eg. empty tile images) to save some space cacheData = createCacheEntry(raw); cache.put(getCacheKey(), cacheData, attributes); LOG.log(Level.FINE, "JCS - downloaded key: {0}, length: {1}, url: {2}", new Object[] { getCacheKey(), raw.length, getUrl() }); return true; } else if (cacheAsEmpty()) { cacheData = createCacheEntry(new byte[] {}); cache.put(getCacheKey(), cacheData, attributes); LOG.log(Level.FINE, "JCS - Caching empty object {0}", getUrl()); return true; } else { LOG.log(Level.FINE, "JCS - failure during load - reponse is not loadable nor cached as empty"); return false; } } } catch (FileNotFoundException e) { LOG.log(Level.FINE, "JCS - Caching empty object as server returned 404 for: {0}", getUrlNoException()); attributes.setResponseCode(404); attributes.setError(e); boolean doCache = isResponseLoadable(null, 404, null) || cacheAsEmpty(); if (doCache) { cacheData = createCacheEntry(new byte[] {}); cache.put(getCacheKey(), cacheData, attributes); } return doCache; } catch (IOException e) { LOG.log(Level.FINE, "JCS - IOExecption during communication with server for: {0}", getUrlNoException()); if (isObjectLoadable()) { return true; } else { attributes.setError(e); attributes.setResponseCode(599); // set dummy error code, greater than 500 so it will be not cached return false; } } catch (InterruptedException e) { attributes.setError(e); LOG.log(Level.WARNING, "JCS - Exception during download {0}", getUrlNoException()); Logging.warn(e); Thread.currentThread().interrupt(); } LOG.log(Level.WARNING, "JCS - Silent failure during download: {0}", getUrlNoException()); return false; }
From source file:org.eclipse.hono.client.impl.HonoClientImpl.java
Future<MessageSender> getOrCreateSender(final String key, final Supplier<Future<MessageSender>> newSenderSupplier) { final Future<MessageSender> result = Future.future(); context.runOnContext(get -> {/*from ww w. j a v a 2 s. co m*/ final MessageSender sender = activeSenders.get(key); if (sender != null && sender.isOpen()) { LOG.debug("reusing existing message sender [target: {}, credit: {}]", key, sender.getCredit()); result.complete(sender); } else if (!creationLocks.computeIfAbsent(key, k -> Boolean.FALSE)) { // register a handler to be notified if the underlying connection to the server fails // so that we can fail the result handler passed in final Handler<Void> connectionFailureHandler = connectionLost -> { // remove lock so that next attempt to open a sender doesn't fail creationLocks.remove(key); result.tryFail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "no connection to service")); }; creationRequests.add(connectionFailureHandler); creationLocks.put(key, Boolean.TRUE); LOG.debug("creating new message sender for {}", key); newSenderSupplier.get().setHandler(creationAttempt -> { creationLocks.remove(key); creationRequests.remove(connectionFailureHandler); if (creationAttempt.succeeded()) { MessageSender newSender = creationAttempt.result(); LOG.debug("successfully created new message sender for {}", key); activeSenders.put(key, newSender); result.tryComplete(newSender); } else { LOG.debug("failed to create new message sender for {}", key, creationAttempt.cause()); activeSenders.remove(key); result.tryFail(creationAttempt.cause()); } }); } else { LOG.debug("already trying to create a message sender for {}", key); result.fail( new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "no connection to service")); } }); return result; }
From source file:net.myrrix.client.ClientRecommender.java
@Override public float[] estimatePreferences(long userID, long... itemIDs) throws TasteException { StringBuilder urlPath = new StringBuilder(); urlPath.append("/estimate/"); urlPath.append(userID);// w w w .j av a 2 s. c om for (long itemID : itemIDs) { urlPath.append('/').append(itemID); } TasteException savedException = null; for (HostAndPort replica : choosePartitionAndReplicas(userID)) { HttpURLConnection connection = null; try { connection = buildConnectionToReplica(replica, urlPath.toString(), "GET"); switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_OK: BufferedReader reader = IOUtils.bufferStream(connection.getInputStream()); try { float[] result = new float[itemIDs.length]; for (int i = 0; i < itemIDs.length; i++) { result[i] = LangUtils.parseFloat(reader.readLine()); } return result; } finally { reader.close(); } case HttpURLConnection.HTTP_UNAVAILABLE: throw new NotReadyException(); default: throw new TasteException(connection.getResponseCode() + " " + connection.getResponseMessage()); } } catch (TasteException te) { log.info("Can't access {} at {}: ({})", urlPath, replica, te.toString()); savedException = te; } catch (IOException ioe) { log.info("Can't access {} at {}: ({})", urlPath, replica, ioe.toString()); savedException = new TasteException(ioe); } finally { if (connection != null) { connection.disconnect(); } } } throw savedException; }
From source file:net.myrrix.client.ClientRecommender.java
public float estimateForAnonymous(long toItemID, long[] itemIDs, float[] values, Long contextUserID) throws TasteException { Preconditions.checkArgument(values == null || values.length == itemIDs.length, "Number of values doesn't match number of items"); StringBuilder urlPath = new StringBuilder(32); urlPath.append("/estimateForAnonymous/"); urlPath.append(toItemID);/*from w w w.ja v a2 s . co m*/ for (int i = 0; i < itemIDs.length; i++) { urlPath.append('/').append(itemIDs[i]); if (values != null) { urlPath.append('=').append(values[i]); } } // Requests are typically partitioned by user, but this request does not directly depend on a user. // If a user ID is supplied anyway, use it for partitioning since it will follow routing for other // requests related to that user. Otherwise just partition on the "to" item ID, which is at least // deterministic. long idToPartitionOn = contextUserID == null ? toItemID : contextUserID; TasteException savedException = null; for (HostAndPort replica : choosePartitionAndReplicas(idToPartitionOn)) { HttpURLConnection connection = null; try { connection = buildConnectionToReplica(replica, urlPath.toString(), "GET"); switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_OK: BufferedReader reader = IOUtils.bufferStream(connection.getInputStream()); try { return LangUtils.parseFloat(reader.readLine()); } finally { reader.close(); } case HttpURLConnection.HTTP_NOT_FOUND: throw new NoSuchItemException(Arrays.toString(itemIDs) + ' ' + toItemID); case HttpURLConnection.HTTP_UNAVAILABLE: throw new NotReadyException(); default: throw new TasteException(connection.getResponseCode() + " " + connection.getResponseMessage()); } } catch (TasteException te) { log.info("Can't access {} at {}: ({})", urlPath, replica, te.toString()); savedException = te; } catch (IOException ioe) { log.info("Can't access {} at {}: ({})", urlPath, replica, ioe.toString()); savedException = new TasteException(ioe); } finally { if (connection != null) { connection.disconnect(); } } } throw savedException; }
From source file:org.eclipse.hono.client.impl.HonoClientImpl.java
Future<MessageConsumer> createConsumer(final String tenantId, final Supplier<Future<MessageConsumer>> newConsumerSupplier) { final Future<MessageConsumer> result = Future.future(); context.runOnContext(get -> {/*from ww w. jav a2 s .c o m*/ // register a handler to be notified if the underlying connection to the server fails // so that we can fail the result handler passed in final Handler<Void> connectionFailureHandler = connectionLost -> { result.tryFail( new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "connection to server lost")); }; creationRequests.add(connectionFailureHandler); newConsumerSupplier.get().setHandler(attempt -> { creationRequests.remove(connectionFailureHandler); if (attempt.succeeded()) { result.tryComplete(attempt.result()); } else { result.tryFail(attempt.cause()); } }); }); return result; }