List of usage examples for org.apache.commons.httpclient HttpMethod getPath
public abstract String getPath();
From source file:org.hydracache.client.transport.HttpTransportTest.java
@Test public void testPutRequestPathGenerationWithContext() { HttpTransport transport = new HttpTransport(); transport.establishConnection("localhost", 90); RequestMessage requestMessage = new RequestMessage(); requestMessage.setMethod("PUT"); requestMessage.setPath("testPath"); requestMessage.setContext("context"); HttpMethod method = transport.createHttpMethod(requestMessage); assertTrue("Method type is incorret", method instanceof PutMethod); assertEquals("Path is incorrect", "/context/testPath", method.getPath()); }
From source file:org.jahia.test.services.logout.LogoutTest.java
protected String logout(String url) throws Exception { String returnUrl = null;/*w w w. java 2s . c o m*/ String baseurl = getBaseServerURL() + Jahia.getContextPath(); HttpMethod method = new GetMethod(baseurl + "/cms/logout"); try { method.setRequestHeader("Referer", baseurl + url); getHttpClient().executeMethod(method); returnUrl = StringUtils.isEmpty(Jahia.getContextPath()) || !(method.getPath().startsWith(Jahia.getContextPath())) ? method.getPath() : StringUtils.substringAfter(method.getPath(), Jahia.getContextPath()); } finally { method.releaseConnection(); } return returnUrl; }
From source file:org.osaf.caldav4j.exceptions.BadStatusException.java
public BadStatusException(HttpMethod method) { super(String.format(MESSAGE, method.getStatusCode(), method.getName(), method.getPath())); }
From source file:org.osaf.caldav4j.methods.HttpClient.java
/** * Overwritten to Handle tickets.//w w w . j a v a 2 s . com */ public int executeMethod(HostConfiguration hostConfiguration, HttpMethod method, HttpState state) throws IOException, HttpException { if (ticket != null) { if (ticketLocation == TicketLocation.HEADER) { method.addRequestHeader(CalDAVConstants.TICKET_HEADER, ticket); } //FIXME what if there are other query parameters! if (ticketLocation == TicketLocation.QUERY_PARAM) { method.setPath(method.getPath() + CalDAVConstants.URL_APPENDER + ticket); } } return super.executeMethod(hostConfiguration, method, state); }
From source file:org.pengyou.client.lib.DavResource.java
/** * Generate and add the If header to the specified HTTP method. *///from ww w. j av a 2s. c o m protected void generateIfHeader(HttpMethod method) { if (client == null) return; if (method == null) return; WebdavState state = (WebdavState) this.client.getState(); String[] lockTokens = state.getAllLocks(method.getPath()); if (lockTokens.length == 0) return; StringBuffer ifHeaderValue = new StringBuffer(); for (int i = 0; i < lockTokens.length; i++) { ifHeaderValue.append("(<").append(lockTokens[i]).append(">) "); } method.setRequestHeader("If", ifHeaderValue.toString()); }
From source file:org.sakaiproject.nakamura.grouper.changelog.util.NakamuraHttpUtils.java
/** * Prepare an HTTP request to Sakai OAE and parse the response (if JSON). * @param client an {@link HttpClient} to execute the request. * @param method an HTTP method to send/*from w ww . j a v a2s . c o m*/ * @return a JSONObject of the response if the request returns JSON * @throws GroupModificationException if there was an error updating the group information. */ public static JSONObject http(HttpClient client, HttpMethod method) throws GroupModificationException { method.setRequestHeader("User-Agent", HTTP_USER_AGENT); method.setRequestHeader("Referer", HTTP_REFERER); String errorMessage = null; String responseString = null; JSONObject responseJSON = null; boolean isJSONRequest = !method.getPath().toString().endsWith(".html"); if (log.isDebugEnabled() && method instanceof PostMethod) { log.debug(method.getName() + " " + method.getPath() + " params:"); for (NameValuePair nvp : ((PostMethod) method).getParameters()) { log.debug(nvp.getName() + " = " + nvp.getValue()); } } int responseCode = -1; try { responseCode = client.executeMethod(method); responseString = StringUtils.trimToNull(IOUtils.toString(method.getResponseBodyAsStream())); if (isJSONRequest) { responseJSON = parseJSONResponse(responseString); } if (log.isDebugEnabled()) { log.debug(responseCode + " " + method.getName() + " " + method.getPath()); } if (log.isTraceEnabled()) { log.trace("reponse: " + responseString); } switch (responseCode) { case HttpStatus.SC_OK: // 200 case HttpStatus.SC_CREATED: // 201 break; case HttpStatus.SC_BAD_REQUEST: // 400 case HttpStatus.SC_UNAUTHORIZED: // 401 case HttpStatus.SC_NOT_FOUND: // 404 case HttpStatus.SC_INTERNAL_SERVER_ERROR: // 500 if (isJSONRequest && responseJSON != null) { errorMessage = responseJSON.getString("status.message"); } if (errorMessage == null) { errorMessage = "Empty " + responseCode + " error. Check the logs on the Sakai OAE server."; } break; default: errorMessage = "Unknown HTTP response " + responseCode; break; } } catch (Exception e) { errorMessage = "An exception occurred communicatingSakai OAE. " + e.toString(); } finally { method.releaseConnection(); } if (errorMessage != null) { log.error(errorMessage); errorToException(responseCode, errorMessage); } return responseJSON; }
From source file:org.socraticgrid.displaycalendarlib.CalendarInit.java
public int executeCalMethod(int expectedStatus, HttpMethod method, boolean deleteOnTearDown) throws IOException { String relativePath = method.getPath(); // prefix path with collection path method.setPath(collectionPath + relativePath); int response = executeHttpMethod(expectedStatus, httpClient, method); if (deleteOnTearDown) { deleteOnTearDownPaths.add(relativePath); }/*from ww w. ja v a 2s .com*/ return response; }
From source file:org.sonatype.nexus.proxy.storage.remote.commonshttpclient.CommonsHttpClientRemoteStorage.java
@Override public AbstractStorageItem retrieveItem(ProxyRepository repository, ResourceStoreRequest request, String baseUrl) throws ItemNotFoundException, RemoteStorageException { URL remoteURL = getAbsoluteUrlFromBase(baseUrl, request.getRequestPath()); HttpMethod method; method = new GetMethod(remoteURL.toString()); int response = executeMethod(repository, request, method, remoteURL); if (response == HttpStatus.SC_OK) { if (method.getPath().endsWith("/")) { // this is a collection and not a file! // httpClient will follow redirections, and the getPath() // _should_ // give us URL with ending "/" method.releaseConnection();/*ww w . j a v a 2 s .co m*/ throw new RemoteItemNotFoundException(request, repository, "remoteIsCollection", remoteURL.toString()); } GetMethod get = (GetMethod) method; InputStream is; try { is = get.getResponseBodyAsStream(); if (get.getResponseHeader("Content-Encoding") != null && "gzip".equals(get.getResponseHeader("Content-Encoding").getValue())) { is = new GZIPInputStream(is); } String mimeType; if (method.getResponseHeader("content-type") != null) { mimeType = method.getResponseHeader("content-type").getValue(); } else { mimeType = getMimeSupport().guessMimeTypeFromPath(repository.getMimeRulesSource(), request.getRequestPath()); } DefaultStorageFileItem httpItem = new DefaultStorageFileItem(repository, request, true, true, new PreparedContentLocator(new HttpClientInputStream(get, is), mimeType)); if (get.getResponseContentLength() != -1) { // FILE httpItem.setLength(get.getResponseContentLength()); } httpItem.setRemoteUrl(remoteURL.toString()); httpItem.setModified(makeDateFromHeader(method.getResponseHeader("last-modified"))); httpItem.setCreated(httpItem.getModified()); httpItem.getItemContext().putAll(request.getRequestContext()); return httpItem; } catch (IOException ex) { method.releaseConnection(); throw new RemoteStorageException("IO Error during response stream handling [repositoryId=\"" + repository.getId() + "\", requestPath=\"" + request.getRequestPath() + "\", remoteUrl=\"" + remoteURL.toString() + "\"]!", ex); } catch (RuntimeException ex) { method.releaseConnection(); throw ex; } } else { method.releaseConnection(); if (response == HttpStatus.SC_NOT_FOUND) { throw new RemoteItemNotFoundException(request, repository, "NotFound", remoteURL.toString()); } else { throw new RemoteStorageException("The method execution returned result code " + response + " (expected 200). [repositoryId=\"" + repository.getId() + "\", requestPath=\"" + request.getRequestPath() + "\", remoteUrl=\"" + remoteURL.toString() + "\"]"); } } }
From source file:org.svenk.redmine.core.client.AbstractRedmineClient.java
synchronized protected int performExecuteMethod(HttpMethod method, HostConfiguration hostConfiguration, IProgressMonitor monitor) throws RedmineException { try {/*from w ww . j a va 2 s. co m*/ //complete URL String baseUrl = new URL(location.getUrl()).getPath(); if (!method.getPath().startsWith(baseUrl)) { method.setPath(baseUrl + method.getPath()); } return WebUtil.execute(httpClient, hostConfiguration, method, monitor); } catch (OperationCanceledException e) { monitor.setCanceled(true); throw new RedmineException(e.getMessage(), e); } catch (RuntimeException e) { IStatus status = RedmineCorePlugin.toStatus(e, null, Messages.AbstractRedmineClient_UNHANDLED_RUNTIME_EXCEPTION); StatusHandler.fail(status); throw new RedmineStatusException(status); } catch (IOException e) { IStatus status = RedmineCorePlugin.toStatus(e, null); StatusHandler.log(status); throw new RedmineStatusException(status); } }
From source file:org.tuleap.mylyn.task.core.internal.client.rest.TuleapRestConnector.java
/** * {@inheritDoc}/*from w w w. j a v a2s .co m*/ * * @see org.tuleap.mylyn.task.core.internal.client.rest.IRestConnector#sendRequest(org.apache.commons.httpclient.HttpMethod) */ @Override public ServerResponse sendRequest(HttpMethod method) { // debug mode boolean debug = false; IEclipsePreferences node = InstanceScope.INSTANCE.getNode(ITuleapConstants.TULEAP_PREFERENCE_NODE); if (node != null) { debug = node.getBoolean(ITuleapConstants.TULEAP_PREFERENCE_DEBUG_MODE, false); } if (hostConfiguration == null) { hostConfiguration = WebUtil.createHostConfiguration(httpClient, location, null); } method.setRequestHeader("Accept", "application/json"); //$NON-NLS-1$ //$NON-NLS-2$ method.setRequestHeader("Accept-Charset", "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ method.setRequestHeader("Content-Type", "application/json"); //$NON-NLS-1$ //$NON-NLS-2$ WebUtil.configureHttpClient(httpClient, getUserAgent()); Header[] responseHeaders = null; String responseBody = null; ServerResponse serverResponse = null; try { int code = WebUtil.execute(httpClient, hostConfiguration, method, null); responseBody = method.getResponseBodyAsString(); responseHeaders = method.getResponseHeaders(); if (debug) { debugRestCall(method, responseBody); } Map<String, String> rHeaders = new LinkedHashMap<String, String>(); for (Header h : responseHeaders) { rHeaders.put(h.getName(), h.getValue()); } serverResponse = new ServerResponse(code, responseBody, rHeaders); } catch (IOException e) { logger.log(new Status(IStatus.ERROR, TuleapCoreActivator.PLUGIN_ID, TuleapCoreMessages .getString(TuleapCoreKeys.ioError, method.getName() + ' ' + method.getPath(), e.getMessage()))); serverResponse = new ServerResponse(IO_ERROR_STATUS_CODE, "", Collections //$NON-NLS-1$ .<String, String>emptyMap()); } finally { method.releaseConnection(); } return serverResponse; }