Example usage for java.net HttpURLConnection HTTP_UNAVAILABLE

List of usage examples for java.net HttpURLConnection HTTP_UNAVAILABLE

Introduction

In this page you can find the example usage for java.net HttpURLConnection HTTP_UNAVAILABLE.

Prototype

int HTTP_UNAVAILABLE

To view the source code for java.net HttpURLConnection HTTP_UNAVAILABLE.

Click Source Link

Document

HTTP Status-Code 503: Service Unavailable.

Usage

From source file:eu.peppol.smp.SmpContentRetrieverImpl.java

/**
 * Gets the XML content of a given url, wrapped in an InputSource object.
 *///from  w w  w .  j  a  v  a  2 s.  com
@Override
public InputSource getUrlContent(URL url) {

    HttpURLConnection httpURLConnection = null;
    try {
        httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.connect();
    } catch (IOException e) {
        throw new IllegalStateException("Unable to connect to " + url + " ; " + e.getMessage(), e);
    }

    try {
        if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_UNAVAILABLE)
            throw new TryAgainLaterException(url, httpURLConnection.getHeaderField("Retry-After"));
        if (httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_OK)
            throw new ConnectionException(url, httpURLConnection.getResponseCode());
    } catch (IOException e) {
        throw new RuntimeException("Problem reading URL data at " + url.toExternalForm(), e);
    }

    try {

        String encoding = httpURLConnection.getContentEncoding();
        InputStream in = new BOMInputStream(httpURLConnection.getInputStream());
        InputStream result;

        if (encoding != null && encoding.equalsIgnoreCase(ENCODING_GZIP)) {
            result = new GZIPInputStream(in);
        } else if (encoding != null && encoding.equalsIgnoreCase(ENCODING_DEFLATE)) {
            result = new InflaterInputStream(in);
        } else {
            result = in;
        }

        String xml = readInputStreamIntoString(result);

        return new InputSource(new StringReader(xml));

    } catch (Exception e) {
        throw new RuntimeException("Problem reading URL data at " + url.toExternalForm(), e);
    }

}

From source file:com.fastbootmobile.encore.api.common.HttpGet.java

/**
 * Downloads the data from the provided URL.
 * @param inUrl The URL to get from//from  w ww.  java 2 s .c o  m
 * @param query The query field. '?' + query will be appended automatically, and the query data
 *              MUST be encoded properly.
 * @return A byte array of the data
 */
public static byte[] getBytes(String inUrl, String query, boolean cached)
        throws IOException, RateLimitException {
    final String formattedUrl = inUrl + (query.isEmpty() ? "" : ("?" + query));

    Log.d(TAG, "Formatted URL: " + formattedUrl);

    URL url = new URL(formattedUrl);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestProperty("User-Agent", "OmniMusic/1.0-dev (http://www.omnirom.org)");
    urlConnection.setUseCaches(cached);
    urlConnection.setInstanceFollowRedirects(true);
    int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
    urlConnection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);
    try {
        final int status = urlConnection.getResponseCode();
        // MusicBrainz returns 503 Unavailable on rate limit errors. Parse the JSON anyway.
        if (status == HttpURLConnection.HTTP_OK) {
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            int contentLength = urlConnection.getContentLength();
            if (contentLength <= 0) {
                // No length? Let's allocate 100KB.
                contentLength = 100 * 1024;
            }
            ByteArrayBuffer bab = new ByteArrayBuffer(contentLength);
            BufferedInputStream bis = new BufferedInputStream(in);
            int character;

            while ((character = bis.read()) != -1) {
                bab.append(character);
            }
            return bab.toByteArray();
        } else if (status == HttpURLConnection.HTTP_NOT_FOUND) {
            // 404
            return new byte[] {};
        } else if (status == HttpURLConnection.HTTP_FORBIDDEN) {
            return new byte[] {};
        } else if (status == HttpURLConnection.HTTP_UNAVAILABLE) {
            throw new RateLimitException();
        } else if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM
                || status == 307 /* HTTP/1.1 TEMPORARY REDIRECT */
                || status == HttpURLConnection.HTTP_SEE_OTHER) {
            // We've been redirected, follow the new URL
            final String followUrl = urlConnection.getHeaderField("Location");
            Log.e(TAG, "Redirected to: " + followUrl);
            return getBytes(followUrl, "", cached);
        } else {
            Log.e(TAG, "Error when fetching: " + formattedUrl + " (" + urlConnection.getResponseCode() + ")");
            return new byte[] {};
        }
    } finally {
        urlConnection.disconnect();
    }
}

