Example usage for org.springframework.web.context.request RequestContextHolder currentRequestAttributes

List of usage examples for org.springframework.web.context.request RequestContextHolder currentRequestAttributes

Introduction

In this page you can find the example usage for org.springframework.web.context.request RequestContextHolder currentRequestAttributes.

Prototype

public static RequestAttributes currentRequestAttributes() throws IllegalStateException 

Source Link

Document

Return the RequestAttributes currently bound to the thread.

Usage

From source file:org.medici.bia.common.util.HtmlUtils.java

private static List<String> showUser(List<String> inputList, String account, boolean forTeaching) {
    if (inputList == null)
        return null;

    ArrayList<String> retValue = new ArrayList<String>(inputList.size());

    String anchorBegin = "<a class=\"searchResult\" href=\"";
    anchorBegin += ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()
            .getContextPath();//from  www.  j  a v a2  s. c om
    anchorBegin += "/" + (forTeaching ? "teaching" : "admin") + "/ShowUser.do?account=";
    anchorBegin += account;
    anchorBegin += "\">";
    String hrefEnd = "</a>";

    for (int i = 0; i < inputList.size(); i++) {
        retValue.add(anchorBegin + inputList.get(i) + hrefEnd);
    }

    return retValue;
}

From source file:org.medici.bia.common.util.HtmlUtils.java

/**
 * /*from w ww .  j a v  a2s. com*/
 * @param inputList
 * @param idFilter
 * @return
 */
public static List<String> showUserSearchFilter(List<Object> inputList, Integer idSearchFilter,
        SearchType searchType) {
    if (inputList == null)
        return null;

    ArrayList<String> retValue = new ArrayList<String>(inputList.size());

    StringBuilder anchorBegin = new StringBuilder("<a class=\"searchResult\" href=\"");
    anchorBegin.append(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()
            .getContextPath());
    anchorBegin.append("/src/AdvancedSearch.do?idSearchFilter=");
    anchorBegin.append(idSearchFilter);
    anchorBegin.append("&searchType=");
    anchorBegin.append(searchType);
    anchorBegin.append("\">");
    String hrefEnd = "</a>";

    for (int i = 0; i < inputList.size(); i++) {
        retValue.add(anchorBegin + inputList.get(i).toString() + hrefEnd);
    }

    return retValue;
}

From source file:org.medici.bia.common.util.HtmlUtils.java

/**
 * /*from  w w w  . j av a2  s  . c  o m*/
 * @param inputList
 * @param summaryId
 * @return
 */
public static List<String> showVolume(List<String> inputList, Integer summaryId) {
    if (inputList == null)
        return null;

    ArrayList<String> retValue = new ArrayList<String>(inputList.size());

    StringBuilder anchorBegin = new StringBuilder("<a class=\"searchResult\" href=\"");
    anchorBegin.append(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()
            .getContextPath());
    anchorBegin.append("/src/volbase/ShowVolume.do?summaryId=");
    anchorBegin.append(summaryId);
    anchorBegin.append("\">");
    String hrefEnd = "</a>";

    for (int i = 0; i < inputList.size(); i++) {
        StringBuilder stringBuilder = new StringBuilder(anchorBegin.toString());
        stringBuilder.append(inputList.get(i));
        stringBuilder.append(hrefEnd);
        retValue.add(stringBuilder.toString());
    }

    return retValue;
}

From source file:org.medici.bia.common.util.HtmlUtils.java

/**
 * /*w  ww  .j  av a 2 s  .c om*/
 * @param inputList
 * @param entryId
 * @return
 */
public static List<String> showSearch(List<String> inputList, Integer idUserHistory) {
    if (inputList == null)
        return null;

    ArrayList<String> retValue = new ArrayList<String>(inputList.size());

    StringBuilder anchorBegin = new StringBuilder("<a class=\"searchResultUserSearch\" href=\"");
    anchorBegin.append(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()
            .getContextPath());
    anchorBegin.append("/src/ShowSearchUserHistory.do?idUserHistory=");
    anchorBegin.append(idUserHistory);
    anchorBegin.append("\">");
    String hrefEnd = "</a>";

    for (int i = 0; i < inputList.size(); i++) {
        StringBuilder stringBuilder = new StringBuilder(anchorBegin.toString());
        stringBuilder.append(inputList.get(i));
        stringBuilder.append(hrefEnd);
        retValue.add(stringBuilder.toString());
    }

    return retValue;
}

