Example usage for javax.servlet.http HttpServletResponse SC_SEE_OTHER

List of usage examples for javax.servlet.http HttpServletResponse SC_SEE_OTHER

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse SC_SEE_OTHER.

Prototype

int SC_SEE_OTHER

To view the source code for javax.servlet.http HttpServletResponse SC_SEE_OTHER.

Click Source Link

Document

Status code (303) indicating that the response to the request can be found under a different URI.

Usage

From source file:de.mpg.imeji.presentation.util.LoginHelper.java

/**
 * Get handle of System administrator of eSciDoc instance.
 * //from   w  w  w  . j ava 2  s .  c o  m
 * @return
 */
//    public static String loginSystemAdmin()
//    {
//        String handle = null;
//        try
//        {
//            handle = login(PropertyReader.getProperty("framework.admin.username"),
//                    PropertyReader.getProperty("framework.admin.password"));
//        }
//        catch (Exception e)
//        {
//            sessionBean = (SessionBean)BeanHelper.getSessionBean(SessionBean.class);
//            BeanHelper
//                    .info(sessionBean.getLabel("error") + ", wrong administrator user. Check config file or FW: " + e);
//            logger.error("Error escidoc admin login", e);
//        }
//        return handle;
//    }

public static String login(String userName, String password) throws Exception {
    String frameworkUrl = PropertyReader.getProperty("escidoc.framework_access.framework.url");
    StringTokenizer tokens = new StringTokenizer(frameworkUrl, "//");
    tokens.nextToken();
    StringTokenizer hostPort = new StringTokenizer(tokens.nextToken(), ":");
    String host = hostPort.nextToken();
    int port = 80;
    if (hostPort.hasMoreTokens()) {
        port = Integer.parseInt(hostPort.nextToken());
    }
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().closeIdleConnections(1000);
    client.getHostConfiguration().setHost(host, port, "http");
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    PostMethod login = new PostMethod(frameworkUrl + "/aa/j_spring_security_check");
    login.addParameter("j_username", userName);
    login.addParameter("j_password", password);
    try {
        client.executeMethod(login);
    } catch (Exception e) {
        throw new RuntimeException("Error login in " + frameworkUrl + "  status: " + login.getStatusCode()
                + " - " + login.getStatusText());
    }
    login.releaseConnection();
    CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
    Cookie[] logoncookies = cookiespec.match(host, port, "/", false, client.getState().getCookies());
    Cookie sessionCookie = logoncookies[0];
    PostMethod postMethod = new PostMethod("/aa/login");
    postMethod.addParameter("target", frameworkUrl);
    client.getState().addCookie(sessionCookie);
    client.executeMethod(postMethod);
    if (HttpServletResponse.SC_SEE_OTHER != postMethod.getStatusCode()) {
        throw new HttpException("Wrong status code: " + postMethod.getStatusCode());
    }
    String userHandle = null;
    Header headers[] = postMethod.getResponseHeaders();
    for (int i = 0; i < headers.length; ++i) {
        if ("Location".equals(headers[i].getName())) {
            String location = headers[i].getValue();
            int index = location.indexOf('=');
            userHandle = new String(Base64.decode(location.substring(index + 1, location.length())));
        }
    }
    if (userHandle == null) {
        throw new ServiceException("User not logged in.");
    }
    return userHandle;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.DeletePropertyController.java

@Override
protected ResponseValues processRequest(VitroRequest vreq) {
    //if error conditions then return here
    String errorMessage = handleErrors(vreq);
    if (errorMessage != null) {
        return doErrorMessage(errorMessage);
    }//  ww  w.  j  a v  a2 s.c o m

    //handle based on whether object property or data property
    if (EditConfigurationUtils.isObjectProperty(EditConfigurationUtils.getPredicateUri(vreq), vreq)) {
        processObjectProperty(vreq);
    } else {
        processDataProperty(vreq);
    }

    String redirectUrl = getRedirectUrl(vreq);
    return new RedirectResponseValues(redirectUrl, HttpServletResponse.SC_SEE_OTHER);
}

From source file:com.pureinfo.tgirls.servlet.GetScriptServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String scriptType = request.getParameter("scriptType");
    if (StringUtils.isEmpty(scriptType)) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;// www .  j  ava 2s . co m
    }

    String location = null;
    String userId = null;
    String photoId = null;
    ScriptType st = null;//

    try {
        st = ScriptType.getScriptType(Integer.parseInt(scriptType));
    } catch (Exception e) {
    }

    switch (st) {
    case UserInfo:
        userId = request.getParameter("utid");
        location = ScriptManager.mapUserInfoScriptLocation(userId);
        break;
    case UserUpload:
        userId = request.getParameter("utid");
        location = ScriptManager.mapUserUploadScriptLocation(userId);
        break;
    case UserBuy:
        userId = request.getParameter("utid");
        location = ScriptManager.mapUserBuyScriptLocation(userId);
        break;
    case Pic:
        photoId = request.getParameter("picId");
        location = ScriptManager.mapPhotoScriptLocation(photoId);
        break;

    default:
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    if (StringUtils.isEmpty(location)) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    location += "?version=" + Math.random();
    logger.debug("return script:" + location);

    response.setHeader("Location", location);
    response.setHeader("Content-Encoding", "gzip");
    response.setStatus(HttpServletResponse.SC_SEE_OTHER);
    response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
    response.setHeader("Pragma", "no-cache"); //HTTP 1.0
    response.setDateHeader("Expires", -1);
    response.setDateHeader("max-age", 0);

    return;

}

