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

The following examples show how to use org.apache.commons.httpclient.methods.GetMethod#getResponseHeaders() . 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: CoursesResourcesFoldersITCase.java    From olat with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetFileDeep() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");
    final URI uri = UriBuilder.fromUri(getCourseFolderURI()).path("SubDir").path("SubSubDir").path("SubSubSubDir").path("3_singlepage.html").build();
    final GetMethod method = createGet(uri, "*/*", true);
    final int code = c.executeMethod(method);
    assertEquals(code, 200);

    final String body = method.getResponseBodyAsString();
    assertNotNull(body);
    assertTrue(body.startsWith("<html>"));

    String contentType = null;
    for (final Header header : method.getResponseHeaders()) {
        if ("Content-Type".equals(header.getName())) {
            contentType = header.getValue();
            break;
        }
    }
    assertNotNull(contentType);
    assertEquals("text/html", contentType);
}
 
Example 2
Source File: CoursesResourcesFoldersITCase.java    From olat with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetFileDeep() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");
    final URI uri = UriBuilder.fromUri(getCourseFolderURI()).path("SubDir").path("SubSubDir").path("SubSubSubDir").path("3_singlepage.html").build();
    final GetMethod method = createGet(uri, "*/*", true);
    final int code = c.executeMethod(method);
    assertEquals(code, 200);

    final String body = method.getResponseBodyAsString();
    assertNotNull(body);
    assertTrue(body.startsWith("<html>"));

    String contentType = null;
    for (final Header header : method.getResponseHeaders()) {
        if ("Content-Type".equals(header.getName())) {
            contentType = header.getValue();
            break;
        }
    }
    assertNotNull(contentType);
    assertEquals("text/html", contentType);
}
 
Example 3
Source File: TreeToolController.java    From webcurator with Apache License 2.0 5 votes vote down vote up
private byte[] fetchHTTPResponse(String url, String[] outStrings)
{
    GetMethod getMethod = new GetMethod(url);
    HttpClient client = new HttpClient();
    try 
    {
        int result = client.executeMethod(getMethod);
        if(result != HttpURLConnection.HTTP_OK)
        {
        	throw new HTTPGetException("HTTP GET Status="+result);
        }
        Header[] headers = getMethod.getResponseHeaders();
        StringBuffer buf = new StringBuffer();
        buf.append("HTTP/1.1 200 OK\n");
        for (int i=0; i<headers.length; i++) {
        	buf.append(headers[i].getName()+": ");
        	buf.append(headers[i].getValue()+"\n");
        	if (headers[i].getName().equalsIgnoreCase("content-type")) {outStrings[0]=headers[i].getValue();}
        }
        buf.append("\n");
        
        ByteArrayOutputStream os = new ByteArrayOutputStream(); 
        os.write(buf.toString().getBytes()); 
        os.write(getMethod.getResponseBody()); 

        return os.toByteArray();
    } 
    catch (HTTPGetException je)
    {
    	throw je;
    }
    catch (Exception e) 
    {
		throw new HTTPGetException("Unable to fetch content at "+url+".", e);
    }
    finally
    {
    	getMethod.releaseConnection();
    }
}
 
Example 4
Source File: MailingComponentImpl.java    From openemm with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean loadContentFromURL() {
	boolean returnValue = true;

	// return false;

	if ((type != TYPE_IMAGE) && (type != TYPE_ATTACHMENT)) {
		return false;
	}
	
	HttpClient httpClient = new HttpClient();
	String encodedURI = encodeURI(componentName);
	GetMethod get = new GetMethod(encodedURI);
	get.setFollowRedirects(true);
	
	try {
		NetworkUtil.setHttpClientProxyFromSystem(httpClient, encodedURI);
		
		httpClient.getParams().setParameter("http.connection.timeout", 5000);

		if (httpClient.executeMethod(get) == 200) {
			get.getResponseHeaders();
			
			// TODO: Due to data types of DB columns binblock and emmblock, replacing getResponseBody() cannot be replaced by safer getResponseBodyAsStream(). Better solutions?
			Header contentType = get.getResponseHeader("Content-Type");
			String contentTypeValue = "";
			if(contentType != null) {
				contentTypeValue = contentType.getValue();
			} else {
				logger.debug("No content-type in response from: " + encodedURI);
			}
			setBinaryBlock(get.getResponseBody(), contentTypeValue);
		}
	} catch (Exception e) {
		logger.error("loadContentFromURL: " + encodedURI, e);
		returnValue = false;
	} finally {
		get.releaseConnection();
	}
	
	if( logger.isInfoEnabled()) {
		logger.info("loadContentFromURL: loaded " + componentName);
	}
	
	return returnValue;
}