Example usage for javax.servlet.http HttpServletRequest getRemoteUser

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

Introduction

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

Prototype

public String getRemoteUser();

Source Link

Document

Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.

Usage

From source file:org.apache.hadoop.hbase.thrift.ThriftHttpServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String effectiveUser = request.getRemoteUser();
    if (securityEnabled) {
        try {//from   ww w .  j  av  a  2s. c  o m
            // As Thrift HTTP transport doesn't support SPNEGO yet (THRIFT-889),
            // Kerberos authentication is being done at servlet level.
            effectiveUser = doKerberosAuth(request);
            // It is standard for client applications expect this header.
            // Please see http://tools.ietf.org/html/rfc4559 for more details.
            response.addHeader(WWW_AUTHENTICATE, NEGOTIATE + " " + outToken);
        } catch (HttpAuthenticationException e) {
            LOG.error("Kerberos Authentication failed", e);
            // Send a 401 to the client
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.addHeader(WWW_AUTHENTICATE, NEGOTIATE);
            response.getWriter().println("Authentication Error: " + e.getMessage());
            return;
        }
    }
    String doAsUserFromQuery = request.getHeader("doAs");
    if (effectiveUser == null) {
        effectiveUser = realUser.getShortUserName();
    }
    if (doAsUserFromQuery != null) {
        if (!doAsEnabled) {
            throw new ServletException("Support for proxyuser is not configured");
        }
        // The authenticated remote user is attempting to perform 'doAs' proxy user.
        UserGroupInformation remoteUser = UserGroupInformation.createRemoteUser(effectiveUser);
        // create and attempt to authorize a proxy user (the client is attempting
        // to do proxy user)
        UserGroupInformation ugi = UserGroupInformation.createProxyUser(doAsUserFromQuery, remoteUser);
        // validate the proxy user authorization
        try {
            ProxyUsers.authorize(ugi, request.getRemoteAddr(), conf);
        } catch (AuthorizationException e) {
            throw new ServletException(e.getMessage());
        }
        effectiveUser = doAsUserFromQuery;
    }
    hbaseHandler.setEffectiveUser(effectiveUser);
    super.doPost(request, response);
}

From source file:MainClass.java

public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, java.io.IOException {

    res.setContentType("text/html");
    java.io.PrintWriter out = res.getWriter();
    out.println("<HTML>");
    out.println("<HEAD><TITLE>User Example</TITLE></HEAD>");
    out.println("<BODY>");

    String username = req.getRemoteUser();
    if (username == null) {
        out.println("Hello. You are not logged in.");
    } else if ("Bob".equals(username)) {
        out.println("Hello, Bob. Nice to see you again.");
    } else {//from   w  w w .  j  a va  2  s. c  om
        out.println("Hello, " + username + ".");
    }
    out.println("</BODY>");
    out.println("</HTML>");
    out.close();
}

From source file:org.jasig.portlet.attachment.mvc.LocalAttachmentController.java

@RequestMapping(value = "/content/attach/local.json", method = RequestMethod.POST)
public ModelAndView uploadForm(@RequestParam(value = "qqfile") MultipartFile file,
        HttpServletRequest servletRequest) throws IOException {
    final Map<String, String> model = new HashMap<String, String>();
    final String user = (String) servletRequest.getSession()
            .getAttribute(AttachmentsController.REMOTE_USER_ATTR);
    final HttpServletRequest request = new AttachmentServletRequestWrapper(servletRequest, user);

    if (file != null) {
        Attachment attachment = generateAttachment(file, request);
        attachment = attachmentService.save(attachment, request.getRemoteUser());
        if (attachment.getId() > 0) {
            String path = getAttachmentAbsolutePath(attachment, request);
            FileUtil.write(path, file.getBytes());
            model.put("id", Long.toString(attachment.getId()));
            model.put("guid", attachment.getGuid());
            model.put("path", attachment.getPath());
            model.put("filename", attachment.getFilename());
            model.put("success", "true");
        }/*from   ww w.  ja  v  a2 s . c  om*/
    }

    return new ModelAndView("jsonView", model);
}

From source file:net.big_oh.common.web.listener.request.ObservedHttpServletRequest.java

public ObservedHttpServletRequest(HttpServletRequest request) {
    super(request);

    httpMethod = request.getMethod();/* w  w w .  j a  v  a 2s  .  c o  m*/

    resourceRequested = request.getRequestURL().toString();

    queryString = request.getQueryString();

    contextName = request.getContextPath();

    containerUserName = request.getRemoteUser();

    HttpSession session = request.getSession(false);
    jSessionId = (session == null) ? null : session.getId();
}

From source file:org.jasig.portlet.attachment.mvc.LocalAttachmentController.java

@RequestMapping(value = "/content/attach/local", method = RequestMethod.POST, produces = "text/plain")
public ModelAndView cKEditorUpload(@RequestParam(value = "upload") MultipartFile file,
        @RequestParam(value = "CKEditorFuncNum") String functionNumber, HttpServletRequest servletRequest)
        throws IOException {

    final Map<String, Object> model = new HashMap<String, Object>();
    final String user = (String) servletRequest.getSession()
            .getAttribute(AttachmentsController.REMOTE_USER_ATTR);
    final HttpServletRequest request = new AttachmentServletRequestWrapper(servletRequest, user);

    if (file != null) {
        Attachment attachment = generateAttachment(file, request);
        attachment = attachmentService.save(attachment, request.getRemoteUser());
        if (attachment.getId() > 0) {
            String path = getAttachmentAbsolutePath(attachment, request);
            FileUtil.write(path, file.getBytes());

            model.put("functionNumber", functionNumber);
            model.put("attachment", attachment);
        } else {//from  ww  w .  jav  a  2  s .co m
            throw new RuntimeException("Failure to upload attachment:  " + attachment.getFilename());
        }
    }

    return new ModelAndView("ckeditor-callback", model);
}

