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:com.netflix.genie.server.services.impl.GenieExecutionServiceImpl.java

/** {@inheritDoc} */
@Override//  w w w  . j av  a2 s  . c  o  m
public JobInfoResponse submitJob(JobInfoRequest jir) {
    logger.info("called");

    JobInfoResponse response;
    JobInfoElement jInfo = jir.getJobInfo();

    // validate parameters
    try {
        validateJobParams(jInfo);
    } catch (CloudServiceException e) {
        response = new JobInfoResponse(e);
        return response;
    }

    // ensure that job won't overload system
    // synchronize until an entry is created and INIT-ed in DB
    // throttling related parameters
    int maxRunningJobs = conf.getInt("netflix.genie.server.max.running.jobs", 0);
    int jobForwardThreshold = conf.getInt("netflix.genie.server.forward.jobs.threshold", 0);
    int maxIdleHostThreshold = conf.getInt("netflix.genie.server.max.idle.host.threshold", 0);
    int idleHostThresholdDelta = conf.getInt("netflix.genie.server.idle.host.threshold.delta", 0);
    synchronized (this) {
        try {
            int numRunningJobs = JobCountManager.getNumInstanceJobs();
            logger.info("Number of running jobs: " + numRunningJobs);

            // find an instance with fewer than (numRunningJobs -
            // idleHostThresholdDelta)
            int idleHostThreshold = numRunningJobs - idleHostThresholdDelta;
            // if numRunningJobs is already >= maxRunningJobs, forward
            // aggressively
            // but cap it at the max
            if ((idleHostThreshold > maxIdleHostThreshold) || (numRunningJobs >= maxRunningJobs)) {
                idleHostThreshold = maxIdleHostThreshold;
            }

            // check to see if job should be forwarded - only forward it
            // once. the assumption is that jobForwardThreshold < maxRunningJobs
            // (set in properties file)
            if ((numRunningJobs >= jobForwardThreshold) && (!jInfo.isForwarded())) {
                logger.info(
                        "Number of running jobs greater than forwarding threshold - trying to auto-forward");
                String idleHost = JobCountManager.getIdleInstance(idleHostThreshold);
                if (!idleHost.equals(NetUtil.getHostName())) {
                    jInfo.setForwarded(true);
                    stats.incrGenieForwardedJobs();
                    response = forwardJobRequest(
                            "http://" + idleHost + ":" + serverPort + "/" + jobResourcePrefix, jir);
                    return response;
                } // else, no idle hosts found - run here if capacity exists
            }

            if (numRunningJobs >= maxRunningJobs) {
                // if we get here, job can't be forwarded to an idle
                // instance anymore and current node is overloaded
                response = new JobInfoResponse(new CloudServiceException(HttpURLConnection.HTTP_UNAVAILABLE,
                        "Number of running jobs greater than system limit (" + maxRunningJobs
                                + ") - try another instance or try again later"));
                return response;
            }

            // if job can be launched, update the URIs
            buildJobURIs(jInfo);
        } catch (CloudServiceException e) {
            response = new JobInfoResponse(e);
            logger.error(response.getErrorMsg(), e);
            return response;
        }

        // init state in DB - return if job already exists
        try {
            pm.createEntity(jInfo);
        } catch (RollbackException e) {
            logger.error("Can't create entity in the database", e);
            if (e.getCause() instanceof EntityExistsException) {
                logger.error(e.getCause().getMessage());
                // most likely entity already exists - return useful message
                response = new JobInfoResponse(new CloudServiceException(HttpURLConnection.HTTP_CONFLICT,
                        "Job already exists for id: " + jInfo.getJobID()));
                return response;
            } else {
                // unknown exception - send it back
                response = new JobInfoResponse(new CloudServiceException(HttpURLConnection.HTTP_INTERNAL_ERROR,
                        "Received exception: " + e.getCause()));
                return response;
            }
        }
    } // end synchronize

    // increment number of submitted jobs
    stats.incrGenieJobSubmissions();

    // try to run the job - return success or error
    try {
        JobManagerFactory.getJobManager(jInfo.getJobType()).launch(jInfo);

        // update entity in DB
        jInfo.setUpdateTime(System.currentTimeMillis());
        pm.updateEntity(jInfo);

        // verification
        jInfo = pm.getEntity(jInfo.getJobID(), JobInfoElement.class);

        // return successful response
        response = new JobInfoResponse();
        response.setMessage("Successfully launched job: " + jInfo.getJobID());
        response.setJob(jInfo);
        return response;
    } catch (Exception e) {
        logger.error("Failed to submit job: ", e);
        // update db
        jInfo.setJobStatus(JobStatus.FAILED, e.getMessage());
        jInfo.setUpdateTime(System.currentTimeMillis());
        pm.updateEntity(jInfo);
        // increment counter for failed jobs
        stats.incrGenieFailedJobs();
        // if it is a known exception, handle differently
        if (e instanceof CloudServiceException) {
            response = new JobInfoResponse((CloudServiceException) e);
        } else {
            response = new JobInfoResponse(
                    new CloudServiceException(HttpURLConnection.HTTP_INTERNAL_ERROR, e.getMessage()));
        }
        return response;
    }
}

