Example usage for javax.servlet.http HttpServletRequest getCookies

List of usage examples for javax.servlet.http HttpServletRequest getCookies

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getCookies.

Prototype

public Cookie[] getCookies();

Source Link

Document

Returns an array containing all of the Cookie objects the client sent with this request.

Usage

From source file:io.hops.hopsworks.api.zeppelin.util.ZeppelinResource.java

/**
 * Retrieves projectId from cookies and returns the project associated with
 * the id.//ww  w.  j  a  v  a2  s .c  o  m
 *
 * @param request
 * @return
 */
public Project getProjectNameFromCookies(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    String projectId = null;
    Integer pId;
    Project project;
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("projectID")) {
                projectId = cookies[i].getValue();
                break;
            }
        }
    }
    try {
        pId = Integer.valueOf(projectId);
        project = projectBean.find(pId);
    } catch (NumberFormatException e) {
        return null;
    }
    return project;
}

From source file:com.ibm.xsp.webdav.DAVCredentials.java

/**
 * Gets the LTPA Tokens from the request if they can be found
 * //from  w w  w  .ja  v  a 2s. c o  m
 * @param req
 */
private void updateLTPAfromRequest(HttpServletRequest req) {
    // Look for LTPA Tokens in the cookies
    Cookie[] cookies = req.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("LtpaToken")) {
                this.ltpaToken = cookies[i].getValue();
                LOGGER.debug("Found LTPA Token:" + this.ltpaToken);
            }
            if (cookies[i].getName().equals("LtpaToken2")) {
                this.ltpaToken2 = cookies[i].getValue();
                LOGGER.debug("Found LTPA Token 2:" + this.ltpaToken2);
            }
        }
    }
}

From source file:org.craftercms.engine.view.freemarker.CrafterFreeMarkerView.java

protected Map<String, String> createCookieMap(HttpServletRequest request) {
    Map<String, String> cookieMap = new HashMap<String, String>();
    Cookie[] cookies = request.getCookies();

    if (ArrayUtils.isNotEmpty(cookies)) {
        for (Cookie cookie : cookies) {
            cookieMap.put(cookie.getName(), cookie.getValue());
        }/*w  ww . j  a  v  a  2s.c  o  m*/
    }

    return cookieMap;
}

From source file:com.streamsets.lib.security.http.SSOUserAuthenticator.java

Cookie getAuthCookie(HttpServletRequest httpReq) {
    Cookie[] cookies = httpReq.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(getAuthCookieName(httpReq))) {
                return cookie;
            }//from w w  w. ja v  a2 s  .  c  o  m
        }
    }
    return null;
}

From source file:marketDB.update_db.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String path = "C:\\Users\\Asus X550ZE\\Documents\\NetBeansProjects\\Web App\\web\\img";
    Cookie[] cookies = request.getCookies();
    String access_token = "";
    for (int i = 0; i < cookies.length; i++) {
        if (cookies[i].getName().equals("access_token")) {
            access_token = cookies[i].getValue();
        }//from   ww  w. j a  v  a 2 s  .  com
    }
    String product_id = request.getParameter("product_id");
    String product_name = request.getParameter("prod_name");
    String desc = request.getParameter("desc_box");
    String price = request.getParameter("price_box");
    Part filePart = request.getPart("imgcatch");
    String fileName = getFileName(filePart);

    InputStream in = filePart.getInputStream();
    byte[] imgByte = readFully(in);
    int status = 0;

    try {
        status = editProduct(access_token, product_id, product_name, desc, price, fileName, imgByte);
        //WEB SERVICE UPDATE PRODUCT
    } catch (IOException_Exception ex) {
        Logger.getLogger(update_db.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ProtocolException_Exception ex) {
        Logger.getLogger(update_db.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseException_Exception ex) {
        Logger.getLogger(update_db.class.getName()).log(Level.SEVERE, null, ex);
    }

    JSONObject arrayObj = new JSONObject();
    arrayObj.put("status", status);
    if (status == 1) {
        response.sendRedirect("your_product.jsp");
    } else {
        response.sendRedirect("login.jsp");
    }

}

From source file:logout2_servlet.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request// w  w  w.j  av  a2 s.  c o  m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String access_token = "";
    Cookie cookie = null;
    Cookie[] cookies = request.getCookies();
    for (int i = 0; i < cookies.length; i++) {
        Cookie cookie1 = cookies[i];
        if (cookies[i].getName().equals("access_token")) {
            access_token = cookie1.getValue();
            cookie = cookie1;
        }
    }
    System.out.println("TOKEN = " + access_token);

    String USER_AGENT = "Mozilla/5.0";
    String url = "http://localhost:8082/Identity_Service/logout_servlet";
    URL connection = new URL(url);
    HttpURLConnection con = (HttpURLConnection) connection.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

    String urlParameters = "access_token=" + access_token;

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuilder resp = new StringBuilder();
    while ((inputLine = in.readLine()) != null) {
        resp.append(inputLine);
    }
    in.close();

    JSONParser parser = new JSONParser();
    JSONObject obj = null;
    try {
        obj = (JSONObject) parser.parse(resp.toString());
    } catch (ParseException ex) {
        Logger.getLogger(logout2_servlet.class.getName()).log(Level.SEVERE, null, ex);
    }

    String status = (String) obj.get("status");
    System.out.println(status);
    if (status.equals("ok")) {
        cookie.setMaxAge(0);
        response.sendRedirect("login.jsp");
    } else {

    }

}