From source file:org.apache.calcite.avatica.remote.AvaticaCommonsHttpClientImplTest.java

@Test
public void testRetryOnHttp503() throws Exception {
    final byte[] requestBytes = "fake_request".getBytes(UTF_8);
    final CloseableHttpResponse badResponse = mock(CloseableHttpResponse.class);
    final CloseableHttpResponse goodResponse = mock(CloseableHttpResponse.class);
    final StatusLine badStatusLine = mock(StatusLine.class);
    final StatusLine goodStatusLine = mock(StatusLine.class);
    final StringEntity responseEntity = new StringEntity("success");
    final Answer<CloseableHttpResponse> failThenSucceed = new Answer<CloseableHttpResponse>() {
        private int iteration = 0;

        @Override/*from  w ww  .  ja va 2s.c  o m*/
        public CloseableHttpResponse answer(InvocationOnMock invocation) throws Throwable {
            iteration++;
            if (1 == iteration) {
                return badResponse;
            } else {
                return goodResponse;
            }
        }
    };

    final AvaticaCommonsHttpClientImpl client = mock(AvaticaCommonsHttpClientImpl.class);

    when(client.send(any(byte[].class))).thenCallRealMethod();
    when(client.execute(any(HttpPost.class), any(HttpClientContext.class))).then(failThenSucceed);

    when(badResponse.getStatusLine()).thenReturn(badStatusLine);
    when(badStatusLine.getStatusCode()).thenReturn(HttpURLConnection.HTTP_UNAVAILABLE);

    when(goodResponse.getStatusLine()).thenReturn(goodStatusLine);
    when(goodStatusLine.getStatusCode()).thenReturn(HttpURLConnection.HTTP_OK);
    when(goodResponse.getEntity()).thenReturn(responseEntity);

    byte[] responseBytes = client.send(requestBytes);
    assertEquals("success", new String(responseBytes, UTF_8));
}

From source file:org.deegree.framework.util.HttpUtils.java

/**
 * validates passed URL. If it is not a valid URL or a client can not connect to it an exception will be thrown
 * //from   w ww  . j a  v  a 2 s  . co m
 * @param url
 * @param user
 * @param password
 * @throws IOException
 */
public static int validateURL(String url, String user, String password) throws IOException {
    if (url.startsWith("http:")) {
        URL tmp = new URL(url);
        HeadMethod hm = new HeadMethod(url);
        setHTTPCredentials(hm, user, password);
        InetAddress.getByName(tmp.getHost());
        HttpClient client = new HttpClient();
        client.executeMethod(hm);
        if (hm.getStatusCode() != HttpURLConnection.HTTP_OK) {
            if (hm.getStatusCode() != HttpURLConnection.HTTP_UNAUTHORIZED && hm.getStatusCode() != 401) {
                // this method just evaluates if a URL/host is valid; it does not takes care
                // if authorization is available/valid
                throw new IOException("Host " + tmp.getHost() + " of URL + " + url + " does not exists");
            }
        }
        return hm.getStatusCode();
    } else if (url.startsWith("file:")) {
        URL tmp = new URL(url);
        InputStream is = tmp.openStream();
        is.close();
        return 200;
    }
    return HttpURLConnection.HTTP_UNAVAILABLE;
}

From source file:co.cask.cdap.client.util.RESTClient.java