From source file:edu.cornell.mannlib.vitro.webapp.controller.individual.IndividualController.java

@Override
protected ResponseValues processRequest(VitroRequest vreq) {
    try {//from w  w w.  j  ava 2s .com
        /*
         * What type of request is this?
         */
        IndividualRequestInfo requestInfo = analyzeTheRequest(vreq);

        switch (requestInfo.getType()) {
        case RDF_REDIRECT:
            /*
             * If someone expects RDF by asking for the individual with an
             * "accept" HTTP header, redirect them to the preferred URL.
             */
            return new RedirectResponseValues(requestInfo.getRedirectUrl(), HttpServletResponse.SC_SEE_OTHER);
        case NO_INDIVIDUAL:
            /*
             * If we can't figure out what individual you want, or if there
             * is no such individual, show an informative error page.
             */
            return doNotFound();
        case BYTESTREAM_REDIRECT:
            /*
             * If the Individual requested is a FileBytestream, redirect
             * them to the direct download URL, so they will get the correct
             * filename, etc.
             */
            return new RedirectResponseValues(requestInfo.getRedirectUrl(), HttpServletResponse.SC_SEE_OTHER);
        case LINKED_DATA:
            /*
             * If they are asking for RDF using the preferred URL, give it
             * to them.
             */
            if (useExtendedLOD(vreq)) {
                return new ExtendedRdfAssembler(vreq, requestInfo.getIndividual(), requestInfo.getRdfFormat())
                        .assembleRdf();
            } else {
                return new IndividualRdfAssembler(vreq, requestInfo.getIndividual().getURI(),
                        requestInfo.getRdfFormat()).assembleRdf();
            }
        default:
            /*
             * Otherwise, prepare an HTML response for the requested
             * individual.
             */
            return new IndividualResponseBuilder(vreq, requestInfo.getIndividual()).assembleResponse();
        }
    } catch (Throwable e) {
        log.error(e, e);
        return new ExceptionResponseValues(e);
    }
}

From source file:de.mpg.mpdl.inge.util.AdminHelper.java

/**
 * Logs in the given user with the given password.
 * //from   w w  w .j  a v  a2s  .c om
 * @param userid The id of the user to log in.
 * @param password The password of the user to log in.
 * @return The handle for the logged in user.
 * @throws HttpException
 * @throws IOException
 * @throws ServiceException
 * @throws URISyntaxException
 */
