List of usage examples for org.apache.commons.httpclient.methods PostMethod setFollowRedirects
public void setFollowRedirects(boolean paramBoolean)
From source file:org.apache.sling.launchpad.webapp.integrationtest.servlets.post.PostServletNopTest.java
private void post(String url, String code, int expectedStatus) throws IOException { final PostMethod post = new PostMethod(url); post.setFollowRedirects(false); post.addParameter(":operation", "nop"); if (code != null) { post.addParameter(":nopstatus", code); }//w w w .j a va 2 s . c om int actualStatus = httpClient.executeMethod(post); assertEquals(expectedStatus, actualStatus); }
From source file:org.apache.sling.launchpad.webapp.integrationtest.servlets.post.PostServletOutputContentTypeTest.java
private void runTest(String acceptHeaderValue, boolean useHttpEquiv, String expectedContentType) throws Exception { final String info = (useHttpEquiv ? "Using http-equiv parameter" : "Using Accept header") + ": "; final String url = HTTP_BASE_URL + MY_TEST_PATH; final PostMethod post = new PostMethod(url); post.setFollowRedirects(false); if (acceptHeaderValue != null) { if (useHttpEquiv) { post.addParameter(":http-equiv-accept", acceptHeaderValue); } else {/*from w ww . ja v a 2s .c o m*/ post.addRequestHeader("Accept", acceptHeaderValue); } } final int status = httpClient.executeMethod(post) / 100; assertEquals(info + "Expected status 20x for POST at " + url, 2, status); final Header h = post.getResponseHeader("Content-Type"); assertNotNull(info + "Expected Content-Type header", h); final String ct = h.getValue(); assertTrue(info + "Expected Content-Type '" + expectedContentType + "' for Accept header=" + acceptHeaderValue + " but got '" + ct + "'", ct.startsWith(expectedContentType)); }
From source file:org.apache.sling.launchpad.webapp.integrationtest.servlets.post.PostServletOutputContentTypeTest.java
public void testJsonContentTypeException() throws Exception { // Perform a POST that fails: invalid PostServlet operation // with Accept header set to JSON final String url = HTTP_BASE_URL + MY_TEST_PATH; final PostMethod post = new PostMethod(url); post.setFollowRedirects(false); post.addParameter(new NameValuePair(SlingPostConstants.RP_OPERATION, "InvalidTestOperationFor" + getClass().getSimpleName())); post.addRequestHeader("Accept", CONTENT_TYPE_JSON); final int status = httpClient.executeMethod(post); assertEquals(500, status);//from w w w . ja v a 2s . c o m final String contentType = post.getResponseHeader("Content-Type").getValue(); final String expected = CONTENT_TYPE_JSON; assertTrue("Expecting content-type " + expected + " for failed POST request, got " + contentType, contentType != null && contentType.startsWith(expected)); }
From source file:org.apache.sling.launchpad.webapp.integrationtest.servlets.post.PostStatusTest.java
private void simplePost(String url, String statusParam, int expectStatus, String expectLocation) throws IOException { final PostMethod post = new PostMethod(url); post.setFollowRedirects(false); if (statusParam != null) { post.addParameter(SlingPostConstants.RP_STATUS, statusParam); }//w ww.j a v a 2s . com final int status = httpClient.executeMethod(post); assertEquals("Unexpected status response", expectStatus, status); if (expectLocation != null) { String location = post.getResponseHeader("Location").getValue(); assertNotNull("Expected location header", location); assertTrue(location.endsWith(expectLocation)); } post.releaseConnection(); }
From source file:org.apache.sling.launchpad.webapp.integrationtest.servlets.post.SlingPostProcessorTest.java
@Test public void processorsActive() throws HttpException, IOException { final PostMethod post = new PostMethod(testUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX); post.setFollowRedirects(false); post.setParameter("DummyModification", "true"); try {/* w w w . ja v a2 s. c o m*/ T.getHttpClient().executeMethod(post); final String content = post.getResponseBodyAsString(); final int i1 = content.indexOf("source:SlingPostProcessorOne"); assertTrue("Expecting first processor to be present", i1 > 0); final int i2 = content.indexOf("source:SlingPostProcessorTwo"); assertTrue("Expecting second processor to be present", i2 > 0); assertTrue("Expecting service ranking to put processor one first", i1 < i2); } finally { post.releaseConnection(); } }
From source file:org.bibsonomy.rest.client.worker.impl.PostWorker.java
@Override protected PostMethod getMethod(String url, String requestBody) { final PostMethod post = new PostMethod(url); post.setFollowRedirects(false); post.setRequestEntity(new StringRequestEntity(requestBody)); return post;/* w w w .j a v a2 s .c o m*/ }
From source file:org.conqat.engine.bugzilla.lib.BugzillaWebClient.java
/** * Authenticate with Bugzilla server. Note that Bugzilla servers can often * be accessed without credentials in a read-only manner. Hence, credentials * are not necessarily required although your Bugzilla server requires them * to edit bugs. Depending on the permissions set on the Bugzilla server, * calls to {@link #query(Set, Set, Set)} may return different results * whether you are authenticated or not. * //from w ww . j ava 2s . c o m * @throws BugzillaException * If Bugzilla server responded with an error. * @throws IOException * if an I/O problem occurs while obtaining the response from * Bugzilla * @throws HttpException * if a protocol exception occurs */ public void authenticate(String username, String password) throws BugzillaException, HttpException, IOException { NameValuePair[] formData = new NameValuePair[2]; formData[0] = new NameValuePair("Bugzilla_login", username); formData[1] = new NameValuePair("Bugzilla_password", password); PostMethod postMethod = new PostMethod(server + "/index.cgi"); postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); postMethod.setRequestBody(formData); postMethod.setDoAuthentication(true); postMethod.setFollowRedirects(false); client.getState().clearCookies(); try { int code = client.executeMethod(postMethod); if (code != HttpStatus.SC_OK) { throw new BugzillaException("Authentication failed: " + HttpStatus.getStatusText(code)); } // Bugzilla assigns cookies if everything went ok. Cookie[] cookies = client.getState().getCookies(); if (cookies.length == 0) { throw new BugzillaException("Authentication failed!"); } // the following loop fixes CR#2801. For some reason, Bugzilla only // accepts the cookies if they are not limited to secure // connections. for (Cookie c : cookies) { c.setSecure(false); } } finally { postMethod.releaseConnection(); } }
From source file:org.conqat.engine.bugzilla.lib.BugzillaWebClient.java
/** * This executes an HTTP post on a Bugzilla URL and deals with the handling * of error messages.//w w w .ja va 2 s . c o m * * @return the body of the HTTP response. * @throws BugzillaException * If Bugzilla server responded with an error. * @throws IOException * if an I/O problem occurs while obtaining the response from * Bugzilla * @throws HttpException * if a protocol exception occurs */ private String executePostMethod(String url, BugzillaFields fields) throws HttpException, IOException, BugzillaException { PostMethod postMethod = new PostMethod(server + "/" + url); postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); postMethod.setRequestBody(fields.createFormData()); postMethod.setFollowRedirects(false); try { int result = client.executeMethod(postMethod); if (result != HttpStatus.SC_OK) { throw new BugzillaException("Error occured when adding comment."); } String response = postMethod.getResponseBodyAsString(); if (StringUtils.containsOneOf(response, ERROR_MARKERS)) { throw new BugzillaException(extractErrorMessage(response)); } return response; } finally { postMethod.releaseConnection(); } }
From source file:org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.java
private HttpMethodBase[] getFormAuthMethods(String repositoryUrl, AuthenticationCredentials credentials) { PostMethod post = new PostMethod(WebUtil.getRequestPath(repositoryUrl + LOGIN_URL)); post.setParameter("username", credentials.getUserName()); //$NON-NLS-1$ post.setParameter("password", credentials.getPassword()); //$NON-NLS-1$ post.setFollowRedirects(false); GetMethod get = new GetMethod(WebUtil.getRequestPath(repositoryUrl + LOGIN_URL)); get.setFollowRedirects(false);/*w w w .ja v a 2 s .c o m*/ return new HttpMethodBase[] { post, get }; }
From source file:org.eclipse.mylyn.internal.jira.core.service.web.JiraWebSession.java
private HostConfiguration login(HttpClient httpClient, IProgressMonitor monitor) throws JiraException { RedirectTracker tracker = new RedirectTracker(); String url = baseUrl + "/login.jsp"; //$NON-NLS-1$ for (int i = 0; i <= MAX_REDIRECTS; i++) { AuthenticationCredentials credentials = location.getCredentials(AuthenticationType.REPOSITORY); if (credentials == null) { // TODO prompt user? credentials = new AuthenticationCredentials("", ""); //$NON-NLS-1$ //$NON-NLS-2$ }//from w w w . j ava 2 s . com PostMethod login = new PostMethod(url); login.setFollowRedirects(false); login.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); login.setRequestHeader("Content-Type", getContentType()); //$NON-NLS-1$ login.addParameter("os_username", credentials.getUserName()); //$NON-NLS-1$ login.addParameter("os_password", credentials.getPassword()); //$NON-NLS-1$ login.addParameter("os_destination", "/success"); //$NON-NLS-1$ //$NON-NLS-2$ tracker.addUrl(url); try { HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(httpClient, location, monitor); int statusCode = WebUtil.execute(httpClient, hostConfiguration, login, monitor); if (needsReauthentication(httpClient, login, monitor)) { continue; } else if (statusCode != HttpStatus.SC_MOVED_TEMPORARILY && statusCode != HttpStatus.SC_MOVED_PERMANENTLY) { throw new JiraServiceUnavailableException("Unexpected status code during login: " + statusCode); //$NON-NLS-1$ } tracker.addRedirect(url, login, statusCode); this.characterEncoding = login.getResponseCharSet(); Header locationHeader = login.getResponseHeader("location"); //$NON-NLS-1$ if (locationHeader == null) { throw new JiraServiceUnavailableException("Invalid redirect, missing location"); //$NON-NLS-1$ } url = locationHeader.getValue(); tracker.checkForCircle(url); if (!insecureRedirect && isSecure() && url.startsWith("http://")) { //$NON-NLS-1$ tracker.log("Redirect to insecure location during login to repository: " + client.getBaseUrl()); //$NON-NLS-1$ insecureRedirect = true; } if (url.endsWith("/success")) { //$NON-NLS-1$ String newBaseUrl = url.substring(0, url.lastIndexOf("/success")); //$NON-NLS-1$ if (baseUrl.equals(newBaseUrl) || !client.getConfiguration().getFollowRedirects()) { // success addAuthenticationCookie(httpClient, login); return hostConfiguration; } else { // need to login to make sure HttpClient picks up the session cookie baseUrl = newBaseUrl; url = newBaseUrl + "/login.jsp"; //$NON-NLS-1$ } } } catch (IOException e) { throw new JiraServiceUnavailableException(e); } finally { login.releaseConnection(); } } tracker.log( "Exceeded maximum number of allowed redirects during login to repository: " + client.getBaseUrl()); //$NON-NLS-1$ throw new JiraServiceUnavailableException("Exceeded maximum number of allowed redirects during login"); //$NON-NLS-1$ }