From source file:com.hangum.tadpole.preference.ui.GeneralPreferencePage.java

/**
 * initialize locale//from  w ww .  j a v  a  2s.  com
 */
private void initLocale() {

    // ? ?   ?  ..
    HttpServletRequest request = RWT.getRequest();
    Cookie[] cookies = request.getCookies();

    boolean isExist = false;
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (PublicTadpoleDefine.TDB_COOKIE_USER_LANGUAGE.equals(cookie.getName())) {
                comboLanguage.setText(cookie.getValue());
                changeUILocale(comboLanguage.getText());
                isExist = true;

                break;
            }
        }
    }

    // ?  ?? ?  .
    if (!isExist)
        comboLanguage.setText(Locale.ENGLISH.getDisplayLanguage(Locale.ENGLISH));

}

From source file:marketDB.add_db.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String path = "C:\\Users\\Asus X550ZE\\Documents\\NetBeansProjects\\Web App\\web\\img";
    Cookie[] cookies = request.getCookies();
    String access_token = "";
    for (int i = 0; i < cookies.length; i++) {
        if (cookies[i].getName().equals("access_token")) {
            access_token = cookies[i].getValue();
        }/*from w  w  w . ja  va 2s.  c o m*/
    }

    String product_name = request.getParameter("prod_name");
    String desc = request.getParameter("desc_box");
    String price = request.getParameter("price_box");
    Part filePart = request.getPart("imgcatch");
    String fileName = getFileName(filePart);

    InputStream in = filePart.getInputStream();
    byte[] imgByte = readFully(in);
    int status = 0;

    try {
        status = addProduct(access_token, product_name, desc, price, fileName, imgByte);
    } catch (ProtocolException_Exception ex) {
        Logger.getLogger(add_db.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseException_Exception ex) {
        Logger.getLogger(add_db.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException_Exception ex) {
        Logger.getLogger(add_db.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (status == 1) {
        //WEB SERVICE ADD PRODUCT
        JSONObject arrayObj = new JSONObject();
        //arrayObj.put("status",status);
        //response.setContentType("application/json:charset=UTF-8");
        //response.getWriter().write(arrayObj.toString());
        response.sendRedirect("your_product.jsp");
    } else {
        response.sendRedirect("login.jsp");
    }
}

From source file:com.bosch.cr.examples.jwt.CustomProxyServlet.java

/**
 * Adds the {@code x-cr-api-token} and {@code Authorization} header to the {@code proxyRequest}. Authorization is
 * set only if an authorization cookie containing a JWT is present on the {@code request}. The JWT cookie is set on
 * login and send back to the browser.// w  ww. j a  v  a2  s.c  om
 *
 * @param request the request.
 * @param proxyRequest the proxy request.
 * @see ImAuthenticationServlet
 * @see GoogleCallbackServlet
 */
@Override
protected void copyRequestHeaders(final HttpServletRequest request, final HttpRequest proxyRequest) {
    super.copyRequestHeaders(request, proxyRequest);

    final Optional<Cookie> authorizationCookie = Stream.of(request.getCookies()) //
            .filter(CookieUtil::isJwtAuthenticationCookie) //
            .findFirst();

    if (authorizationCookie.isPresent()) {
        // add authorization if cookie is present
        proxyRequest.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + authorizationCookie.get().getValue());
    }

    // add api token
    proxyRequest.addHeader(X_CR_API_TOKEN_HTTP_HEADER,
            configurationProperties.getPropertyAsString(ConfigurationProperty.THINGS_API_TOKEN));

    System.out.println("Proxying request with Headers:");
    System.out.println(proxyRequest.getFirstHeader(HttpHeaders.AUTHORIZATION));
}

From source file:com.boylesoftware.web.impl.auth.SessionlessAuthenticationService.java

/**
 * Get authentication cookie value./*w  ww.  j  a  v  a  2s.  c om*/
 *
 * @param request The HTTP request.
 *
 * @return The cookie value, or {@code null} if not found.
 */
private String getAuthCookieValue(final HttpServletRequest request) {

    final Cookie[] cookies = request.getCookies();
    if (cookies != null)
        for (final Cookie cookie : request.getCookies())
            if (cookie.getName().equals(AUTH_COOKIE_NAME))
                return cookie.getValue();

    return null;
}