Java Code Examples for org.apache.commons.httpclient.methods.GetMethod#getResponseBodyAsStream()

The following examples show how to use org.apache.commons.httpclient.methods.GetMethod#getResponseBodyAsStream() . 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: NetworkUtil.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
public static byte[] loadUrlData(String url) throws Exception {
	HttpClient httpClient = new HttpClient();
	setHttpClientProxyFromSystem(httpClient, url);
	GetMethod get = new GetMethod(url);		
	get.setFollowRedirects(true);
	
	try {			
		httpClient.getParams().setParameter("http.connection.timeout", 5000);
		int httpReturnCode = httpClient.executeMethod(get); 

		if (httpReturnCode == 200) {
			// Don't use get.getResponseBody, it causes a warning in log
			try (InputStream inputStream = get.getResponseBodyAsStream()) {
				return IOUtils.toByteArray(inputStream);
			}
		} else {
			throw new Exception("ERROR: Received httpreturncode " + httpReturnCode);
		}
	} finally {
		get.releaseConnection();
	}
}
 
Example 2
Source File: UriUtils.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public static List<String> getMetalinkChecksums(String url) {
    HttpClient httpClient = getHttpClient();
    GetMethod getMethod = new GetMethod(url);
    try {
        if (httpClient.executeMethod(getMethod) == HttpStatus.SC_OK) {
            InputStream is = getMethod.getResponseBodyAsStream();
            Map<String, List<String>> checksums = getMultipleValuesFromXML(is, new String[] {"hash"});
            if (checksums.containsKey("hash")) {
                List<String> listChksum = new ArrayList<>();
                for (String chk : checksums.get("hash")) {
                    listChksum.add(chk.replaceAll("\n", "").replaceAll(" ", "").trim());
                }
                return listChksum;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        getMethod.releaseConnection();
    }
    return null;
}
 
Example 3
Source File: UriUtils.java    From cosmic with Apache License 2.0 6 votes vote down vote up
public static InputStream getInputStreamFromUrl(final String url, final String user, final String password) {

        try {
            final Pair<String, Integer> hostAndPort = validateUrl(url);
            final HttpClient httpclient = new HttpClient(new MultiThreadedHttpConnectionManager());
            if ((user != null) && (password != null)) {
                httpclient.getParams().setAuthenticationPreemptive(true);
                final Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
                httpclient.getState().setCredentials(new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM), defaultcreds);
                s_logger.info("Added username=" + user + ", password=" + password + "for host " + hostAndPort.first() + ":" + hostAndPort.second());
            }
            // Execute the method.
            final GetMethod method = new GetMethod(url);
            final int statusCode = httpclient.executeMethod(method);

            if (statusCode != HttpStatus.SC_OK) {
                s_logger.error("Failed to read from URL: " + url);
                return null;
            }

            return method.getResponseBodyAsStream();
        } catch (final Exception ex) {
            s_logger.error("Failed to read from URL: " + url);
            return null;
        }
    }
 
Example 4
Source File: HttpClient3Util.java    From oim-fx with MIT License 5 votes vote down vote up
public static String get(String url) {
	String body = null;
	GetMethod getMethod = new GetMethod(url);
	InputStream in = null;
	BufferedReader reader = null;
	try {

		getMethod.addRequestHeader("Content-type", "text/html;charset=UTF-8");
		getMethod.getParams().setContentCharset("UTF-8");

		HttpClient client = new HttpClient();

		client.getHttpConnectionManager().getParams().setConnectionTimeout(6000);
		client.executeMethod(getMethod);
		in = getMethod.getResponseBodyAsStream();

		body = getMethod.getResponseBodyAsString();
	} catch (Exception ex) {
		ex.printStackTrace();
	} finally {
		try {
			if (null != in) {
				in.close();
			}
			if (null != reader) {
				reader.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		getMethod.releaseConnection();
	}
	return body;
}
 
Example 5
Source File: ClientMediaEntry.java    From rome with Apache License 2.0 5 votes vote down vote up
private InputStream getResourceAsStream() throws ProponoException {
    if (getEditURI() == null) {
        throw new ProponoException("ERROR: not yet saved to server");
    }
    final GetMethod method = new GetMethod(((Content) getContents()).getSrc());
    try {
        getCollection().getHttpClient().executeMethod(method);
        if (method.getStatusCode() != 200) {
            throw new ProponoException("ERROR HTTP status=" + method.getStatusCode());
        }
        return method.getResponseBodyAsStream();
    } catch (final IOException e) {
        throw new ProponoException("ERROR: getting media entry", e);
    }
}
 
Example 6
Source File: QueueInformation.java    From sequenceiq-samples with Apache License 2.0 5 votes vote down vote up
public void printQueueInfo(HttpClient client, ObjectMapper mapper, String schedulerResourceURL) {
	
	//http://sandbox.hortonworks.com:8088/ws/v1/cluster/scheduler in case of HDP
	GetMethod get = new GetMethod(schedulerResourceURL);
    get.setRequestHeader("Accept", "application/json");
    try {
        int statusCode = client.executeMethod(get);

        if (statusCode != HttpStatus.SC_OK) {
        	LOGGER.error("Method failed: " + get.getStatusLine());
        }
        
		InputStream in = get.getResponseBodyAsStream();
		
		JsonNode jsonNode = mapper.readValue(in, JsonNode.class);
		ArrayNode queues = (ArrayNode) jsonNode.path("scheduler").path("schedulerInfo").path("queues").get("queue");
		for (int i = 0; i < queues.size(); i++) {
			JsonNode queueNode = queues.get(i);						
			LOGGER.info("queueName / usedCapacity / absoluteUsedCap / absoluteCapacity / absMaxCapacity: " + 
					queueNode.findValue("queueName") + " / " +
					queueNode.findValue("usedCapacity") + " / " + 
					queueNode.findValue("absoluteUsedCapacity") + " / " + 
					queueNode.findValue("absoluteCapacity") + " / " +
					queueNode.findValue("absoluteMaxCapacity"));
		}
	} catch (IOException e) {
		LOGGER.error("Exception occured", e);
	} finally {	        
		get.releaseConnection();
	}	      
        
}
 
Example 7
Source File: UserMgmtITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testPortrait() throws IOException, URISyntaxException {
    final URL portraitUrl = RepositoryEntriesITCase.class.getResource("portrait.jpg");
    assertNotNull(portraitUrl);
    final File portrait = new File(portraitUrl.toURI());

    final HttpClient c = loginWithCookie("rest-one", "A6B7C8");

    // upload portrait
    final String request = "/users/" + id1.getKey() + "/portrait";
    final PostMethod method = createPost(request, MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", portrait), new StringPart("filename", "portrait.jpg") };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
    final int code = c.executeMethod(method);
    assertEquals(code, 200);
    method.releaseConnection();

    // check if big and small portraits exist
    final DisplayPortraitManager dps = DisplayPortraitManager.getInstance();
    final File uploadDir = dps.getPortraitDir(id1);
    assertTrue(new File(uploadDir, DisplayPortraitManager.PORTRAIT_SMALL_FILENAME).exists());
    assertTrue(new File(uploadDir, DisplayPortraitManager.PORTRAIT_BIG_FILENAME).exists());

    // check get portrait
    final String getRequest = "/users/" + id1.getKey() + "/portrait";
    final GetMethod getMethod = createGet(getRequest, MediaType.APPLICATION_OCTET_STREAM, true);
    final int getCode = c.executeMethod(getMethod);
    assertEquals(getCode, 200);
    final InputStream in = getMethod.getResponseBodyAsStream();
    int b = 0;
    int count = 0;
    while ((b = in.read()) > -1) {
        count++;
    }
    assertEquals(-1, b);// up to end of file
    assertTrue(count > 1000);// enough bytes
    method.releaseConnection();
}
 
Example 8
Source File: UserMgmtITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testPortrait() throws IOException, URISyntaxException {
    final URL portraitUrl = RepositoryEntriesITCase.class.getResource("portrait.jpg");
    assertNotNull(portraitUrl);
    final File portrait = new File(portraitUrl.toURI());

    final HttpClient c = loginWithCookie("rest-one", "A6B7C8");

    // upload portrait
    final String request = "/users/" + id1.getKey() + "/portrait";
    final PostMethod method = createPost(request, MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", portrait), new StringPart("filename", "portrait.jpg") };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
    final int code = c.executeMethod(method);
    assertEquals(code, 200);
    method.releaseConnection();

    // check if big and small portraits exist
    final DisplayPortraitManager dps = DisplayPortraitManager.getInstance();
    final File uploadDir = dps.getPortraitDir(id1);
    assertTrue(new File(uploadDir, DisplayPortraitManager.PORTRAIT_SMALL_FILENAME).exists());
    assertTrue(new File(uploadDir, DisplayPortraitManager.PORTRAIT_BIG_FILENAME).exists());

    // check get portrait
    final String getRequest = "/users/" + id1.getKey() + "/portrait";
    final GetMethod getMethod = createGet(getRequest, MediaType.APPLICATION_OCTET_STREAM, true);
    final int getCode = c.executeMethod(getMethod);
    assertEquals(getCode, 200);
    final InputStream in = getMethod.getResponseBodyAsStream();
    int b = 0;
    int count = 0;
    while ((b = in.read()) > -1) {
        count++;
    }
    assertEquals(-1, b);// up to end of file
    assertTrue(count > 1000);// enough bytes
    method.releaseConnection();
}
 
Example 9
Source File: RestRuntimeExceptionMappingTest.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
private ExceptionToJson readResponse(GetMethod httpGetMethod) throws IOException {
    ObjectMapper jsonMapper = new ObjectMapper();
    jsonMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    InputStream is = httpGetMethod.getResponseBodyAsStream();
    return jsonMapper.readValue(is, ExceptionToJson.class);
}
 
Example 10
Source File: BundleDataUtil.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
public static Map<String, String> getData(OsgiClient osgiClient, String bundleSymbolicName) throws OsgiClientException {
    Map<String, String> answer = new HashMap<>();
    RepositoryInfo repositoryInfo = getRepositoryInfo(osgiClient);
    GetMethod method = new GetMethod(repositoryInfo.appendPath("system/console/bundles/" + bundleSymbolicName + ".json"));
    HttpClient client = getHttpClient(osgiClient);

    try {
        int result = client.executeMethod(method);
        if (result != HttpStatus.SC_OK) {
            throw new HttpException("Got status code " + result + " for call to " + method.getURI());
        }
        try ( InputStream input = method.getResponseBodyAsStream() ) {
            JSONObject object = new JSONObject(new JSONTokener(new InputStreamReader(input)));
            JSONArray bundleData = object.getJSONArray("data");
            if(bundleData.length() > 1) {
                throw new OsgiClientException("More than one Bundle found");
            } else {
                JSONObject bundle = bundleData.getJSONObject(0);
                for(Object item: bundle.keySet()) {
                    String name = item + "";
                    String value = bundle.get(name) + "";
                    answer.put(name, value);
                }
            }
        }
    } catch (IOException | JSONException e) {
        throw new OsgiClientException(e);
    } finally {
        method.releaseConnection();
    }
    return answer;
}
 
Example 11
Source File: AbstractSolrAdminHTTPClient.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Executes an action or a command in SOLR using REST API 
 * 
 * @param httpClient HTTP Client to be used for the invocation
 * @param url Complete URL of SOLR REST API Endpoint
 * @return A JSON Object including SOLR response
 * @throws UnsupportedEncodingException
 */
protected JSONObject getOperation(HttpClient httpClient, String url) throws UnsupportedEncodingException 
{

    GetMethod get = new GetMethod(url);
    
    try {
        
        httpClient.executeMethod(get);
        if(get.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || get.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY)
        {
            Header locationHeader = get.getResponseHeader("location");
            if (locationHeader != null)
            {
                String redirectLocation = locationHeader.getValue();
                get.setURI(new URI(redirectLocation, true));
                httpClient.executeMethod(get);
            }
        }
        if (get.getStatusCode() != HttpServletResponse.SC_OK)
        {
            throw new LuceneQueryParserException("Request failed " + get.getStatusCode() + " " + url.toString());
        }

        Reader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream(), get.getResponseCharSet()));
        JSONObject json = new JSONObject(new JSONTokener(reader));
        return json;
        
    }
    catch (IOException | JSONException e) 
    {
        throw new AlfrescoRuntimeException(e.getMessage(), e);
    } 
    finally 
    {
        get.releaseConnection();
    }
}
 
Example 12
Source File: DavExchangeSession.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ExchangeSession.ContactPhoto getContactPhoto(ExchangeSession.Contact contact) throws IOException {
    ContactPhoto contactPhoto;
    final GetMethod method = new GetMethod(URIUtil.encodePath(contact.getHref()) + "/ContactPicture.jpg");
    method.setRequestHeader("Translate", "f");
    method.setRequestHeader("Accept-Encoding", "gzip");

    InputStream inputStream = null;
    try {
        DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, true);
        if (DavGatewayHttpClientFacade.isGzipEncoded(method)) {
            inputStream = (new GZIPInputStream(method.getResponseBodyAsStream()));
        } else {
            inputStream = method.getResponseBodyAsStream();
        }

        contactPhoto = new ContactPhoto();
        contactPhoto.contentType = "image/jpeg";

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream partInputStream = inputStream;
        IOUtil.write(partInputStream, baos);
        contactPhoto.content = IOUtil.encodeBase64AsString(baos.toByteArray());
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                LOGGER.debug(e);
            }
        }
        method.releaseConnection();
    }
    return contactPhoto;
}
 