public static String loginUser(String userid, String password)
        throws HttpException, IOException, ServiceException, URISyntaxException {
    String frameworkUrl = PropertyReader.getLoginUrl();

    int delim1 = frameworkUrl.indexOf("//");
    int delim2 = frameworkUrl.indexOf(":", delim1);

    String host;
    int port;

    if (delim2 > 0) {
        host = frameworkUrl.substring(delim1 + 2, delim2);
        port = Integer.parseInt(frameworkUrl.substring(delim2 + 1));
    } else {
        host = frameworkUrl.substring(delim1 + 2);
        port = 80;
    }
    HttpClient client = new HttpClient();

    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

    PostMethod login = new PostMethod(frameworkUrl + "/aa/j_spring_security_check");
    login.addParameter("j_username", userid);
    login.addParameter("j_password", password);

    ProxyHelper.executeMethod(client, login);

    login.releaseConnection();
    CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
    Cookie[] logoncookies = cookiespec.match(host, port, "/", false, client.getState().getCookies());

    Cookie sessionCookie = logoncookies[0];

    PostMethod postMethod = new PostMethod(frameworkUrl + "/aa/login");
    postMethod.addParameter("target", frameworkUrl);
    client.getState().addCookie(sessionCookie);
    ProxyHelper.executeMethod(client, postMethod);

    if (HttpServletResponse.SC_SEE_OTHER != postMethod.getStatusCode()) {
        throw new HttpException("Wrong status code: " + login.getStatusCode());
    }

    String userHandle = null;
    Header headers[] = postMethod.getResponseHeaders();
    for (int i = 0; i < headers.length; ++i) {
        if ("Location".equals(headers[i].getName())) {
            String location = headers[i].getValue();
            int index = location.indexOf('=');
            userHandle = new String(
                    Base64.getDecoder().decode(location.substring(index + 1, location.length())));
            // System.out.println("location: "+location);
            // System.out.println("handle: "+userHandle);
        }
    }

    if (userHandle == null) {
        throw new ServiceException("User not logged in.");
    }
    return userHandle;
}

From source file:de.mpg.escidoc.services.framework.AdminHelper.java

/**
 * Logs in the given user with the given password.
 * /*w ww. ja  v  a 2s  .  c  o  m*/
 * @param userid The id of the user to log in.
 * @param password The password of the user to log in.
 * @return The handle for the logged in user.
 * @throws HttpException
 * @throws IOException
 * @throws ServiceException
 * @throws URISyntaxException 
 */
public static String loginUser(String userid, String password)
        throws HttpException, IOException, ServiceException, URISyntaxException {
    String frameworkUrl = ServiceLocator.getLoginUrl();

    int delim1 = frameworkUrl.indexOf("//");
    int delim2 = frameworkUrl.indexOf(":", delim1);

    String host;
    int port;

    if (delim2 > 0) {
        host = frameworkUrl.substring(delim1 + 2, delim2);
        port = Integer.parseInt(frameworkUrl.substring(delim2 + 1));
    } else {
        host = frameworkUrl.substring(delim1 + 2);
        port = 80;
    }
    HttpClient client = new HttpClient();

    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

    PostMethod login = new PostMethod(frameworkUrl + "/aa/j_spring_security_check");
    login.addParameter("j_username", userid);
    login.addParameter("j_password", password);

    ProxyHelper.executeMethod(client, login);

    login.releaseConnection();
    CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
    Cookie[] logoncookies = cookiespec.match(host, port, "/", false, client.getState().getCookies());

    Cookie sessionCookie = logoncookies[0];

    PostMethod postMethod = new PostMethod(frameworkUrl + "/aa/login");
    postMethod.addParameter("target", frameworkUrl);
    client.getState().addCookie(sessionCookie);
    ProxyHelper.executeMethod(client, postMethod);

    if (HttpServletResponse.SC_SEE_OTHER != postMethod.getStatusCode()) {
        throw new HttpException("Wrong status code: " + login.getStatusCode());
    }

    String userHandle = null;
    Header headers[] = postMethod.getResponseHeaders();
    for (int i = 0; i < headers.length; ++i) {
        if ("Location".equals(headers[i].getName())) {
            String location = headers[i].getValue();
            int index = location.indexOf('=');
            userHandle = new String(Base64.decode(location.substring(index + 1, location.length())));
            //System.out.println("location: "+location);
            //System.out.println("handle: "+userHandle);
        }
    }

    if (userHandle == null) {
        throw new ServiceException("User not logged in.");
    }
    return userHandle;
}