private HttpResponse execute(HttpRequest request, int... allowedErrorCodes)
        throws IOException, UnauthorizedException, DisconnectedException {

    int currentTry = 0;
    HttpResponse response;//from  w ww  .  jav  a  2 s .  c  om
    do {
        onRequest(request, currentTry);
        response = HttpRequests.execute(request, clientConfig.getDefaultRequestConfig());

        int responseCode = response.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
            currentTry++;
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                break;
            }
            continue;
        }

        onResponse(request, response, currentTry);
        if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
            throw new UnauthorizedException("Unauthorized status code received from the server.");
        }
        if (!isSuccessful(responseCode) && !ArrayUtils.contains(allowedErrorCodes, responseCode)) {
            throw new IOException(responseCode + ": " + response.getResponseBodyAsString());
        }
        return response;
    } while (currentTry <= clientConfig.getUnavailableRetryLimit());
    return response;
}

From source file:org.opencastproject.deliver.itunesu.HTTPHelper.java

/**
 * Throws exception if status code is not 200.
 *//*from  w ww.java2 s . co m*/
private void checkStatusCode() {
    int statusCode = 0;
    String errorMessage = "";

    try {
        statusCode = connection.getResponseCode();
        errorMessage = connection.getResponseMessage();
    } catch (IOException e) {
        throw new RuntimeException("Error in HTTP connection!");
    }

    if (statusCode != HttpURLConnection.HTTP_OK) {
        switch (statusCode) {
        case HttpURLConnection.HTTP_CLIENT_TIMEOUT:
            errorMessage = "Request Time-Out";
            break;
        case HttpURLConnection.HTTP_INTERNAL_ERROR:
            errorMessage = "Internal Server Error";
            break;
        case HttpURLConnection.HTTP_UNAVAILABLE:
            errorMessage = "Service Unavailable";
            break;
        }

        throw new RuntimeException(errorMessage);
    }
}

From source file:org.apache.calcite.avatica.remote.AvaticaCommonsHttpClientImplTest.java

@Test
public void testRetryOnMissingHttpResponse() throws Exception {
    final byte[] requestBytes = "fake_request".getBytes(UTF_8);
    final CloseableHttpResponse badResponse = mock(CloseableHttpResponse.class);
    final CloseableHttpResponse goodResponse = mock(CloseableHttpResponse.class);
    final StatusLine badStatusLine = mock(StatusLine.class);
    final StatusLine goodStatusLine = mock(StatusLine.class);
    final StringEntity responseEntity = new StringEntity("success");
    final Answer<CloseableHttpResponse> failThenSucceed = new Answer<CloseableHttpResponse>() {
        private int iteration = 0;

        @Override//from   w  ww . ja  v  a2s.  c om
        public CloseableHttpResponse answer(InvocationOnMock invocation) throws Throwable {
            iteration++;
            if (1 == iteration) {
                throw new NoHttpResponseException("The server didn't respond!");
            } else {
                return goodResponse;
            }
        }
    };

    final AvaticaCommonsHttpClientImpl client = mock(AvaticaCommonsHttpClientImpl.class);

    when(client.send(any(byte[].class))).thenCallRealMethod();
    when(client.execute(any(HttpPost.class), any(HttpClientContext.class))).then(failThenSucceed);

    when(badResponse.getStatusLine()).thenReturn(badStatusLine);
    when(badStatusLine.getStatusCode()).thenReturn(HttpURLConnection.HTTP_UNAVAILABLE);

    when(goodResponse.getStatusLine()).thenReturn(goodStatusLine);
    when(goodStatusLine.getStatusCode()).thenReturn(HttpURLConnection.HTTP_OK);
    when(goodResponse.getEntity()).thenReturn(responseEntity);

    byte[] responseBytes = client.send(requestBytes);
    assertEquals("success", new String(responseBytes, UTF_8));
}