Example 13
Source File: GenBaseCase.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
public static BaseCase genURLFuzzBaseCase(Manager manager, String fuzzStart, String FuzzEnd)
        throws MalformedURLException, IOException {
    BaseCase baseCase = null;
    int failcode = 0;
    String failString = Config.failCaseString;
    String baseResponce = "";

    /*
     * markers for using regex instead
     */
    boolean useRegexInstead = false;
    String regex = null;

    URL failurl = new URL(fuzzStart + failString + FuzzEnd);

    GetMethod httpget = new GetMethod(failurl.toString());
    // set the custom HTTP headers
    Vector HTTPheaders = manager.getHTTPHeaders();
    for (int a = 0; a < HTTPheaders.size(); a++) {
        HTTPHeader httpHeader = (HTTPHeader) HTTPheaders.elementAt(a);
        /*
         * Host header has to be set in a different way!
         */
        if (httpHeader.getHeader().startsWith("Host")) {
            httpget.getParams().setVirtualHost(httpHeader.getValue());
        } else {
            httpget.setRequestHeader(httpHeader.getHeader(), httpHeader.getValue());
        }
    }
    httpget.setFollowRedirects(Config.followRedirects);

    // save the http responce code for the base case
    failcode = manager.getHttpclient().executeMethod(httpget);
    manager.workDone();

    if (failcode == 200) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Base case for " + failurl.toString() + " came back as 200!");
        }

        BufferedReader input =
                new BufferedReader(new InputStreamReader(httpget.getResponseBodyAsStream()));
        String tempLine;
        StringBuffer buf = new StringBuffer();
        while ((tempLine = input.readLine()) != null) {
            buf.append("\r\n" + tempLine);
        }
        baseResponce = buf.toString();
        input.close();

        // clean up the base case, based on the basecase URL
        baseResponce = FilterResponce.CleanResponce(baseResponce, failurl, failString);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Base case was set to: " + baseResponce);
        }
    }

    httpget.releaseConnection();

    /*
     * create the base case object
     */
    baseCase =
            new BaseCase(
                    null, failcode, false, failurl, baseResponce, null, useRegexInstead, regex);

    return baseCase;
}
 