From source file:org.medici.bia.controller.manuscriptviewer.AjaxController.java

/**
 * This method update the annotations of the folio presented in the manuscript viewer.
 * // w  w w . ja  v  a 2  s .co  m
 * @param httpServletRequest the request
 * @return
 */
@RequestMapping(value = { "/src/mview/UpdateAnnotations.json",
        "/de/mview/UpdateAnnotations.json" }, method = RequestMethod.POST)
public Map<String, Object> updateAnnotations(HttpServletRequest httpServletRequest) {
    Map<String, Object> model = new HashMap<String, Object>(0);

    String account = ((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
            .getUsername();
    Boolean administrator = getUserService().isAccountAdministrator(account);

    try {
        // In this controller we get input parameter at low level because 
        // there is a bug in spring which construct a wrong list of 
        // annotations in case of client send 1 single annotation 
        //String imageName = httpServletRequest.getParameter("imageName");
        Integer imageId = NumberUtils.toInt(httpServletRequest.getParameter("imageId"));
        String[] annotationsFormView = httpServletRequest.getParameterValues("annotations");
        List<Annotation> annotationsList = new ArrayList<Annotation>(0);
        List<Object> resultList = new ArrayList<Object>();

        if (annotationsFormView != null) {
            for (String string : annotationsFormView) {
                //Next code is instructed on code of javascript IIPMooViewer.annotationsAsQueryParameterString
                String[] splitted = StringUtils.splitPreserveAllTokens(string, "");
                Annotation annotation = new Annotation();
                annotation.setAnnotationId(NumberUtils.toInt(splitted[0]));
                annotation.setX(NumberUtils.toDouble(splitted[2]));
                annotation.setY(NumberUtils.toDouble(splitted[3]));
                annotation.setWidth(NumberUtils.toDouble(splitted[4]));
                annotation.setHeight(NumberUtils.toDouble(splitted[5]));
                annotation.setType(Annotation.Type.valueOf(splitted[6].toUpperCase()));
                annotation.setTitle(splitted[7]);
                annotation.setText(splitted[8]);
                annotation.setVisible(Boolean.valueOf(splitted[11]));
                annotationsList.add(annotation);
            }
        }
        Map<Annotation, Integer> imageAnnotationsMap = getManuscriptViewerService().updateAnnotations(imageId,
                annotationsList, httpServletRequest.getRemoteAddr(), administrator);
        for (Annotation currentAnnotation : imageAnnotationsMap.keySet()) {
            Map<String, Object> singleRow = new HashMap<String, Object>(0);
            if (imageAnnotationsMap.get(currentAnnotation) > -1) {
                singleRow.put("forum",
                        ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                                .getRequest().getContextPath()
                                + "/community/EditForumPostAnnotation.do?topicId="
                                + imageAnnotationsMap.get(currentAnnotation));
                resultList.add(singleRow);
            }
        }
        // links -> only new annotations associated to a forum 
        model.put("links", resultList);
        // annotation -> all of the annotations associated to the current image
        model.put("annotations", getAnnotationsForView(null, imageAnnotationsMap.keySet()));
        model.put("adminPrivileges", administrator);
    } catch (ApplicationThrowable applicationThrowable) {
        model.put("operation", "KO");
        model.put("error", applicationThrowable.getMessage() != null ? applicationThrowable.toString()
                : applicationThrowable.getCause().toString());
        return model;
    }
    model.put("operation", "OK");
    return model;
}

From source file:org.medici.bia.controller.teaching.AjaxController.java

/**
 * This method update the annotations of the folio presented in the manuscript viewer.
 * //from ww w. j a va2  s  .  c om
 * @param httpServletRequest the request
 * @return
 */
@RequestMapping(value = { "/teaching/UpdateAnnotations.json" }, method = RequestMethod.POST)
public Map<String, Object> updateAnnotations(HttpServletRequest httpServletRequest) {
    Map<String, Object> model = new HashMap<String, Object>(0);

    try {
        Boolean isAdminOrTeacher = isAdminOrTeacher();

        // In this controller we get input parameter at low level beacause 
        // there is a bug in spring which construct a wrong list of 
        // annotations in case of client send 1 single annotation 
        //String imageName = httpServletRequest.getParameter("imageName");
        Integer imageId = NumberUtils.toInt(httpServletRequest.getParameter("imageId"));
        String forumContainerIdFromView = httpServletRequest.getParameter("resourcesForum");
        Integer forumContainerId = forumContainerIdFromView != null
                ? NumberUtils.toInt(forumContainerIdFromView)
                : null;
        String[] annotationsFormView = httpServletRequest.getParameterValues("annotations");
        List<Annotation> annotationsList = new ArrayList<Annotation>(0);
        List<Object> resultList = new ArrayList<Object>();

        if (annotationsFormView != null) {
            for (String string : annotationsFormView) {
                //Next code is instructed on code of javascript IIPMooViewer.annotationsAsQueryParameterString
                String[] splitted = org.apache.commons.lang.StringUtils.splitPreserveAllTokens(string, "");
                Annotation annotation = new Annotation();
                annotation.setAnnotationId(NumberUtils.toInt(splitted[0]));
                annotation.setX(NumberUtils.toDouble(splitted[2]));
                annotation.setY(NumberUtils.toDouble(splitted[3]));
                annotation.setWidth(NumberUtils.toDouble(splitted[4]));
                annotation.setHeight(NumberUtils.toDouble(splitted[5]));
                annotation.setType(Annotation.Type.valueOf(splitted[6].toUpperCase()));
                annotation.setTitle(splitted[7]);
                annotation.setText(splitted[8]);
                annotation.setVisible(Boolean.valueOf(splitted[11]));
                annotation.setRgbColor("none".equals(splitted[12]) ? null : splitted[12]);
                annotationsList.add(annotation);
            }
        }
        Map<Annotation, Integer> imageAnnotationsMap = getTeachingService().updateAnnotations(imageId,
                forumContainerId, annotationsList, httpServletRequest.getRemoteAddr(), isAdminOrTeacher);
        for (Annotation currentAnnotation : imageAnnotationsMap.keySet()) {
            Map<String, Object> singleRow = new HashMap<String, Object>(0);
            if (imageAnnotationsMap.get(currentAnnotation) > -1) {
                String topicForumURL = ((ServletRequestAttributes) RequestContextHolder
                        .currentRequestAttributes()).getRequest().getContextPath();
                topicForumURL += "/teaching/ShowTopicForum.do?topicId="
                        + currentAnnotation.getForumTopic().getTopicId();
                topicForumURL += "&forumId=" + currentAnnotation.getForumTopic().getForum().getForumId();
                topicForumURL += "&completeDOM=true";
                singleRow.put("forum", topicForumURL);
                resultList.add(singleRow);
            }
        }
        // links -> only new annotations associated to a forum 
        model.put("links", resultList);
        // annotation -> all of the annotations associated to the current image
        model.put("annotations", getAnnotationsForView(imageAnnotationsMap.keySet()));
        model.put("adminPrivileges", isAdminOrTeacher);
    } catch (ApplicationThrowable applicationThrowable) {
        model.put("operation", "KO");
        model.put("error", applicationThrowable.getMessage() != null ? applicationThrowable.toString()
                : applicationThrowable.getCause().toString());
        return model;
    }

    model.put("operation", "OK");
    return model;
}

From source file:org.medici.bia.service.user.UserServiceImpl.java

/**
 * {@inheritDoc}//www.  j a  v a  2 s . co  m
 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public void activateUser(UUID uuid) throws ApplicationThrowable {
    try {
        //Extract activationUser entity to obtains account linked the activation request
        ActivationUser activationUser = getActivationUserDAO().find(uuid.toString());

        //Search user by account and update active flag
        User user = activationUser.getUser();
        user.setActive(Boolean.TRUE);
        user.setActivationDate(new Date());
        getUserDAO().merge(user);

        //Complete the activation by updating the activation request.
        activationUser.setActive(Boolean.TRUE);
        activationUser.setActivationDate(new Date());
        activationUser.setActivationIPAddress(
                ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()
                        .getRemoteAddr());
        getActivationUserDAO().merge(activationUser);

        ApprovationUser approvationUser = getApprovationUserDAO().find(user.getAccount());
        if (approvationUser != null) {
            approvationUser.setMessageSended(Boolean.FALSE);
            approvationUser.setApproved(Boolean.FALSE);
            approvationUser.setMailSended(Boolean.FALSE);
            getApprovationUserDAO().merge(approvationUser);
        } else {
            approvationUser = new ApprovationUser(user);
            approvationUser.setMessageSended(Boolean.FALSE);
            approvationUser.setApproved(Boolean.FALSE);
            approvationUser.setMailSended(Boolean.FALSE);
            getApprovationUserDAO().persist(approvationUser);
        }
    } catch (Throwable th) {
        throw new ApplicationThrowable(th);
    }
}

From source file:org.medici.bia.service.user.UserServiceImpl.java

/**
 * {@inheritDoc}/*from ww  w. ja v  a 2s .  c  om*/
 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public void registerUser(User user) throws ApplicationThrowable {
    try {
        // User account is generated by application
        user.setAccount(generateAccount(user));
        user.setPassword(getPasswordEncoder().encodePassword(user.getPassword(), null));
        //User initial is generated by application
        user.setInitials(generateInitials(user));

        if (user.getMiddleName() == null) {
            user.setMiddleName("");
        }

        user.setRegistrationDate(new Date());
        Calendar expirationDate = Calendar.getInstance();
        expirationDate.add(Calendar.MONTH, NumberUtils.createInteger(
                ApplicationPropertyManager.getApplicationProperty("user.expiration.user.months")));
        user.setExpirationDate(expirationDate.getTime());
        Calendar expirationPasswordDate = Calendar.getInstance();
        expirationPasswordDate.add(Calendar.MONTH, NumberUtils.createInteger(
                ApplicationPropertyManager.getApplicationProperty("user.expiration.password.months")));
        user.setExpirationPasswordDate(expirationPasswordDate.getTime());
        user.setBadLogin(0);
        user.setActive(false);
        user.setApproved(false);
        user.setLocked(false);
        user.setForumNumberOfPost(new Long(0));
        user.setMailHide(Boolean.TRUE);
        user.setPortrait(Boolean.FALSE);
        user.setMailNotification(Boolean.FALSE);
        user.setForumTopicSubscription(Boolean.TRUE);

        getUserDAO().persist(user);

        // Every user is always register as COMMUNITY_USER
        UserAuthority userAuthority = getUserAuthorityDAO().find(Authority.COMMUNITY_USERS);

        UserRole userRole = new UserRole(user, userAuthority);
        getUserRoleDAO().persist(userRole);

        if (getUserPersonalNotesDAO().getPersonalNotes(user) == null) {
            UserPersonalNotes userPersonalNotes = new UserPersonalNotes();
            userPersonalNotes.setUser(user);
            userPersonalNotes.setPersonalNotes("");
        }

        // We generate the request for sending activation mail 
        ActivationUser activationUser = new ActivationUser();
        activationUser.setUuid(UUID.randomUUID().toString());
        activationUser.setUser(user);
        activationUser.setActive(Boolean.FALSE);
        activationUser.setMailSended(Boolean.FALSE);
        activationUser.setRequestDate(new Date());
        activationUser.setRequestIPAddress(
                ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()
                        .getRemoteAddr());
        getActivationUserDAO().persist(activationUser);
    } catch (Throwable th) {
        throw new ApplicationThrowable(th);
    }
}

From source file:org.medici.bia.service.user.UserServiceImpl.java

/**
 * {@inheritDoc}//  w  w w .  j a  v a2 s.  c om
 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public void updateUserPassword(UUID uuid, String password) throws ApplicationThrowable {
    try {
        PasswordChangeRequest passwordChangeRequest = getPasswordChangeRequestDAO().find(uuid.toString());

        User user = passwordChangeRequest.getUser();
        user.setPassword(getPasswordEncoder().encodePassword(password, null));
        getUserDAO().merge(user);
        passwordChangeRequest.setReset(Boolean.TRUE);
        passwordChangeRequest.setResetDate(new Date());
        passwordChangeRequest
                .setResetIPAddress(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                        .getRequest().getRemoteAddr());
        getPasswordChangeRequestDAO().merge(passwordChangeRequest);
    } catch (Throwable th) {
        throw new ApplicationThrowable(th);
    }
}

From source file:org.molgenis.util.GsonHttpMessageConverter.java

private String getCallbackParam() {
    try {//from   www .j  a  va  2  s . co  m
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes()).getRequest();
        return request.getParameter("callback");
    } catch (IllegalStateException ex) {
        return null;
    }
}