From source file:ORG.oclc.os.ipUseThrottleFilter.ipUseThrottleFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    String longAddr = null, shortAddr, s, transactionKey = null;
    int count;//from w  w  w .j a va2s  .c  o  m
    boolean ignorable = false;

    synchronized (simultaneousRequestsByShortIPAddr) {
        if (totalSimultaneousRequests >= maxTotalSimultaneousRequests) {
            log.error("This system has exceeded the maxTotalSimultaneousRequests limit of "
                    + maxTotalSimultaneousRequests);
            log.error(simultaneousRequestsByShortIPAddr);
            for (String str : simultaneousRequests)
                log.error(str);
            ((HttpServletResponse) response).setStatus(HttpURLConnection.HTTP_UNAVAILABLE);
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.println("<html><body><h1>Service Temporarily Unavailable</h1>");
            writer.println(
                    "The system is experiencing a severe load and is temporarily unable to accept new requests");
            if (contactInfo != null)
                writer.println("<p>Contact " + contactInfo + " for more information</p>");
            writer.println("</body></html>");
            writer.close();
            return;
        }
        if (addressInHeader != null) {
            @SuppressWarnings("unchecked")
            Enumeration<String> addrs = ((HttpServletRequest) request).getHeaders(addressInHeader);
            while (addrs.hasMoreElements()) {
                longAddr = addrs.nextElement();
                if (longAddr == null) {
                    if (++addressInHeaderErrorCount < 10)
                        log.error("Expected a " + addressInHeader + " header but got null");
                    continue;
                }
                if (longAddr.lastIndexOf('.') >= 0)
                    break;
            }
        }
        if (longAddr == null)
            longAddr = request.getRemoteAddr();
        int i = longAddr.lastIndexOf('.');
        if (i < 0) {
            log.error("bogus IP address: '" + longAddr + "'");
            longAddr = "0.0.0.0";
        }
        shortAddr = longAddr.substring(0, i); // trim off 4th number group
        // that lets us spot requests from clusters
        s = equivalentAddresses.get(shortAddr); // map one short addr to another?
        if (s != null)
            shortAddr = s;
        if (ignorableAddresses.contains(shortAddr)) {
            ignorable = true;
        } else {
            Integer icount = simultaneousRequestsByShortIPAddr.get(shortAddr);
            if (icount != null)
                count = icount;
            else
                count = 0;

            int maxSimultaneousRequests = (maxTotalSimultaneousRequests - totalSimultaneousRequests) / 4;
            if (maxSimultaneousRequests == 0)
                maxSimultaneousRequests = 1;
            if (count >= maxSimultaneousRequests) {
                log.error("IP addr " + shortAddr + ".* has exceeded " + maxSimultaneousRequests
                        + " simultaneous requests!");
                log.error("maxTotalSimultaneousRequests=" + maxTotalSimultaneousRequests);
                log.error("totalSimultaneousRequests=" + totalSimultaneousRequests);
                for (String str : simultaneousRequests)
                    log.error(str);
                //                ((HttpServletResponse)response).setStatus(HttpURLConnection.HTTP_TOO_MANY_REQUESTS); // someday
                ((HttpServletResponse) response).setStatus(429); // too many requests
                response.setContentType("text/html");
                PrintWriter writer = response.getWriter();
                writer.println(
                        "<html><head><title>Too Many Requests</title></head><body><h1>Too Many Requests</h1>");
                writer.println("You have exceeded the maximum simultaneous request value of "
                        + maxSimultaneousRequests);
                writer.println("<p>This message and your IP address have been logged and reported</p>");
                if (contactInfo != null)
                    writer.println("<p>Contact " + contactInfo + " for more information</p>");
                writer.println("</body></html>");
                writer.close();
                return;
            }
            simultaneousRequestsByShortIPAddr.put(shortAddr, count + 1);
            icount = totalRequests.get(shortAddr);
            if (icount != null)
                count = icount;
            else
                count = 0;
            totalRequests.put(shortAddr, count + 1);
            totalSimultaneousRequests++;
            transactionKey = new StringBuilder((new Date(System.currentTimeMillis())).toString()).append('|')
                    .append(shortAddr).append('|').append(((HttpServletRequest) request).getQueryString())
                    .toString();
            simultaneousRequests.add(transactionKey);
        }
    }

    try {
        HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) response);
        chain.doFilter(request, wrapper);
    } finally {
        if (!ignorable)
            synchronized (simultaneousRequestsByShortIPAddr) {
                totalSimultaneousRequests--;
                simultaneousRequests.remove(transactionKey);
                count = simultaneousRequestsByShortIPAddr.get(shortAddr);
                if (count == 1) // prune them from the table
                    simultaneousRequestsByShortIPAddr.remove(shortAddr);
                else
                    simultaneousRequestsByShortIPAddr.put(shortAddr, count - 1);
            }
    }

    Calendar c = new GregorianCalendar();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    if (hour == 0 && nextReportingHour == 24) { // new day!
        // you could reset your daily limits table here
        nextReportingHour = 0;
    }

    if (hour >= nextReportingHour) { // generate the hourly report
        // you could reset your hourly limits table here
        nextReportingHour = hour + 1;

        if (log.isInfoEnabled()) {
            HashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
            List<String> yourMapKeys = new ArrayList<String>(totalRequests.keySet());
            List<Integer> yourMapValues = new ArrayList<Integer>(totalRequests.values());
            TreeSet<Integer> sortedSet = new TreeSet<Integer>(yourMapValues);
            Integer[] sortedArray = sortedSet.descendingSet().toArray(new Integer[0]);
            int size = sortedArray.length;

            for (int i = 0; i < size; i++)
                map.put(yourMapKeys.get(yourMapValues.indexOf(sortedArray[i])), sortedArray[i]);
            Iterator<String> it = map.keySet().iterator();
            String key;
            StringBuilder sb = new StringBuilder("Top 10 users in the last hour");
            for (int i = 0; i < 10 && it.hasNext(); i++) {
                key = it.next();
                sb.append("\n    ").append(key).append(" : ").append(map.get(key));
            }
            log.info(sb);
        }
        totalRequests.clear();
    }
}

