List of usage examples for org.apache.commons.httpclient HttpStatus SC_MOVED_TEMPORARILY
int SC_MOVED_TEMPORARILY
To view the source code for org.apache.commons.httpclient HttpStatus SC_MOVED_TEMPORARILY.
Click Source Link
From source file:org.apache.sling.maven.bundlesupport.AbstractBundleInstallMojo.java
/** * Add a new configuration for the file system provider * @throws MojoExecutionException//from ww w . j av a 2s .c o m */ protected void addConfiguration(final HttpClient client, final String targetURL, String dir, String path) throws MojoExecutionException { final String postUrl = targetURL + "/configMgr/" + FS_FACTORY; final PostMethod post = new PostMethod(postUrl); post.addParameter("apply", "true"); post.addParameter("factoryPid", FS_FACTORY); post.addParameter("pid", "[Temporary PID replaced by real PID upon save]"); post.addParameter("provider.file", dir); post.addParameter("provider.roots", path); post.addParameter("propertylist", "provider.roots,provider.file"); try { final int status = client.executeMethod(post); // we get a moved temporarily back from the configMgr plugin if (status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_OK) { getLog().info("Configuration created."); } else { getLog().error("Configuration failed, cause: " + HttpStatus.getStatusText(status)); } } catch (HttpException ex) { throw new MojoExecutionException("Configuration on " + postUrl + " failed, cause: " + ex.getMessage(), ex); } catch (IOException ex) { throw new MojoExecutionException("Configuration on " + postUrl + " failed, cause: " + ex.getMessage(), ex); } finally { post.releaseConnection(); } }
From source file:org.apache.sling.sitebuilder.internal.ResourceTypeEditorServlet.java
@Override protected void doRun(SlingHttpServletRequest request, PostResponse postResponse, List<Modification> modifications) throws RepositoryException { ResourceResolver resourceResolver = request.getResourceResolver(); String resourceType = request.getParameter("resourceType"); Resource resourceTypeResource; try {/* www . j av a 2 s. c o m*/ String targetResourceTypePath = "/apps/" + resourceType; resourceTypeResource = getOrCreateResource(resourceResolver, targetResourceTypePath); String selectedScriptFilePath = request.getParameter("selectedScriptFilePath"); selectedScriptFilePath = "/apps/" + selectedScriptFilePath; String scriptName = selectedScriptFilePath.substring(selectedScriptFilePath.lastIndexOf("/") + 1); Resource targetScriptResource = resourceResolver.resolve(targetResourceTypePath + "/" + scriptName); if (!(targetScriptResource instanceof NonExistingResource)) { resourceResolver.delete(targetScriptResource); } ResourceUtils.copy(resourceResolver, selectedScriptFilePath, targetResourceTypePath, scriptName); // resourceResolver.copy(selectedScriptFilePath, targetResourceTypePath); String resourceSuperType = request.getParameter("resourceSuperType"); // resourceTypeResource.getValueMap().put("sling:resourceSuperType", resourceSuperType); resourceTypeResource.adaptTo(ModifiableValueMap.class).put("sling:resourceSuperType", resourceSuperType); request.getResource().adaptTo(ModifiableValueMap.class).put("sling:resourceType", resourceType); resourceResolver.commit(); postResponse.setLocation(request.getRequestURL().toString()); postResponse.setStatus(HttpStatus.SC_MOVED_TEMPORARILY, "redirect to target"); } catch (PersistenceException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.atricore.idbus.capabilities.josso.test.JOSSO11WebSelfservicesTest.java
private void initLostPasswordProcess(HttpClient client) throws Exception { log.debug("******************************************************************************"); log.debug("initLostPasswordProcess"); log.debug("******************************************************************************"); GetMethod get = new GetMethod(lostPasswordEndpoint + "?josso_cmd=lostPwd"); get.setFollowRedirects(false);//from w ww.j a v a2 s . co m int status = client.executeMethod(get); get.getResponseBodyAsString(); assert status == HttpStatus.SC_MOVED_TEMPORARILY : "status code spected " + HttpStatus.SC_MOVED_TEMPORARILY + " found [" + status + "]"; Header h = get.getResponseHeader("Location"); assert h != null : "No Location found"; URL url = new URL(h.getValue()); assert url.getQuery() != null && url.getQuery().contains("artifactId") : "No artifactId found"; }
From source file:org.eclipse.mylyn.internal.bugzilla.core.BugzillaClient.java
/** * calling method must release the connection on the returned PostMethod once finished. * * @throws CoreException//from w w w . j a v a 2 s . c o m */ private GzipPostMethod postFormData(String formUrl, NameValuePair[] formData, IProgressMonitor monitor) throws IOException, CoreException { GzipPostMethod postMethod = null; monitor = Policy.monitorFor(monitor); hostConfiguration = WebUtil.createHostConfiguration(httpClient, location, monitor); authenticate(monitor); postMethod = new GzipPostMethod(WebUtil.getRequestPath(repositoryUrl.toString() + formUrl), true); postMethod.setRequestHeader("Content-Type", //$NON-NLS-1$ "application/x-www-form-urlencoded; charset=" + getCharacterEncoding()); //$NON-NLS-1$ httpClient.getHttpConnectionManager().getParams().setSoTimeout(WebUtil.getConnectionTimeout()); postMethod.setRequestBody(formData); postMethod.setDoAuthentication(true); int status = WebUtil.execute(httpClient, hostConfiguration, postMethod, monitor); if (status == HttpStatus.SC_OK) { return postMethod; } else if (status == HttpStatus.SC_MOVED_TEMPORARILY) { String redirectLocation; Header locationHeader = postMethod.getResponseHeader("location"); //$NON-NLS-1$ if (locationHeader != null) { redirectLocation = locationHeader.getValue(); WebUtil.releaseConnection(postMethod, monitor); throw new RedirectException(redirectLocation); } } WebUtil.releaseConnection(postMethod, monitor); throw new CoreException(new BugzillaStatus(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, RepositoryStatus.ERROR_IO, repositoryUrl.toString(), new IOException( "Communication error occurred during upload. \n\n" + HttpStatus.getStatusText(status)))); //$NON-NLS-1$ }
From source file:org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.java
private int authenticateDevelopmentMode(AuthenticationCredentials credentials, IProgressMonitor monitor) throws IOException, GerritException { // try to detect if user name is user id, email or account id String key;//from w w w . j ava 2 s.c o m if (credentials.getUserName().contains("@")) { //$NON-NLS-1$ key = "preferred_email"; //$NON-NLS-1$ } else { try { Long.parseLong(credentials.getUserName()); key = "account_id"; //$NON-NLS-1$ } catch (NumberFormatException e) { key = "user_name"; //$NON-NLS-1$ } } String repositoryUrl = getUrl(); String gerrit_2_5_RequestPath = WebUtil.getRequestPath(repositoryUrl + BECOME_URL + "?" + key + "=" //$NON-NLS-1$ //$NON-NLS-2$ + credentials.getUserName()); String gerrit_2_6_RequestPath = WebUtil.getRequestPath(repositoryUrl + LOGIN_URL + "?" + key + "=" //$NON-NLS-1$ //$NON-NLS-2$ + credentials.getUserName()); for (String requestPath : new String[] { gerrit_2_5_RequestPath, gerrit_2_6_RequestPath }) { GetMethod method = new GetMethod(requestPath); method.setFollowRedirects(false); try { int code = WebUtil.execute(httpClient, hostConfiguration, method, monitor); if (needsReauthentication(code, monitor)) { return -1; } if (code == HttpStatus.SC_OK) { // authentication failed return code; } if (code == HttpStatus.SC_NOT_FOUND) { continue; } if (code != HttpStatus.SC_MOVED_TEMPORARILY && code != HttpStatus.SC_MOVED_TEMPORARILY) { throw new GerritHttpException(code); } return code; } finally { WebUtil.releaseConnection(method, monitor); } } return HttpStatus.SC_NOT_FOUND; }
From source file:org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.java
private int authenticateForm(AuthenticationCredentials credentials, IProgressMonitor monitor) throws IOException, GerritException { // try standard basic/digest/ntlm authentication first String repositoryUrl = getUrl(); AuthScope authScope = new AuthScope(WebUtil.getHost(repositoryUrl), WebUtil.getPort(repositoryUrl), null, AuthScope.ANY_SCHEME);// w w w. j a va 2 s. co m Credentials httpCredentials = WebUtil.getHttpClientCredentials(credentials, WebUtil.getHost(repositoryUrl)); httpClient.getState().setCredentials(authScope, httpCredentials); HttpMethodBase[] methods = getFormAuthMethods(repositoryUrl, credentials); for (HttpMethodBase method : methods) { int code; try { code = WebUtil.execute(httpClient, hostConfiguration, method, monitor); if (code == HttpStatus.SC_METHOD_NOT_ALLOWED) { continue; // try next http method } else if (needsReauthentication(code, monitor)) { return -1; } else if (code == HttpStatus.SC_MOVED_TEMPORARILY) { Header locationHeader = method.getResponseHeader("Location"); //$NON-NLS-1$ if (locationHeader != null) { if (locationHeader.getValue() .endsWith("SignInFailure,SIGN_IN,Session cookie not available.")) { //$NON-NLS-1$ // try different authentication method return HttpStatus.SC_NOT_FOUND; } } } else if (code == HttpStatus.SC_OK) { // try different authentication method as the server maybe using development mode authentication return HttpStatus.SC_NOT_FOUND; } else if (code != HttpStatus.SC_NOT_FOUND) { throw new GerritHttpException(code); } return code; } finally { WebUtil.releaseConnection(method, monitor); } } return HttpStatus.SC_NOT_FOUND; }
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$ }//w w w .j a va 2 s.c om 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$ }
From source file:org.eclipse.mylyn.internal.jira.core.service.web.rss.JiraRssSessionCallback.java
@Override public final void run(JiraClient client, String baseUrl, IProgressMonitor monitor) throws JiraException, IOException { String rssUrl = getRssUrl(baseUrl); //for (int i = 0; i <= MAX_REDIRECTS; i++) { GetMethod rssRequest = new GetMethod(rssUrl); rssRequest.setFollowRedirects(false); if (useCompression) { // request compressed response, this does not guarantee it will be done rssRequest.setRequestHeader("Accept-Encoding", "gzip"); //$NON-NLS-1$ //$NON-NLS-2$ }//from w ww. j a v a 2 s . c o m try { int code = execute(rssRequest); // TODO refactor, code was copied from JiraWebSession.expectRedirect() if (code == HttpStatus.SC_MOVED_TEMPORARILY) { // check if redirect was to issue page, this means only a single result was received Header locationHeader = rssRequest.getResponseHeader("location"); //$NON-NLS-1$ if (locationHeader == null) { throw new JiraRedirectException(); } String url = locationHeader.getValue(); if (!url.startsWith(baseUrl + "/browse/")) { //$NON-NLS-1$ throw new JiraRedirectException(url); } rssRequest.releaseConnection(); // request XML for single result rssUrl = url + "?decorator=none&view=rss"; //$NON-NLS-1$ } else if (code != HttpStatus.SC_OK) { StringBuilder sb = new StringBuilder("Unexpected result code "); //$NON-NLS-1$ sb.append(code); sb.append(" while running query: "); //$NON-NLS-1$ sb.append(rssUrl); throw new JiraRemoteMessageException(sb.toString(), rssRequest.getResponseBodyAsString()); } // if it still isn't an XML response, an invalid issue was entered if (!isXMLOrRSS(rssRequest)) { throw new JiraInvalidResponseTypeException( Messages.JiraRssSessionCallback_Repository_returned_invalid_type); } parseResult(client, baseUrl, rssRequest, monitor); // success return; } finally { rssRequest.releaseConnection(); } //} //throw new JiraException("Maximum number of query redirects exceeded: " + rssUrl); //$NON-NLS-1$ }
From source file:org.executequery.http.spi.DefaultRemoteHttpClient.java
private boolean isRedirection(int responseCode) { return responseCode == HttpStatus.SC_MOVED_PERMANENTLY || responseCode == HttpStatus.SC_MOVED_TEMPORARILY || responseCode == HttpStatus.SC_SEE_OTHER || responseCode == HttpStatus.SC_TEMPORARY_REDIRECT; }
From source file:org.gcaldaemon.core.GmailEntry.java
private final void connectLDAP(String username, String password) throws Exception { // Google Apps For Your Domain support String domain = null;/*w w w.j a va 2 s.c o m*/ int i = username.indexOf('@'); String loginUsername = username; String loginURL = "https://www.google.com/accounts/ServiceLoginAuth"; if (i != -1) { if (username.indexOf("gmail.com") == -1) { domain = username.substring(i + 1); loginUsername = username.substring(0, i); // Use Google Apps URLs instead of 'gmail.com' URLs gmailURL = "http://mail.google.com/a/" + domain + '/'; loginURL = "https://www.google.com/a/" + domain + "/ServiceLogin"; logoutURL = "http://mail.google.com/a/" + domain + "/?logout"; refererURL = "https://www.google.com/a/" + domain + "/ServiceLogin?service=mail&continue=https%3A%2F%2Fwww.google.com%3A443%2Fa%2F" + domain + "%2FDashboard&passive=true"; oldContactURL = "http://mail.google.com/a/" + domain + "/?ui=1&view=fec"; newContactURL = "http://mail.google.com/a/" + domain + "/mail/contacts/data/export?" + "exportType=ALL&out=GMAIL_CSV"; } } // Default 'gmail.com' login fields PostMethod post = new PostMethod(loginURL); String usernameField = "Email"; String passwordField = "Passwd"; if (domain != null) { // Google Apps For Your Domain login fields usernameField = "userName"; passwordField = "password"; } // Create login request NameValuePair[] data = { new NameValuePair("service", "mail"), new NameValuePair(usernameField, loginUsername), new NameValuePair(passwordField, password), new NameValuePair("null", "Sign in"), new NameValuePair("continue", gmailURL) }; post.addRequestHeader("User-Agent", USER_AGENT); post.addRequestHeader("referer", refererURL); post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded"); post.setRequestBody(data); // Send login form to Gmail int httpStatus = httpClient.executeMethod(post); if (httpStatus == -1) { throw new Exception("Login failed!\r\n" + "Invalid HTTP status code (check username/password)!"); } String response = post.getResponseBodyAsString(); post.releaseConnection(); if (response.toLowerCase().indexOf("errormsg") != -1) { log.warn("Response from Google:\r\n" + response); throw new Exception("Login failed (check username/password)!"); } // Check redirection #1 GetMethod get = null; String newLocationURL; i = response.toLowerCase().indexOf("moved temporarily"); if (i != -1) { i = response.toLowerCase().indexOf("href"); if (i != -1) { int n = response.indexOf('"', i + 7); // Get new URL #1 newLocationURL = response.substring(i + 6, n); get = new GetMethod(newLocationURL); get.addRequestHeader("User-Agent", USER_AGENT); try { httpStatus = httpClient.executeMethod(get); // Get 'GMAIL_AT' cookie's value gmailAt = getGmailAt(get.getRequestHeaders()); response = get.getResponseBodyAsString(); get.releaseConnection(); } catch (IOException e) { log.error("Failed to redirect URL!", e); } } } // Finish login (cookie handshake) get = new GetMethod(gmailURL); get.addRequestHeader("User-Agent", USER_AGENT); get.addRequestHeader("referer", refererURL); get.addRequestHeader("Content-Type", "text/html"); try { httpStatus = httpClient.executeMethod(get); // Get 'GMAIL_AT' cookie's value gmailAt = getGmailAt(get.getRequestHeaders()); // Check redirection #2 if (httpStatus == HttpStatus.SC_MOVED_PERMANENTLY || httpStatus == HttpStatus.SC_MOVED_TEMPORARILY || httpStatus == HttpStatus.SC_SEE_OTHER || httpStatus == HttpStatus.SC_TEMPORARY_REDIRECT) { // Get new URL #2 Header newLocationHeader = get.getResponseHeader("location"); if (newLocationHeader != null) { newLocationURL = newLocationHeader.getValue(); get.releaseConnection(); get = new GetMethod(newLocationURL); try { httpStatus = httpClient.executeMethod(get); // Check redirection #3 if (httpStatus == HttpStatus.SC_MOVED_PERMANENTLY || httpStatus == HttpStatus.SC_MOVED_TEMPORARILY || httpStatus == HttpStatus.SC_SEE_OTHER || httpStatus == HttpStatus.SC_TEMPORARY_REDIRECT) { // Get new URL #3 newLocationHeader = get.getResponseHeader("location"); if (newLocationHeader != null) { newLocationURL = newLocationHeader.getValue(); get.releaseConnection(); get = new GetMethod(newLocationURL); try { httpStatus = httpClient.executeMethod(get); // Get 'GMAIL_AT' cookie's value gmailAt = getGmailAt(get.getRequestHeaders()); get.releaseConnection(); } catch (IOException e) { log.warn("Failed to redirect URL!", e); } } else { log.warn("Missing location URL!"); } } } catch (IOException e) { log.warn("Failed to redirect URL!", e); } } else { log.warn("Missing location header!"); } } } catch (IOException e) { log.error("Failed to open URL", e); } }