From source file:com.scvngr.levelup.core.net.LevelUpResponseTest.java

/**
 * Tests {@link com.scvngr.levelup.core.net.LevelUpResponse#mapStatus(int, String)}.
 *///from   ww w . j  a v  a  2  s .c om
@SmallTest
public void testMapStatusHttp_errorCodeMaintenenace() {
    assertEquals(LevelUpStatus.ERROR_MAINTENANCE,
            LevelUpResponse.mapStatus(HttpURLConnection.HTTP_UNAVAILABLE, SERVER_LEVELUP_PLATFORM));
}

From source file:org.hyperic.hq.plugin.netservices.HTTPCollector.java

private double getAvail(int code) {
    // There are too many options to list everything that is
    // successful. So, instead we are going to call out the
    // things that should be considered failure, everything else
    // is OK./*from   w  w w .  j  a  va 2 s . c o  m*/
    switch (code) {
    case HttpURLConnection.HTTP_BAD_REQUEST:
    case HttpURLConnection.HTTP_FORBIDDEN:
    case HttpURLConnection.HTTP_NOT_FOUND:
    case HttpURLConnection.HTTP_BAD_METHOD:
    case HttpURLConnection.HTTP_CLIENT_TIMEOUT:
    case HttpURLConnection.HTTP_CONFLICT:
    case HttpURLConnection.HTTP_PRECON_FAILED:
    case HttpURLConnection.HTTP_ENTITY_TOO_LARGE:
    case HttpURLConnection.HTTP_REQ_TOO_LONG:
    case HttpURLConnection.HTTP_INTERNAL_ERROR:
    case HttpURLConnection.HTTP_NOT_IMPLEMENTED:
    case HttpURLConnection.HTTP_UNAVAILABLE:
    case HttpURLConnection.HTTP_VERSION:
    case HttpURLConnection.HTTP_BAD_GATEWAY:
    case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
        return Metric.AVAIL_DOWN;
    default:
    }

    if (hasCredentials()) {
        if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {
            return Metric.AVAIL_DOWN;
        }
    }

    return Metric.AVAIL_UP;
}

From source file:com.scvngr.levelup.core.net.LevelUpResponseTest.java

/**
 * Tests {@link com.scvngr.levelup.core.net.LevelUpResponse#mapStatus(int, String)}. Maintenance
 * mode applies even for responses not coming directly from LevelUp Platform.
 *///from  w ww.  ja  va  2  s. c om
@SmallTest
public void testMapStatusHttp_errorCodeMaintenenace_notFromPlatform() {
    assertEquals(LevelUpStatus.ERROR_MAINTENANCE,
            LevelUpResponse.mapStatus(HttpURLConnection.HTTP_UNAVAILABLE, SERVER_NOT_LEVELUP_PLATFORM));
}

From source file:org.intermine.webservice.client.util.HttpConnection.java

/**
 * Handles an error response received while executing a service request.
 * Throws a {@link ServiceException} or one of its subclasses, depending on
 * the failure conditions./* www .j a  v  a 2s.  c  om*/
 *
 * @throws ServiceException exception describing the failure.
 * @throws IOException error reading the error response from the
 *         service.
 */
protected void handleErrorResponse() throws IOException {

    String message = executedMethod.getResponseBodyAsString();
    try {
        JSONObject jo = new JSONObject(message);
        message = jo.getString("error");
    } catch (JSONException e) {
        // Pass
    }

    switch (executedMethod.getStatusCode()) {

    case HttpURLConnection.HTTP_NOT_FOUND:
        throw new ResourceNotFoundException(this);

    case HttpURLConnection.HTTP_BAD_REQUEST:
        if (message != null) {
            throw new BadRequestException(message);
        } else {
            throw new BadRequestException(this);
        }
    case HttpURLConnection.HTTP_FORBIDDEN:
        if (message != null) {
            throw new ServiceForbiddenException(message);
        } else {
            throw new ServiceForbiddenException(this);
        }
    case HttpURLConnection.HTTP_NOT_IMPLEMENTED:
        throw new NotImplementedException(this);

    case HttpURLConnection.HTTP_INTERNAL_ERROR:
        if (message != null) {
            throw new InternalErrorException(message);
        } else {
            throw new InternalErrorException(this);
        }
    case HttpURLConnection.HTTP_UNAVAILABLE:
        throw new ServiceUnavailableException(this);

    default:
        if (message != null) {
            throw new ServiceException(message);
        } else {
            throw new ServiceException(this);
        }
    }
}

