List of usage examples for org.apache.commons.httpclient.methods OptionsMethod getResponseBodyAsStream
@Override public InputStream getResponseBodyAsStream() throws IOException
From source file:com.moss.bdbadmin.client.service.BdbClient.java
public Category map(IdProof assertion) throws ServiceException { try {/*from ww w.j a va2 s .c o m*/ OptionsMethod method = new OptionsMethod(baseUrl); method.setRequestHeader(AuthenticationHeader.HEADER_NAME, AuthenticationHeader.encode(assertion)); int result = httpClient.executeMethod(method); if (result != 200) { throw new ServiceException(result); } else { Unmarshaller u = context.createUnmarshaller(); return (Category) u.unmarshal(method.getResponseBodyAsStream()); } } catch (Exception ex) { throw new ServiceFailure(ex); } }
From source file:com.moss.bdbadmin.client.service.BdbClient.java
public EntryInfo entryInfo(IdProof assertion, String path) throws ServiceException { try {/* w w w .j av a 2s. co m*/ OptionsMethod method = new OptionsMethod(baseUrl + "/" + path); method.setRequestHeader(AuthenticationHeader.HEADER_NAME, AuthenticationHeader.encode(assertion)); int result = httpClient.executeMethod(method); if (result != 200) { throw new ServiceException(result); } else { Unmarshaller u = context.createUnmarshaller(); return (EntryInfo) u.unmarshal(method.getResponseBodyAsStream()); } } catch (Exception ex) { throw new ServiceFailure(ex); } }
From source file:edu.indiana.d2i.registryext.RegistryExtAgent.java
/** * Only return file names (no directory) * /*from ww w . j av a 2s. c om*/ * @param repoPath * path in registry * @return * @throws RegistryExtException * @throws ClientProtocolException * @throws IOException * @throws IllegalStateException * @throws JAXBException * @throws OAuthProblemException * @throws OAuthSystemException */ public ListResourceResponse getAllChildren(String repoPath) throws RegistryExtException, ClientProtocolException, IOException, IllegalStateException, JAXBException, OAuthSystemException, OAuthProblemException { Map<String, Object> session = ActionContext.getContext().getSession(); String accessToken = (String) session.get(Constants.SESSION_TOKEN); int statusCode = 200; String requestURL = composeURL(FILEOPPREFIX, repoPath); if (logger.isDebugEnabled()) { logger.debug("List request URL=" + requestURL); } HttpClient httpclient = new HttpClient(); OptionsMethod options = new OptionsMethod(requestURL); options.addRequestHeader("Accept", "application/xml"); options.addRequestHeader("Authorization", "Bearer " + accessToken); try { httpclient.executeMethod(options); statusCode = options.getStatusLine().getStatusCode(); /* handle token expiration */ if (statusCode == 401) { logger.info(String.format("Access token %s expired, going to refresh it", accessToken)); refreshToken(session); // use refreshed access token accessToken = (String) session.get(Constants.SESSION_TOKEN); options.addRequestHeader("Authorization", "Bearer " + accessToken); // re-send the request httpclient.executeMethod(options); statusCode = options.getStatusLine().getStatusCode(); } if (statusCode == 404) { logger.equals("List request URL=" + requestURL + " doesn't exist"); return new ListResourceResponse(null, statusCode); } if (statusCode != 200) { throw new RegistryExtException("Failed in listing children : HTTP error code : " + options.getStatusLine().getStatusCode()); } Entry xmlFile = FileSchemaUtil.readConfigXML(options.getResponseBodyAsStream()); if (logger.isDebugEnabled()) { logger.debug(String.format("Returned content:%n%s", FileSchemaUtil.toXMLString(xmlFile))); } Entries entries = xmlFile.getEntries(); return new ListResourceResponse(entries, statusCode); } finally { if (options != null) options.releaseConnection(); } }
From source file:org.exjello.mail.Exchange2003Connection.java
private void signOn() throws Exception { HttpClient client = getClient();// w ww. j av a 2 s. co m URL serverUrl = new URL(server); String host = serverUrl.getHost(); int port = serverUrl.getPort(); if (port == -1) port = serverUrl.getDefaultPort(); AuthScope authScope = new AuthScope(host, port); if (username.indexOf("\\") < 0) { client.getState().setCredentials(authScope, new UsernamePasswordCredentials(username, password)); } else { // Try to connect with NTLM authentication String domainUser = username.substring(username.indexOf("\\") + 1, username.length()); String domain = username.substring(0, username.indexOf("\\")); client.getState().setCredentials(authScope, new NTCredentials(domainUser, password, host, domain)); } boolean authenticated = false; OptionsMethod authTest = new OptionsMethod(server + "/exchange"); try { authenticated = (client.executeMethod(authTest) < 400); } finally { try { InputStream stream = authTest.getResponseBodyAsStream(); byte[] buf = new byte[65536]; try { if (session.getDebug()) { PrintStream log = session.getDebugOut(); log.println("Response Body:"); int count; while ((count = stream.read(buf, 0, 65536)) != -1) { log.write(buf, 0, count); } log.flush(); log.println(); } else { while (stream.read(buf, 0, 65536) != -1) ; } } catch (Exception ignore) { } finally { try { stream.close(); } catch (Exception ignore2) { } } } finally { authTest.releaseConnection(); } } if (!authenticated) { PostMethod op = new PostMethod(server + SIGN_ON_URI); op.setRequestHeader("Content-Type", FORM_URLENCODED_CONTENT_TYPE); op.addParameter("destination", server + "/exchange"); op.addParameter("flags", "0"); op.addParameter("username", username); op.addParameter("password", password); try { int status = client.executeMethod(op); if (status >= 400) { throw new IllegalStateException("Sign-on failed: " + status); } } finally { try { InputStream stream = op.getResponseBodyAsStream(); byte[] buf = new byte[65536]; try { if (session.getDebug()) { PrintStream log = session.getDebugOut(); log.println("Response Body:"); int count; while ((count = stream.read(buf, 0, 65536)) != -1) { log.write(buf, 0, count); } log.flush(); log.println(); } else { while (stream.read(buf, 0, 65536) != -1) ; } } catch (Exception ignore) { } finally { try { stream.close(); } catch (Exception ignore2) { } } } finally { op.releaseConnection(); } } } findInbox(); }