List of usage examples for org.apache.commons.httpclient HttpMethod releaseConnection
public abstract void releaseConnection();
From source file:edu.utah.further.core.ws.HttpClientTemplate.java
/** * Executes a HttpClient HttpMethod with the given settings of this class. This method * is not responsible for releasing the connection unless there is an exception. * Releasing of the connection is left to the caller since releasing the connection at * this point would be pointless; the caller would not be able to read from the * stream./*from ww w . jav a2 s .c o m*/ * * @param method * the method to execute * @return an open inputstream for reading. */ public int execute(final HttpMethod method) { Exception exception = null; try { return httpClient.executeMethod(method); } catch (final Exception e) { exception = e; throw new ApplicationException("An exception occured while invoking the Http Method", e); } finally { if (exception != null) { method.releaseConnection(); } } }
From source file:com.jdom.word.playdough.model.ServerCommunicationManagerImpl.java
public int getUserHighScoreForWord(String user, String word) { if (configuration.isCommunicateWithServer()) { HttpClient client = getHttpClient(); HttpMethod method = getHttpMethod(Constants.GET_HIGH_SCORE_URL + "?" + Constants.USER_KEY + "=" + user + "&" + Constants.WORD_KEY + "=" + word); try {/*w w w.jav a 2 s.c om*/ client.executeMethod(method); return Integer.parseInt(method.getResponseBodyAsString()); } catch (Exception e) { return cacheStatusMarker.getCachedUserHighScore(word); } finally { method.releaseConnection(); } } else { return cacheStatusMarker.getCachedUserHighScore(word); } }
From source file:net.sourceforge.jcctray.model.HTTPCruise.java
public void forceBuild(DashBoardProject project) throws Exception { HttpMethod method = httpMethod(project); try {//ww w . j a v a 2 s . c o m if (executeMethod(method, project.getHost()) != HttpStatus.SC_OK) throw new Exception( "There was an http error connecting to the server at " + project.getHost().getHostName()); if (!isInvokeSuccessful(method, project)) throw new Exception( "The force build was not successful, the server did not return what JCCTray was expecting."); } finally { method.releaseConnection(); } }
From source file:com.jdom.word.playdough.model.ServerCommunicationManagerImpl.java
public boolean isGamePackLocked(String username, String gamePackFileName) { if (configuration.isCommunicateWithServer()) { HttpClient client = getHttpClient(); HttpMethod method = getHttpMethod(Constants.GET_LOCK_STATUS_URL + "?" + Constants.USER_KEY + "=" + username + "&" + Constants.PACK_KEY + "=" + gamePackFileName); try {//from ww w. jav a 2 s . c om client.executeMethod(method); return Boolean.parseBoolean(method.getResponseBodyAsString()); } catch (Exception e) { return cacheStatusMarker.isGamePackLocked(username, gamePackFileName); } finally { method.releaseConnection(); } } else { return cacheStatusMarker.isGamePackLocked(username, gamePackFileName); } }
From source file:com.thoughtworks.twist.mingle.core.MingleClient.java
public boolean validate() { HttpMethod method = getMethod(loginUrl()); try {/*from w w w . jav a 2s. c o m*/ int httpStatus = executeMethod(method); switch (httpStatus) { case HttpStatus.SC_OK: return true; case HttpStatus.SC_UNAUTHORIZED: throw new MingleAuthenticationException(); } } finally { if (method != null) method.releaseConnection(); } return false; }
From source file:com.zimbra.cs.util.WebClientServiceUtil.java
/** * send service request to one random ui node, keep trying until succeeds. * @param serviceUrl the url that should be matched and handled by ServiceServlet in ZimbraWebClient * reqHeaders the map of req/rsp attributes that need to be set by the UI node * @return response from ui node in String * @throws ServiceException// w w w . j a v a2 s .c om */ public static String sendServiceRequestToOneRandomUiNode(String serviceUrl, Map<String, String> reqHeaders) throws ServiceException { List<Server> servers = Provisioning.getInstance().getAllServers(Provisioning.SERVICE_WEBCLIENT); if (servers == null || servers.isEmpty()) { servers.add(Provisioning.getInstance().getLocalServer()); } HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient(); HttpProxyUtil.configureProxy(client); AuthToken authToken = AuthProvider.getAdminAuthToken(); ZimbraLog.misc.debug("got admin auth token"); String resp = ""; for (Server server : servers) { if (isServerAtLeast8dot5(server)) { HttpMethod method = null; try { method = new GetMethod(URLUtil.getServiceURL(server, serviceUrl, false)); ZimbraLog.misc.debug("connecting to ui node %s", server.getName()); method.addRequestHeader(PARAM_AUTHTOKEN, authToken.getEncoded()); // Add all the req headers passed to this function as well for (Map.Entry<String, String> entry : reqHeaders.entrySet()) { method.addRequestHeader(entry.getKey(), entry.getValue()); ZimbraLog.misc.debug("adding request header %s=%s", entry.getKey(), entry.getValue()); } int result = HttpClientUtil.executeMethod(client, method); ZimbraLog.misc.debug("resp: %d", result); resp = method.getResponseBodyAsString(); ZimbraLog.misc.debug("got response from ui node: %s", resp); break; //try ui nodes one by one until one succeeds. } catch (IOException e) { ZimbraLog.misc.warn("failed to get response from ui node", e); } catch (AuthTokenException e) { ZimbraLog.misc.warn("failed to get authToken", e); } finally { if (method != null) { method.releaseConnection(); } } } } if (authToken != null && authToken.isRegistered()) { try { authToken.deRegister(); ZimbraLog.misc.debug("de-registered auth token, isRegistered?%s", authToken.isRegistered()); } catch (AuthTokenException e) { ZimbraLog.misc.warn("failed to de-register authToken", e); } } return resp; }
From source file:datasoul.util.OnlineUsageStats.java
@Override public void run() { // Check if enabled if (!ConfigObj.getActiveInstance().getOnlineUsageStatsBool()) { return;/*from w ww . j a v a2 s .c om*/ } // Check time from last update long last = 0; try { last = Long.parseLong(UsageStatsConfig.getInstance().getLastSent()); } catch (Exception e) { // ignore } if (System.currentTimeMillis() < last + UPD_INTERVAL) { return; } StringBuffer sb = new StringBuffer(); sb.append(OnlineUpdateCheck.ONLINE_BASE_URL + "datasoulweb?"); sb.append("sysid="); sb.append(UsageStatsConfig.getInstance().getID()); sb.append("&"); sb.append("osname="); try { sb.append(URLEncoder.encode(System.getProperty("os.name"), "UTF-8")); } catch (UnsupportedEncodingException ex) { sb.append("unknown"); } sb.append("&"); sb.append("osversion="); try { sb.append(URLEncoder.encode(System.getProperty("os.version"), "UTF-8")); } catch (UnsupportedEncodingException ex) { sb.append("unknown"); } sb.append("&"); sb.append("javaversion="); try { sb.append(URLEncoder.encode(System.getProperty("java.version"), "UTF-8")); } catch (UnsupportedEncodingException ex) { sb.append("unknown"); } sb.append("&"); sb.append("dsversion="); sb.append(DatasoulMainForm.getVersion()); sb.append("&"); sb.append("templates="); sb.append(TemplateManager.getInstance().getRowCount()); sb.append("&"); sb.append("songall="); sb.append(AllSongsListTable.getInstance().getSongCount()); sb.append("&"); sb.append("songchords="); sb.append(AllSongsListTable.getInstance().getSongsWithChords()); sb.append("&"); sb.append("numdisplays="); sb.append(OutputDevice.getNumDisplays()); sb.append("&"); sb.append("geometry1="); sb.append(ConfigObj.getActiveInstance().getMainOutputDeviceObj().getDiplayGeometry()); sb.append("&"); sb.append("geometry2="); if (ConfigObj.getActiveInstance().getMonitorOutputDeviceObj() != null) { sb.append(ConfigObj.getActiveInstance().getMonitorOutputDeviceObj().getDiplayGeometry()); } else { sb.append("disabled"); } HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(sb.toString()); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { UsageStatsConfig.getInstance().setLastSent(Long.toString(System.currentTimeMillis())); } } catch (Exception e) { e.printStackTrace(); } finally { // Release the connection. method.releaseConnection(); } }
From source file:com.thoughtworks.go.agent.launcher.ServerCall.java
public static ServerResponseWrapper invoke(HttpMethod method) throws Exception { HashMap<String, String> headers = new HashMap<String, String>(); HttpClient httpClient = new HttpClient(); httpClient.setConnectionTimeout(HTTP_TIMEOUT_IN_MILLISECONDS); try {/*from w ww.jav a 2s. c o m*/ final int status = httpClient.executeMethod(method); if (status == HttpStatus.SC_NOT_FOUND) { StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); out.println("Return Code: " + status); out.println("Few Possible Causes: "); out.println("1. Your Go Server is down or not accessible."); out.println( "2. This agent might be incompatible with your Go Server.Please fix the version mismatch between Go Server and Go Agent."); out.close(); throw new Exception(sw.toString()); } if (status != HttpStatus.SC_OK) { throw new Exception("Got status " + status + " " + method.getStatusText() + " from server"); } for (Header header : method.getResponseHeaders()) { headers.put(header.getName(), header.getValue()); } return new ServerResponseWrapper(headers, method.getResponseBodyAsStream()); } catch (Exception e) { String message = "Couldn't access Go Server with base url: " + method.getURI() + ": " + e.toString(); LOG.error(message); throw new Exception(message, e); } finally { method.releaseConnection(); } }
From source file:com.apifest.BasicTest.java
public String readResponse(HttpMethod method) throws IOException { HttpClient client = new HttpClient(); String response = null;//ww w . j ava 2 s .c o m InputStream in = null; try { int status = client.executeMethod(method); if (status >= HttpStatus.SC_OK) { in = method.getResponseBodyAsStream(); response = readInputStream(in); } } catch (HttpException e) { log.error("cannot read response", e); } catch (IOException e) { log.error("cannot read response", e); } finally { in.close(); method.releaseConnection(); } return response; }
From source file:com.kylinolap.common.restclient.RestClient.java
public String getKylinProperties() throws IOException { String url = baseUrl + "/admin/config"; HttpMethod get = new GetMethod(url); try {// www. j a v a2 s. c o m int code = client.executeMethod(get); String msg = Bytes.toString(get.getResponseBody()); JSONObject obj = new JSONObject(msg); msg = obj.getString("config"); if (code != 200) throw new IOException("Invalid response " + code + " with cache wipe url " + url + "\n" + msg); return msg; } catch (JSONException e) { throw new IOException("Error when parsing json response from REST"); } finally { get.releaseConnection(); } }