From source file:org.oclc.oai.harvester2.verb.HarvesterVerb.java

/**
 * Preforms the OAI request//from  w w  w.j a  v a2 s .  c  o  m
 *
 * @param requestURL
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
public void harvestOldOclcImplementation(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.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();
    }

    InputSource data = new InputSource(in);

    Thread t = Thread.currentThread();
    DocumentBuilder builder = (DocumentBuilder) builderMap.get(t);
    if (builder == null) {
        builder = factory.newDocumentBuilder();
        builderMap.put(t, builder);
    }
    doc = builder.parse(data);

    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();
}

From source file:org.oclc.oai.harvester2.verb.HarvesterVerb.java

public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
    String requestURL = "https://databank.ora.ox.ac.uk/oaipmh?verb=ListRecords&resumptionToken=20121206_TKMGW4A_SWT3NHV";

    //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=ListRecords&resumptionToken=1354116062009:ELibM_external:eudml-article2:33753:37054::";
    //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=GetRecord&identifier=urn:eudml.eu:ELibM_external:05152756&metadataPrefix=eudml-article2";
    //String requestURL = "C:/Users/Gilberto Pedrosa/Desktop/OAIHandler.xml";
    //FileInputStream fis = new FileInputStream(requestURL);
    //InputStream in = fis;
    logger.debug("requestURL=" + requestURL);
    DocumentBuilderFactory factory;
    factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from w  w w  .  j av a 2  s. c o  m*/
    Thread t = Thread.currentThread();
    DocumentBuilder builder = factory.newDocumentBuilder();
    HashMap builderMap = new HashMap();
    builderMap.put(t, builder);

    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));

    String xmlString = new String(inputBytes, "UTF-8");
    xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString);

    builder.parse(data);

    System.out.println("data = " + data);
}

From source file:com.google.apphosting.vmruntime.VmApiProxyDelegateTest.java

private void callDelegateWithHttpError(boolean sync, ApiProxyException expectedException) throws Exception {
    HttpClient mockClient = createMockHttpClient();
    HttpResponse mockHttpResponse = createMockHttpResponse("Error from RPC proxy".getBytes(),
            HttpURLConnection.HTTP_UNAVAILABLE);
    when(mockClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(HttpContext.class)))
            .thenReturn(mockHttpResponse);

    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) {//from w  w  w  .  j a  v  a2 s. c o  m
        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.harvester.verb.HarvesterVerb.java

/**
 * Performs the OAI request//from w  ww  .  j  av  a2 s .c  o  m
 * 
 * @param requestURL
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
public void harvestOldOclcImplementation(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.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();
    }

    InputSource data = new InputSource(in);

    Thread t = Thread.currentThread();
    DocumentBuilder builder = builderMap.get(t);
    if (builder == null) {
        builder = factory.newDocumentBuilder();
        builderMap.put(t, builder);
    }
    doc = builder.parse(data);

    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();
}

From source file:org.oclc.oai.harvester.verb.HarvesterVerb.java

/**
 * @param args//  w  ww. ja  v  a  2  s.c om
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
    String requestURL = "https://databank.ora.ox.ac.uk/oaipmh?verb=ListRecords&resumptionToken=20121206_TKMGW4A_SWT3NHV";

    //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=ListRecords&resumptionToken=1354116062009:ELibM_external:eudml-article2:33753:37054::";
    //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=GetRecord&identifier=urn:eudml.eu:ELibM_external:05152756&metadataPrefix=eudml-article2";
    //String requestURL = "C:/Users/Gilberto Pedrosa/Desktop/OAIHandler.xml";
    //FileInputStream fis = new FileInputStream(requestURL);
    //InputStream in = fis;
    logger.debug("requestURL=" + requestURL);
    DocumentBuilderFactory factory;
    factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    Thread t = Thread.currentThread();
    DocumentBuilder builder = factory.newDocumentBuilder();
    HashMap<Thread, DocumentBuilder> builderMap = new HashMap<Thread, DocumentBuilder>();
    builderMap.put(t, builder);

    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));

    String xmlString = new String(inputBytes, "UTF-8");
    xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString);

    builder.parse(data);

    System.out.println("data = " + data);
}