Example 14
Source File: GenBaseCase.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
private static String getBaseCaseAgain(Manager manager, URL failurl, String failString)
        throws IOException {
    int failcode;
    String baseResponce = "";

    GetMethod httpget = new GetMethod(failurl.toString());
    // set the custom HTTP headers
    Vector HTTPheaders = manager.getHTTPHeaders();
    for (int a = 0; a < HTTPheaders.size(); a++) {
        HTTPHeader httpHeader = (HTTPHeader) HTTPheaders.elementAt(a);
        /*
         * Host header has to be set in a different way!
         */
        if (httpHeader.getHeader().startsWith("Host")) {
            httpget.getParams().setVirtualHost(httpHeader.getValue());
        } else {
            httpget.setRequestHeader(httpHeader.getHeader(), httpHeader.getValue());
        }
    }
    httpget.setFollowRedirects(Config.followRedirects);

    // save the http responce code for the base case
    failcode = manager.getHttpclient().executeMethod(httpget);
    manager.workDone();

    // we now need to get the content as we need a base case!
    if (failcode == 200) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Base case for " + failurl.toString() + " came back as 200!");
        }

        BufferedReader input =
                new BufferedReader(new InputStreamReader(httpget.getResponseBodyAsStream()));
        String tempLine;
        StringBuffer buf = new StringBuffer();
        while ((tempLine = input.readLine()) != null) {
            buf.append("\r\n" + tempLine);
        }
        baseResponce = buf.toString();
        input.close();

        // HTMLparse.parseHTML();

        // HTMLparse htmlParse = new HTMLparse(baseResponce, null);
        // Thread parse  = new Thread(htmlParse);
        // parse.start();

        // clean up the base case, based on the basecase URL
        baseResponce = FilterResponce.CleanResponce(baseResponce, failurl, failString);

        httpget.releaseConnection();

        /*
         * return the cleaned responce
         */
        return baseResponce;
    } else {
        /*
         * we have a big problem here as the server has returned an other responce code, for the same request
         * TODO: think of a way to deal with this!
         */
        return null;
    }
}
 
