List of usage examples for org.apache.commons.httpclient HttpMethodBase getStatusCode
@Override public int getStatusCode()
From source file:JiraWebSessionCallback.java
protected boolean expectRedirect(HttpMethodBase method, String page, boolean fullMatch) throws JiraException { if (method.getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY) { return false; }//from w w w .j ava2 s.c o m Header locationHeader = method.getResponseHeader("location"); //$NON-NLS-1$ if (locationHeader == null) { throw new JiraRedirectException(); } String url = locationHeader.getValue(); if (fullMatch) { // only if followRedirects is enabled the baseUrl is guaranteed to match the redirect url, otherwise the repository might be sending back a different url if ((followRedirects && !url.startsWith(baseUrl + page)) || (!followRedirects && !url.endsWith(page))) { throw new JiraRedirectException(url); } } else { // the client does not know exactly where the repository will redirect to if (!url.contains(page)) { throw new JiraRedirectException(url); } } return true; }
From source file:com.assemblade.client.AbstractClient.java
protected int executeMethod(HttpMethodBase method) throws CallFailedException { try {// ww w .j av a2s .com generateSignature(method); client.executeMethod(method); return method.getStatusCode(); } catch (Exception e) { throw new CallFailedException("Method execution failed", e); } }
From source file:com.cloud.network.bigswitch.BigSwitchVnsApi.java
protected void executeMethod(HttpMethodBase method) throws BigSwitchVnsApiException { try {/* ww w. java2 s . c o m*/ _client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { method.releaseConnection(); // login and try again login(); _client.executeMethod(method); } } catch (HttpException e) { s_logger.error("HttpException caught while trying to connect to the BigSwitch Controller", e); method.releaseConnection(); throw new BigSwitchVnsApiException("API call to BigSwitch Controller Failed", e); } catch (IOException e) { s_logger.error("IOException caught while trying to connect to the BigSwitch Controller", e); method.releaseConnection(); throw new BigSwitchVnsApiException("API call to BigSwitch Controller Failed", e); } }
From source file:com.cloud.utils.rest.RESTValidationStrategy.java
public void executeMethod(final HttpMethodBase method, final HttpClient client, final String protocol) throws CloudstackRESTException, HttpException, IOException { if (host == null || host.isEmpty() || user == null || user.isEmpty() || password == null || password.isEmpty()) {/*from www.j av a 2 s . c om*/ throw new CloudstackRESTException("Hostname/credentials are null or empty"); } client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { method.releaseConnection(); // login and try again login(protocol, client); client.executeMethod(method); } }
From source file:com.cloud.network.bigswitch.BigSwitchBcfApi.java
protected void executeMethod(final HttpMethodBase method) throws BigSwitchBcfApiException { try {//w w w. ja va2 s . c o m _client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { method.releaseConnection(); } } catch (HttpException e) { S_LOGGER.error("HttpException caught while trying to connect to the BigSwitch Controller", e); method.releaseConnection(); throw new BigSwitchBcfApiException("API call to BigSwitch Controller Failed", e); } catch (IOException e) { S_LOGGER.error("IOException caught while trying to connect to the BigSwitch Controller", e); method.releaseConnection(); throw new BigSwitchBcfApiException("API call to BigSwitch Controller Failed", e); } }
From source file:com.cloud.network.bigswitch.BigSwitchBcfApi.java
private String checkResponse(final HttpMethodBase m, final String errorMessageBase) throws BigSwitchBcfApiException, IllegalArgumentException { String customErrorMsg = null; if (m.getStatusCode() == HttpStatus.SC_OK) { String hash = ""; if (m.getResponseHeader(HASH_MATCH) != null) { hash = m.getResponseHeader(HASH_MATCH).getValue(); set_hash(hash);/* w ww . ja va2 s. c o m*/ } return hash; } if (m.getStatusCode() == HttpStatus.SC_CONFLICT) { if (m instanceof GetMethod) { return HASH_CONFLICT; } throw new BigSwitchBcfApiException("BCF topology sync required", true); } if (m.getStatusCode() == HttpStatus.SC_SEE_OTHER) { isMaster = false; set_hash(HASH_IGNORE); return HASH_IGNORE; } if (m.getStatusCode() == HttpStatus.SC_NOT_FOUND) { if (m instanceof DeleteMethod) { return ""; } } if (m.getStatusCode() == HttpStatus.SC_BAD_REQUEST) { customErrorMsg = " Invalid data in BCF request"; throw new IllegalArgumentException(customErrorMsg); } String errorMessage = responseToErrorMessage(m); m.releaseConnection(); S_LOGGER.error(errorMessageBase + errorMessage); throw new BigSwitchBcfApiException(errorMessageBase + errorMessage + customErrorMsg); }
From source file:com.cloud.network.nicira.NiciraNvpApi.java
private void executeMethod(HttpMethodBase method) throws NiciraNvpApiException { try {//from w w w . ja v a 2 s .c o m _client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { method.releaseConnection(); // login and try again login(); _client.executeMethod(method); } } catch (HttpException e) { s_logger.error("HttpException caught while trying to connect to the Nicira NVP Controller", e); throw new NiciraNvpApiException("API call to Nicira NVP Controller Failed", e); } catch (IOException e) { s_logger.error("IOException caught while trying to connect to the Nicira NVP Controller", e); throw new NiciraNvpApiException("API call to Nicira NVP Controller Failed", e); } }
From source file:fedora.server.security.servletfilters.pubcookie.ConnectPubcookie.java
public final void connect(String urlString, Map requestParameters, Cookie[] requestCookies, String truststoreLocation, String truststorePassword) { log.debug(this.getClass().getName() + ".connect() " + " url==" + urlString + " requestParameters==" + requestParameters + " requestCookies==" + requestCookies); responseCookies2 = null;/*from ww w .java 2 s. c o m*/ URL url = null; try { url = new URL(urlString); } catch (MalformedURLException mue) { log.error(this.getClass().getName() + ".connect() " + "bad configured url==" + urlString); } if (urlString.startsWith("https:") && null != truststoreLocation && !"".equals(truststoreLocation) && null != truststorePassword && !"".equals(truststorePassword)) { log.debug("setting " + FilterPubcookie.TRUSTSTORE_LOCATION_KEY + " to " + truststoreLocation); System.setProperty(FilterPubcookie.TRUSTSTORE_LOCATION_KEY, truststoreLocation); log.debug("setting " + FilterPubcookie.TRUSTSTORE_PASSWORD_KEY + " to " + truststorePassword); System.setProperty(FilterPubcookie.TRUSTSTORE_PASSWORD_KEY, truststorePassword); log.debug("setting " + FilterPubcookie.KEYSTORE_LOCATION_KEY + " to " + truststoreLocation); System.setProperty(FilterPubcookie.KEYSTORE_LOCATION_KEY, truststoreLocation); log.debug("setting " + FilterPubcookie.KEYSTORE_PASSWORD_KEY + " to " + truststorePassword); System.setProperty(FilterPubcookie.KEYSTORE_PASSWORD_KEY, truststorePassword); System.setProperty("javax.net.debug", "ssl,handshake,data,trustmanager"); } else { log.debug("DIAGNOSTIC urlString==" + urlString); log.debug("didn't set " + FilterPubcookie.TRUSTSTORE_LOCATION_KEY + " to " + truststoreLocation); log.debug("didn't set " + FilterPubcookie.TRUSTSTORE_PASSWORD_KEY + " to " + truststorePassword); } /* * log.debug("\n-a-"); Protocol easyhttps = null; try { easyhttps = new * Protocol("https", (ProtocolSocketFactory) new * EasySSLProtocolSocketFactory(), 443); } catch (Throwable t) { * log.debug(t); log.debug(t.getMessage()); if (t.getCause() != null) * log.debug(t.getCause().getMessage()); } log.debug("\n-b-"); * Protocol.registerProtocol("https", easyhttps); log.debug("\n-c-"); */ HttpClient client = new HttpClient(); log.debug(this.getClass().getName() + ".connect() " + " b4 calling setup"); log.debug(this.getClass().getName() + ".connect() requestCookies==" + requestCookies); HttpMethodBase method = setup(client, url, requestParameters, requestCookies); log.debug(this.getClass().getName() + ".connect() " + " after calling setup"); int statusCode = 0; try { log.debug(this.getClass().getName() + ".connect() " + " b4 calling executeMethod"); client.executeMethod(method); log.debug(this.getClass().getName() + ".connect() " + " after calling executeMethod"); statusCode = method.getStatusCode(); log.debug( this.getClass().getName() + ".connect() " + "(with configured url) statusCode==" + statusCode); } catch (Exception e) { log.error(this.getClass().getName() + ".connect() " + "failed original connect, url==" + urlString); log.error(e); log.error(e.getMessage()); if (e.getCause() != null) { log.error(e.getCause().getMessage()); } e.printStackTrace(); } log.debug(this.getClass().getName() + ".connect() " + " status code==" + statusCode); if (302 == statusCode) { Header redirectHeader = method.getResponseHeader("Location"); if (redirectHeader != null) { String redirectString = redirectHeader.getValue(); if (redirectString != null) { URL redirectURL = null; try { redirectURL = new URL(redirectString); method = setup(client, redirectURL, requestParameters, requestCookies); } catch (MalformedURLException mue) { log.error(this.getClass().getName() + ".connect() " + "bad redirect, url==" + urlString); } statusCode = 0; try { client.executeMethod(method); statusCode = method.getStatusCode(); log.debug(this.getClass().getName() + ".connect() " + "(on redirect) statusCode==" + statusCode); } catch (Exception e) { log.error(this.getClass().getName() + ".connect() " + "failed redirect connect"); } } } } if (statusCode == 200) { // this is either the original, non-302, status code or the status code after redirect log.debug(this.getClass().getName() + ".connect() " + "status code 200"); String content = null; try { log.debug(this.getClass().getName() + ".connect() " + "b4 gRBAS()"); content = method.getResponseBodyAsString(); log.debug(this.getClass().getName() + ".connect() " + "after gRBAS() content==" + content); } catch (IOException e) { log.error(this.getClass().getName() + ".connect() " + "couldn't get content"); return; } if (content == null) { log.error(this.getClass().getName() + ".connect() content==null"); return; } else { log.debug(this.getClass().getName() + ".connect() content != null, about to new Tidy"); Tidy tidy = null; try { tidy = new Tidy(); } catch (Throwable t) { log.debug("new Tidy didn't"); log.debug(t); log.debug(t.getMessage()); if (t != null) { log.debug(t.getCause().getMessage()); } } log.debug(this.getClass().getName() + ".connect() after newing Tidy, tidy==" + tidy); byte[] inputBytes = content.getBytes(); log.debug(this.getClass().getName() + ".connect() A1"); ByteArrayInputStream inputStream = new ByteArrayInputStream(inputBytes); log.debug(this.getClass().getName() + ".connect() A2"); responseDocument = tidy.parseDOM(inputStream, null); //use returned root node as only output log.debug(this.getClass().getName() + ".connect() A3"); } log.debug(this.getClass().getName() + ".connect() " + "b4 getState()"); HttpState state = client.getState(); log.debug(this.getClass().getName() + ".connect() state==" + state); try { responseCookies2 = method.getRequestHeaders(); log.debug(this.getClass().getName() + ".connect() just got headers"); for (Header element : responseCookies2) { log.debug(this.getClass().getName() + ".connect() header==" + element); } responseCookies = state.getCookies(); log.debug(this.getClass().getName() + ".connect() responseCookies==" + responseCookies); } catch (Throwable t) { log.error(this.getClass().getName() + ".connect() exception==" + t.getMessage()); if (t.getCause() != null) { log.error(this.getClass().getName() + ".connect() cause==" + t.getCause().getMessage()); } } completedFully = true; log.debug(this.getClass().getName() + ".connect() completedFully==" + completedFully); } }
From source file:JiraWebClient.java
protected void handleErrorMessage(HttpMethodBase method) throws JiraException { try {// w ww. j a v a 2 s . c o m String response = method.getResponseBodyAsString(); // TODO consider logging the error if (method.getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) { throw new JiraRemoteException("JIRA system error", null); //$NON-NLS-1$ } if (response == null) { throw new JiraRemoteMessageException("Error making JIRA request: " + method.getStatusCode(), ""); //$NON-NLS-1$ //$NON-NLS-2$ } StringReader reader = new StringReader(response); try { StringBuilder msg = new StringBuilder(); HtmlStreamTokenizer tokenizer = new HtmlStreamTokenizer(reader, null); for (Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = tokenizer .nextToken()) { if (token.getType() == Token.TAG) { HtmlTag tag = (HtmlTag) token.getValue(); String classValue = tag.getAttribute("class"); //$NON-NLS-1$ if (classValue != null) { if (tag.getTagType() == Tag.DIV) { if (classValue.startsWith("infoBox") || classValue.startsWith("errorArea") //$NON-NLS-1$ //$NON-NLS-2$ || classValue.contains("error")) { //$NON-NLS-1$ throw new JiraRemoteMessageException(getContent(tokenizer, Tag.DIV)); } } else if (tag.getTagType() == Tag.SPAN) { if (classValue.startsWith("errMsg")) { //$NON-NLS-1$ msg.append(getContent(tokenizer, Tag.SPAN)); } } } } } if (msg.length() == 0) { throw new JiraRemoteMessageException(response); } else { throw new JiraRemoteMessageException(msg.toString()); } } catch (ParseException e) { throw new JiraRemoteMessageException("Error parsing JIRA response: " + method.getStatusCode(), ""); //$NON-NLS-1$ //$NON-NLS-2$ } finally { reader.close(); } } catch (IOException e) { throw new JiraException(e); } }
From source file:com.snaker.DownloadManager.java
private Runnable createRunnable(final Downloader d) { final Task task = d.getTask(); return new Runnable() { @Override//from w w w. ja v a 2 s .c o m public void run() { logger.info("start download:" + d.getUrl()); HttpClient client = clients.get(task.getId()); DownloadHandler handler = d.getHandler(); HttpMethodBase m = null; d.setStatus(Status.STARTED); d.setStartTime(System.currentTimeMillis()); try { String url = d.getUrl(); if (d.isGet()) { GetMethod get = new GetMethod(url); m = get; } else { final String requestCharset = d.getRequestCharset(); PostMethod post = new PostMethod(url) { public String getRequestCharSet() { if (requestCharset != null) return requestCharset; else return super.getRequestCharSet(); } public boolean getFollowRedirects() { return d.isFollowRedirects(); } }; if (requestCharset != null) { post.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=" + requestCharset); } DownloadParams parms = d.getParms(); if (parms != null) post.setRequestBody(parms.toNVP()); m = post; } { // set the headers m.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1"); m.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); m.setRequestHeader("Accept-Language", "en-us,zh-cn;q=0.5"); m.setRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); m.setRequestHeader("Referer", url); } client.executeMethod(m); //check status int sc = m.getStatusCode(); d.setStatusCode(sc); if (isBadStatusCode(sc)) { logger.error("download failed,url:" + d.getUrl() + ",Status Code:" + sc); d.setStatus(Status.FAILED); d.setDescription(m.getStatusText()); return; } else if (sc == 404 || sc == 410) { d.setStatus(Status.FINISHED); d.setDescription("NOT FOUND"); return; } long size = m.getResponseContentLength(); d.setFileSize(size); // get File Name if (d.getFileName() == null) { Header h = m.getResponseHeader("Content-Disposition"); String fileName = null; if (h != null) { String f = h.getValue(); int tag = f.indexOf("filename="); if (tag != -1 && tag != f.length() - 1) fileName = f.substring(tag + 1); } if (fileName == null || fileName.length() == 0) { int tag1 = url.lastIndexOf("/"); int tag2 = url.lastIndexOf("?"); if (tag1 != -1 && tag1 != url.length() - 1) { if (tag2 > tag1) { fileName = url.substring(tag1 + 1, tag2); } else { fileName = url.substring(tag1 + 1); } } } d.setFileName(fileName); } // set the all headers Header[] headers = m.getResponseHeaders(); if (headers != null) { for (Header header : headers) { d.addResponseHeader(header.getName(), header.getValue()); } } d.setStatus(Status.RUNNING); // recv the body if (handler == null) { byte[] content = m.getResponseBody(); int len = content.length; d.setFileSize(len); d.setReceived(len); d.setResponseCharset(m.getResponseCharSet()); d.setResponseBody(content); } else { InputStream is = m.getResponseBodyAsStream(); handler.start(d); byte[] buffer = new byte[102400]; long count = 0; while (true) { int r = is.read(buffer); if (r > 0) { count += r; d.setReceived(count); handler.handle(buffer, r); } else { break; } } is.close(); } d.setStatus(Status.FINISHED); } catch (Exception e) { logger.error("download failed,url:" + d.getUrl(), e); d.setStatus(Status.FAILED); d.setDescription(e.getMessage()); } finally { m.releaseConnection(); if (handler != null) { handler.stop(); } downloadings.remove(d); d.setEndTime(System.currentTimeMillis()); while (downloaded.size() >= maxDownloadedCount) { downloaded.poll(); } downloaded.offer(d); task.downloadFininshed(d); } } }; }