From source file:com.google.ie.common.openid.appengine.Openid4javaFetcher.java

private static boolean isRedirect(int responseCode) {
    switch (responseCode) {
    case HttpServletResponse.SC_MOVED_PERMANENTLY:
    case HttpServletResponse.SC_MOVED_TEMPORARILY:
    case HttpServletResponse.SC_SEE_OTHER:
    case HttpServletResponse.SC_TEMPORARY_REDIRECT:
        return true;
    default://from w  w w .j  av  a2 s. c o  m
        return false;
    }
}

From source file:de.mpg.imeji.presentation.util.Scripts.java

public static String login(String frameworkUrl, String userName, String password) throws Exception {
    StringTokenizer tokens = new StringTokenizer(frameworkUrl, "//");

    tokens.nextToken();// w  w  w  .  j  a  va 2s  .co m
    StringTokenizer hostPort = new StringTokenizer(tokens.nextToken(), ":");

    String host = hostPort.nextToken();
    int port = Integer.parseInt(hostPort.nextToken());

    HttpClient client = new HttpClient();
    client.getHostConfiguration().setHost(host, port, "http");
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

    PostMethod login = new PostMethod(frameworkUrl + "/aa/j_spring_security_check");
    login.addParameter("j_username", userName);
    login.addParameter("j_password", password);

    client.executeMethod(login);
    //System.out.println("Login form post: " + login.getStatusLine().toString());

    login.releaseConnection();
    CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
    Cookie[] logoncookies = cookiespec.match(host, port, "/", false, client.getState().getCookies());

    Cookie sessionCookie = logoncookies[0];

    PostMethod postMethod = new PostMethod("/aa/login");
    postMethod.addParameter("target", frameworkUrl);
    client.getState().addCookie(sessionCookie);
    client.executeMethod(postMethod);
    //System.out.println("Login second post: " + postMethod.getStatusLine().toString());

    if (HttpServletResponse.SC_SEE_OTHER != postMethod.getStatusCode()) {
        throw new HttpException("Wrong status code: " + login.getStatusCode());
    }

    String userHandle = null;
    Header headers[] = postMethod.getResponseHeaders();
    for (int i = 0; i < headers.length; ++i) {
        if ("Location".equals(headers[i].getName())) {
            String location = headers[i].getValue();
            int index = location.indexOf('=');
            userHandle = new String(Base64.decode(location.substring(index + 1, location.length())));
            //System.out.println("location: "+location);
            //System.out.println("handle: "+userHandle);
        }
    }

    if (userHandle == null) {
        throw new ServiceException("User not logged in.");
    }
    return userHandle;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.InstitutionalInternalClassController.java

private RedirectResponseValues redirectToSiteAdmin() {
    return new RedirectResponseValues(REDIRECT_PAGE, HttpServletResponse.SC_SEE_OTHER);
}

From source file:org.picketlink.test.identity.federation.bindings.wildfly.SPInitiatedSSOWorkflowTestCase.java

@Test
public void testSSO() throws Exception {
    String spURI = "http://localhost:8080/" + getContextPathShortForm() + "/secured/test";
    WebRequest serviceRequest1 = new GetMethodWebRequest(spURI);
    webConversation = new WebConversation();
    HttpUnitOptions.setLoggingHttpHeaders(true);

    webResponse = webConversation.getResponse(serviceRequest1);

    responseCode = webResponse.getResponseCode();
    if (responseCode == HttpServletResponse.SC_SEE_OTHER) {
        String otherLocation = webResponse.getHeaderField("LOCATION");
        webResponse = webConversation.getResponse(otherLocation);
    }//  w  w w. j  av  a 2 s. co  m
    WebForm loginForm = webResponse.getForms()[0];
    loginForm.setParameter("j_username", "user1");
    loginForm.setParameter("j_password", "password1");
    SubmitButton submitButton = loginForm.getSubmitButtons()[0];
    submitButton.click();

    webResponse = webConversation.getCurrentPage();
    responseCode = webResponse.getResponseCode();
    while (responseCode == 303) {
        handle303();
    }
    String text = webResponse.getText();
    assertTrue(" Saw user1 ", text.contains("user1"));
}