Example 15
Source File: BasicAuthWSDLLocator.java    From development with Apache License 2.0 4 votes vote down vote up
private InputSource createInputSource(String url) throws IOException {
    final HttpClient client = new HttpClient();
    final String proxyHost = System.getProperty("http.proxyHost");
    final String proxyPort = System.getProperty("http.proxyPort", "80");
    if (proxyHost != null && proxyHost.trim().length() > 0) {
        try {
            client.getHostConfiguration().setProxy(proxyHost.trim(),
                    Integer.parseInt(proxyPort));
        } catch (NumberFormatException e) {
            logger.logError(Log4jLogger.SYSTEM_LOG, e,
                    LogMessageIdentifier.ERROR_USE_PROXY_DEFINITION_FAILED);
        }
    }

    // Set credentials if specified:
    URL targetURL = new URL(url);
    if (userName != null && userName.length() > 0) {
        client.getParams().setAuthenticationPreemptive(true);
        final Credentials credentials = new UsernamePasswordCredentials(
                userName, password);
        client.getState().setCredentials(
                new AuthScope(targetURL.getHost(), targetURL.getPort(),
                        AuthScope.ANY_REALM), credentials);
    }

    // Retrieve content:
    // opening a local resource isn't supported by apache
    // instead open stream directly
    if (targetURL.getProtocol().startsWith("file")) {
        in = targetURL.openStream();
        return new InputSource(in);
    }
    final GetMethod method = new GetMethod(url);
    final int status = client.executeMethod(method);
    if (status != HttpStatus.SC_OK) {
        throw new IOException("Error " + status + " while retrieving "
                + url);
    }
    in = method.getResponseBodyAsStream();
    return new InputSource(in);
}
 