From source file:org.eclipse.hono.client.impl.HonoClientImpl.java

/**
 * Checks if this client is currently connected to the server.
 * //from w  w  w.  j a v  a  2  s  .c o  m
 * @return A succeeded future if this client is connected.
 */
protected final Future<Void> checkConnected() {

    final Future<Void> result = Future.future();
    if (isConnectedInternal()) {
        result.complete();
    } else {
        result.fail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "not connected"));
    }
    return result;
}

From source file:org.apache.calcite.avatica.remote.AvaticaCommonsHttpClientImpl.java

public byte[] send(byte[] request) {
    while (true) {
        HttpClientContext context = HttpClientContext.create();

        context.setTargetHost(host);/*  w ww . j a va 2  s.  c o  m*/

        // Set the credentials if they were provided.
        if (null != this.credentials) {
            context.setCredentialsProvider(credentialsProvider);
            context.setAuthSchemeRegistry(authRegistry);
            context.setAuthCache(authCache);
        }

        ByteArrayEntity entity = new ByteArrayEntity(request, ContentType.APPLICATION_OCTET_STREAM);

        // Create the client with the AuthSchemeRegistry and manager
        HttpPost post = new HttpPost(uri);
        post.setEntity(entity);

        try (CloseableHttpResponse response = execute(post, context)) {
            final int statusCode = response.getStatusLine().getStatusCode();
            if (HttpURLConnection.HTTP_OK == statusCode
                    || HttpURLConnection.HTTP_INTERNAL_ERROR == statusCode) {
                return EntityUtils.toByteArray(response.getEntity());
            } else if (HttpURLConnection.HTTP_UNAVAILABLE == statusCode) {
                LOG.debug("Failed to connect to server (HTTP/503), retrying");
                continue;
            }

            throw new RuntimeException("Failed to execute HTTP Request, got HTTP/" + statusCode);
        } catch (NoHttpResponseException e) {
            // This can happen when sitting behind a load balancer and a backend server dies
            LOG.debug("The server failed to issue an HTTP response, retrying");
            continue;
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            LOG.debug("Failed to execute HTTP request", e);
            throw new RuntimeException(e);
        }
    }
}