From source file:alpha.portal.webapp.controller.PayloadVersionsController.java

/**
 * shows the list of payload versions./*from w  w w  .  j  a  va 2  s .c  om*/
 * 
 * @param request
 *            the request
 * @return ModelView
 * @throws Exception
 *             the exception
 */
@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(final HttpServletRequest request) throws Exception {

    final ModelAndView returnMaV = new ModelAndView();

    final User currentUser = this.getUserManager().getUserByUsername(request.getRemoteUser());
    final Locale locale = request.getLocale();

    if ((request.getParameter("card") == null) || (request.getParameter("case") == null)) {
        this.saveError(request, this.getText("payloadVersions.cardNotFound", locale));
        returnMaV.addObject("isErrors", true);
        return returnMaV;
    }
    final String cardId = request.getParameter("card");
    final String caseId = request.getParameter("case");

    final AlphaCard currentCard = this.alphaCardManager.get(new AlphaCardIdentifier(caseId, cardId));
    if (currentCard == null) {
        this.saveError(request, this.getText("payloadVersions.cardNotFound", locale));
        returnMaV.addObject("isErrors", true);
        return returnMaV;
    }

    Long cardContributor = null;
    try {
        cardContributor = Long.parseLong(currentCard.getAlphaCardDescriptor()
                .getAdornment(AdornmentType.Contributor.getName()).getValue());
    } catch (final NumberFormatException e) {
        cardContributor = 0L;
    }
    final Long currentUserId = currentUser.getId();
    if (cardContributor != currentUserId) {
        this.saveError(request, this.getText("adornment.noAccess", locale));
        returnMaV.addObject("isErrors", true);
        return returnMaV;
    }

    final List<Payload> payloads = this.payloadManager.getAllVersions(currentCard.getPayload());
    if (payloads.size() < 1) {
        this.saveError(request, this.getText("payloadVersions.noPayloads", locale));
        returnMaV.addObject("isErrors", true);
        return returnMaV;
    }

    returnMaV.addObject("payloadList", payloads);
    returnMaV.addObject("cardName", currentCard.getAlphaCardDescriptor().getTitle());
    returnMaV.addObject("caseId", currentCard.getAlphaCardIdentifier().getCaseId());
    returnMaV.addObject("cardId", currentCard.getAlphaCardIdentifier().getCardId());

    return returnMaV;
}

From source file:org.magnum.mobilecloud.video.VideoSvc.java

@RequestMapping(value = VideoSvcApi.VIDEO_SVC_PATH + "/{id}/like", method = RequestMethod.POST)
public @ResponseBody void likeVideo(@PathVariable("id") long id, HttpServletRequest request,
        HttpServletResponse response) {// w  w w  . j  av  a2 s  .c om
    Video video = videos.findOne(id);
    if (video != null) {
        Set<String> users = video.getLikesUserNames();
        String user = request.getRemoteUser();
        System.out.println("User: " + user);
        if (users.contains(user)) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        } else {
            users.add(user);
            video.setLikesUserNames(users);
            video.setLikes(users.size());
            videos.save(video);
            response.setStatus(HttpServletResponse.SC_OK);
        }
    } else {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:org.magnum.mobilecloud.video.VideoSvc.java

@RequestMapping(value = VideoSvcApi.VIDEO_SVC_PATH + "/{id}/unlike", method = RequestMethod.POST)
public @ResponseBody void unlikeVideo(@PathVariable("id") long id, HttpServletRequest request,
        HttpServletResponse response) {/*  w ww .j av  a2s  . c  om*/
    Video video = videos.findOne(id);
    if (video != null) {
        Set<String> users = video.getLikesUserNames();
        String user = request.getRemoteUser();
        if (users.contains(user)) {
            users.remove(user);
            video.setLikesUserNames(users);
            video.setLikes(users.size());
            videos.save(video);
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }

    } else {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:io.hops.hopsworks.api.user.UserService.java

@POST
@Path("changeTwoFactor")
@Produces(MediaType.APPLICATION_JSON)//  www.  ja va2s.  co m
public Response changeTwoFactor(@FormParam("password") String password,
        @FormParam("twoFactor") boolean twoFactor, @Context HttpServletRequest req) throws UserException {
    Users user = userBean.findByEmail(req.getRemoteUser());

    byte[] qrCode;
    RESTApiJsonResponse json = new RESTApiJsonResponse();
    if (user.getTwoFactor() == twoFactor) {
        json.setSuccessMessage("No change made.");
        return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(json).build();
    }

    qrCode = userController.changeTwoFactor(user, password, req);
    if (qrCode != null) {
        json.setQRCode(new String(Base64.encodeBase64(qrCode)));
    } else {
        json.setSuccessMessage("Tow factor authentication disabled.");
    }
    return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(json).build();
}

From source file:org.hoteia.qalingo.core.web.bean.clickstream.ClickstreamRequest.java

public ClickstreamRequest(HttpServletRequest request, Date timestamp) {
    protocol = request.getProtocol();/*from  w ww.  j  a  va  2 s . c  o m*/
    serverName = request.getServerName();
    serverPort = request.getServerPort();
    requestURI = request.getRequestURI();
    queryString = request.getQueryString();
    remoteUser = request.getRemoteUser();
    this.timestamp = timestamp;
}