Example 16
Source File: SalesforceProvisioningConnector.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * @return
 * @throws IdentityProvisioningException
 */
public String listUsers(String query) throws IdentityProvisioningException {

    if (log.isTraceEnabled()) {
        log.trace("Starting listUsers() of " + SalesforceProvisioningConnector.class);
    }
    boolean isDebugEnabled = log.isDebugEnabled();

    if (StringUtils.isBlank(query)) {
        query = SalesforceConnectorDBQueries.SALESFORCE_LIST_USER_SIMPLE_QUERY;
    }

    HttpClient httpclient = new HttpClient();
    GetMethod get = new GetMethod(this.getDataQueryEndpoint());
    setAuthorizationHeader(get);

    // set the SOQL as a query param
    NameValuePair[] params = new NameValuePair[1];
    params[0] = new NameValuePair("q", query);
    get.setQueryString(params);

    StringBuilder sb = new StringBuilder();
    try {
        httpclient.executeMethod(get);
        if (get.getStatusCode() == HttpStatus.SC_OK) {

            JSONObject response = new JSONObject(new JSONTokener(new InputStreamReader(
                    get.getResponseBodyAsStream())));
            if (isDebugEnabled) {
                log.debug("Query response: " + response.toString(2));
            }

            // Build the returning string
            sb.append(response.getString("totalSize") + " record(s) returned\n\n");
            JSONArray results = response.getJSONArray("records");
            for (int i = 0; i < results.length(); i++) {
                sb.append(results.getJSONObject(i).getString("Id") + ", "

                        + results.getJSONObject(i).getString("Alias") + ", "
                        + results.getJSONObject(i).getString("Email") + ", "
                        + results.getJSONObject(i).getString("LastName") + ", "
                        + results.getJSONObject(i).getString("Name") + ", "
                        + results.getJSONObject(i).getString("ProfileId") + ", "
                        + results.getJSONObject(i).getString("Username") + "\n");
            }
            sb.append("\n");
        } else {
            log.error("recieved response status code:" + get.getStatusCode() + " text : "
                    + get.getStatusText());
        }
    } catch (JSONException | IOException e) {
        log.error("Error in invoking provisioning operation for the user listing");
        throw new IdentityProvisioningException(e);
    }finally {
        get.releaseConnection();
    }

    if (isDebugEnabled) {
        log.debug("Returning string : " + sb.toString());
    }

    if (log.isTraceEnabled()) {
        log.trace("Ending listUsers() of " + SalesforceProvisioningConnector.class);
    }
    return sb.toString();
}
 
Example 17
Source File: HttpFileObject.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
public HttpInputStream(final GetMethod method) throws IOException {
    super(method.getResponseBodyAsStream());
    this.method = method;
}
 
Example 18
Source File: ChangeLogListener.java    From RestServices with Apache License 2.0 4 votes vote down vote up
void startConnection() throws IOException,
		HttpException {
	String requestUrl = getChangesRequestUrl(true);
	
	GetMethod get = this.currentRequest = new GetMethod(requestUrl);
	get.setRequestHeader(RestServices.HEADER_ACCEPT, RestServices.CONTENTTYPE_APPLICATIONJSON);
	
	RestConsumer.includeHeaders(get, headers);
	int status = RestConsumer.client.executeMethod(get);
	try {
		if (status != IMxRuntimeResponse.OK)
			throw new RuntimeException("Failed to setup stream to " + url +  ", status: " + status);

		InputStream inputStream = get.getResponseBodyAsStream();
	
		JSONTokener jt = new JSONTokener(inputStream);
		JSONObject instr = null;
		
		try {
			isConnected  = true;
			while(true) {
				instr = new JSONObject(jt);
				
				processChange(instr);
			}
		}
		catch(InterruptedException e2) {
			cancelled = true;
			RestServices.LOGCONSUME.warn("Changefeed interrupted", e2);
		}
		catch(Exception e) {
			//Not graceful disconnected?
			if (!cancelled && !(jt.end() && e instanceof JSONException))
				throw new RuntimeException(e);
		}
	}
	finally {
		isConnected = false;
		get.releaseConnection();
	}
}
 
Example 19
Source File: HttpAlfrescoContentReader.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private ReadableByteChannel getDirectReadableChannelImpl() throws ContentIOException
{
    String contentUrl = getContentUrl();
    
    try
    {
        if (!exists())
        {
            throw new IOException("Content doesn't exist");
        }
        String ticket = transactionService.getRetryingTransactionHelper().doInTransaction(ticketCallback, false, true);
        String url = HttpAlfrescoContentReader.generateURL(baseHttpUrl, contentUrl, ticket, false);

        GetMethod method = new GetMethod(url);
        int statusCode = httpClient.executeMethod(method);
        if (statusCode == HttpServletResponse.SC_OK)
        {
            // Get the stream from the request
            InputStream contentStream = method.getResponseBodyAsStream();
            // Attach a listener to the stream to ensure that the HTTP request is cleaned up
            this.addListener(new StreamCloseListener(method));
            // Done
            if (logger.isDebugEnabled())
            {
                logger.debug("\n" +
                        "HttpReader retrieve intput stream: \n" +
                        "   Reader: " + this + "\n" +
                        "   Server: " + baseHttpUrl);
            }
            return Channels.newChannel(contentStream);
        }
        else
        {
            // The content exists, but we failed to get it
            throw new IOException("Failed to get content remote content that supposedly exists.");
        }
    }
    catch (Throwable e)
    {
        throw new ContentIOException(
                "Failed to open stream: \n" +
                "   Reader:        " + this + "\n" +
                "   Remote server: " + baseHttpUrl,
                e);
    }
}
 
Example 20
Source File: HttpFileObject.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
public HttpInputStream(final GetMethod method, final int bufferSize) throws IOException {
    super(method.getResponseBodyAsStream(), bufferSize);
    this.method = method;
}