Example usage for org.springframework.web.util UriComponents getQuery

List of usage examples for org.springframework.web.util UriComponents getQuery

Introduction

In this page you can find the example usage for org.springframework.web.util UriComponents getQuery.

Prototype

@Nullable
public abstract String getQuery();

Source Link

Document

Return the query.

Usage

From source file:org.mitre.discovery.util.WebfingerURLNormalizer.java

/**
 * Normalize the resource string as per OIDC Discovery.
 * @param identifier//from  w w  w. j a  v  a  2s .  c  o  m
 * @return the normalized string, or null if the string can't be normalized
 */
public static UriComponents normalizeResource(String identifier) {
    // try to parse the URI
    // NOTE: we can't use the Java built-in URI class because it doesn't split the parts appropriately

    if (Strings.isNullOrEmpty(identifier)) {
        logger.warn("Can't normalize null or empty URI: " + identifier);
        return null; // nothing we can do
    } else {

        //UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(identifier);
        UriComponentsBuilder builder = UriComponentsBuilder.newInstance();

        Matcher m = pattern.matcher(identifier);
        if (m.matches()) {
            builder.scheme(m.group(2));
            builder.userInfo(m.group(6));
            builder.host(m.group(8));
            String port = m.group(10);
            if (!Strings.isNullOrEmpty(port)) {
                builder.port(Integer.parseInt(port));
            }
            builder.path(m.group(11));
            builder.query(m.group(13));
            builder.fragment(m.group(15)); // we throw away the hash, but this is the group it would be if we kept it
        } else {
            // doesn't match the pattern, throw it out
            logger.warn("Parser couldn't match input: " + identifier);
            return null;
        }

        UriComponents n = builder.build();

        if (Strings.isNullOrEmpty(n.getScheme())) {
            if (!Strings.isNullOrEmpty(n.getUserInfo()) && Strings.isNullOrEmpty(n.getPath())
                    && Strings.isNullOrEmpty(n.getQuery()) && n.getPort() < 0) {

                // scheme empty, userinfo is not empty, path/query/port are empty
                // set to "acct" (rule 2)
                builder.scheme("acct");

            } else {
                // scheme is empty, but rule 2 doesn't apply
                // set scheme to "https" (rule 3)
                builder.scheme("https");
            }
        }

        // fragment must be stripped (rule 4)
        builder.fragment(null);

        return builder.build();
    }

}

From source file:org.wallride.web.controller.admin.customfield.CustomFieldSearchController.java

@RequestMapping(method = RequestMethod.GET)
public String search(@PathVariable String language,
        @Validated @ModelAttribute("form") CustomFieldSearchForm form, BindingResult result,
        @PageableDefault(50) Pageable pageable, Model model, HttpServletRequest servletRequest)
        throws UnsupportedEncodingException {
    Page<CustomField> customFields = customfieldService.getCustomFields(form.toCustomFieldSearchRequest(),
            pageable);/* w  ww. j  a va  2  s . c  om*/

    model.addAttribute("customFields", customFields);
    model.addAttribute("pageable", pageable);
    model.addAttribute("pagination", new Pagination<>(customFields, servletRequest));

    UriComponents uriComponents = ServletUriComponentsBuilder.fromRequest(servletRequest)
            .queryParams(ControllerUtils.convertBeanForQueryParams(form, conversionService)).build();
    if (!StringUtils.isEmpty(uriComponents.getQuery())) {
        model.addAttribute("query", URLDecoder.decode(uriComponents.getQuery(), "UTF-8"));
    }

    return "customfield/index";
}

From source file:org.wallride.web.controller.admin.user.UserSearchController.java

@RequestMapping(method = RequestMethod.GET)
public String search(@PathVariable String language, @Validated @ModelAttribute("form") UserSearchForm form,
        BindingResult result, @PageableDefault(50) Pageable pageable, Model model,
        HttpServletRequest servletRequest) throws UnsupportedEncodingException {
    Page<User> users = userService.getUsers(form.toUserSearchRequest(), pageable);

    model.addAttribute("users", users);
    model.addAttribute("pageable", pageable);
    model.addAttribute("pagination", new Pagination<>(users, servletRequest));

    UriComponents uriComponents = ServletUriComponentsBuilder.fromRequest(servletRequest)
            .queryParams(ControllerUtils.convertBeanForQueryParams(form, conversionService)).build();
    if (!StringUtils.isEmpty(uriComponents.getQuery())) {
        model.addAttribute("query", URLDecoder.decode(uriComponents.getQuery(), "UTF-8"));
    }//from   w w w. j av  a 2  s .c o m

    return "user/index";
}

From source file:org.wallride.web.controller.admin.comment.CommentSearchController.java

@RequestMapping(method = RequestMethod.GET)
public String search(@PathVariable String language, @Validated CommentSearchForm form, BindingResult result,
        @PageableDefault(value = 50, sort = "date", direction = Sort.Direction.DESC) Pageable pageable,
        Model model, HttpServletRequest servletRequest) throws UnsupportedEncodingException {
    Page<Comment> comments = commentService.getComments(form.toCommentSearchRequest(), pageable);

    model.addAttribute("form", form);
    model.addAttribute("comments", comments);
    model.addAttribute("pageable", pageable);
    model.addAttribute("pagination", new org.wallride.web.support.Pagination<>(comments, servletRequest));

    UriComponents uriComponents = ServletUriComponentsBuilder.fromRequest(servletRequest)
            .queryParams(ControllerUtils.convertBeanForQueryParams(form, conversionService)).build();
    if (!StringUtils.isEmpty(uriComponents.getQuery())) {
        model.addAttribute("query", URLDecoder.decode(uriComponents.getQuery(), "UTF-8"));
    }//from  ww  w .  j  a  v a  2s. c  o  m

    return "comment/index";
}

From source file:org.wallride.web.controller.admin.tag.TagSearchController.java

@RequestMapping
public String search(@PathVariable String language, @Validated @ModelAttribute("form") TagSearchForm form,
        BindingResult errors, @PageableDefault(50) Pageable pageable, Model model,
        HttpServletRequest servletRequest) throws UnsupportedEncodingException {
    Page<Tag> tags = tagService.getTags(form.toTagSearchRequest(), pageable);

    model.addAttribute("tags", tags);
    model.addAttribute("pageable", pageable);
    model.addAttribute("pagination", new Pagination<>(tags, servletRequest));

    UriComponents uriComponents = ServletUriComponentsBuilder.fromRequest(servletRequest)
            .queryParams(ControllerUtils.convertBeanForQueryParams(form, conversionService)).build();
    if (!StringUtils.isEmpty(uriComponents.getQuery())) {
        model.addAttribute("query", URLDecoder.decode(uriComponents.getQuery(), "UTF-8"));
    }/*  w  ww .  j  a va  2 s  . c  o  m*/

    return "tag/index";
}

From source file:org.wallride.web.controller.admin.article.ArticleSearchController.java

@RequestMapping(method = RequestMethod.GET)
public String search(@PathVariable String language, @Validated @ModelAttribute("form") ArticleSearchForm form,
        BindingResult result, @PageableDefault(50) Pageable pageable, Model model,
        HttpServletRequest servletRequest) throws UnsupportedEncodingException {
    Page<Article> articles = articleService.getArticles(form.toArticleSearchRequest(), pageable);

    model.addAttribute("articles", articles);
    model.addAttribute("pageable", pageable);
    model.addAttribute("pagination", new Pagination<>(articles, servletRequest));

    UriComponents uriComponents = ServletUriComponentsBuilder.fromRequest(servletRequest)
            .queryParams(ControllerUtils.convertBeanForQueryParams(form, conversionService)).build();
    if (!StringUtils.isEmpty(uriComponents.getQuery())) {
        model.addAttribute("query", URLDecoder.decode(uriComponents.getQuery(), "UTF-8"));
    }/*ww w  .  j  a  va2s. c  o  m*/

    return "article/index";
}

From source file:org.wallride.web.controller.admin.page.PageSearchController.java

@RequestMapping(method = RequestMethod.GET)
public String search(@PathVariable String language, @Validated @ModelAttribute("form") PageSearchForm form,
        BindingResult result, @PageableDefault(50) Pageable pageable, Model model,
        HttpServletRequest servletRequest) throws UnsupportedEncodingException {
    org.springframework.data.domain.Page<Page> pages = pageService.getPages(form.toPageSearchRequest(),
            pageable);//from   ww w . j  a v a  2  s  .  co m

    model.addAttribute("pages", pages);
    model.addAttribute("pageable", pageable);
    model.addAttribute("pagination", new Pagination<>(pages, servletRequest));

    UriComponents uriComponents = ServletUriComponentsBuilder.fromRequest(servletRequest)
            .queryParams(ControllerUtils.convertBeanForQueryParams(form, conversionService)).build();
    if (!StringUtils.isEmpty(uriComponents.getQuery())) {
        model.addAttribute("query", URLDecoder.decode(uriComponents.getQuery(), "UTF-8"));
    }

    return "page/index";
}

From source file:org.meruvian.yama.webapp.interceptor.OAuth2ClientContextInterceptor.java

/**
 * Calculate the current URI given the request.
 * /*  w  w  w .ja v a2 s .c  o m*/
 * @param request The request.
 * @return The current uri.
 */
protected String calculateCurrentUri(HttpServletRequest request) throws UnsupportedEncodingException {
    ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequest(request);
    // Now work around SPR-10172...
    String queryString = request.getQueryString();
    boolean legalSpaces = queryString != null && queryString.contains("+");
    if (legalSpaces) {
        builder.replaceQuery(queryString.replace("+", "%20"));
    }
    UriComponents uri = null;
    try {
        uri = builder.replaceQueryParam("code").build(true);
    } catch (IllegalArgumentException ex) {
        // ignore failures to parse the url (including query string). does't make sense
        // for redirection purposes anyway.
        return null;
    }
    String query = uri.getQuery();
    if (legalSpaces) {
        query = query.replace("%20", "+");
    }
    return ServletUriComponentsBuilder.fromUri(uri.toUri()).replaceQuery(query).build().toString();
}

From source file:org.mitre.discovery.util.WebfingerURLNormalizer.java

public static String serializeURL(UriComponents uri) {
    if (uri.getScheme() != null && (uri.getScheme().equals("acct") || uri.getScheme().equals("mailto")
            || uri.getScheme().equals("tel") || uri.getScheme().equals("device"))) {

        // serializer copied from HierarchicalUriComponents but with "//" removed

        StringBuilder uriBuilder = new StringBuilder();

        if (uri.getScheme() != null) {
            uriBuilder.append(uri.getScheme());
            uriBuilder.append(':');
        }/*  w w  w  .j a v a  2s.  c  o  m*/

        if (uri.getUserInfo() != null || uri.getHost() != null) {
            if (uri.getUserInfo() != null) {
                uriBuilder.append(uri.getUserInfo());
                uriBuilder.append('@');
            }
            if (uri.getHost() != null) {
                uriBuilder.append(uri.getHost());
            }
            if (uri.getPort() != -1) {
                uriBuilder.append(':');
                uriBuilder.append(uri.getPort());
            }
        }

        String path = uri.getPath();
        if (StringUtils.hasLength(path)) {
            if (uriBuilder.length() != 0 && path.charAt(0) != '/') {
                uriBuilder.append('/');
            }
            uriBuilder.append(path);
        }

        String query = uri.getQuery();
        if (query != null) {
            uriBuilder.append('?');
            uriBuilder.append(query);
        }

        if (uri.getFragment() != null) {
            uriBuilder.append('#');
            uriBuilder.append(uri.getFragment());
        }

        return uriBuilder.toString();
    } else {
        return uri.toUriString();
    }

}

From source file:org.wallride.job.UpdatePostViewsItemWriter.java

@Override
public void write(List<? extends List> items) throws Exception {
    WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext,
            "org.springframework.web.servlet.FrameworkServlet.CONTEXT.guestServlet");
    if (context == null) {
        throw new ServiceException("GuestServlet is not ready yet");
    }/*  w  w w .  j  av a2s.  c  o  m*/

    final RequestMappingHandlerMapping mapping = context.getBean(RequestMappingHandlerMapping.class);

    for (List item : items) {
        UriComponents uriComponents = UriComponentsBuilder.fromUriString((String) item.get(0)).build();
        logger.info("Processing [{}]", uriComponents.toString());

        MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
        request.setMethod("GET");
        request.setRequestURI(uriComponents.getPath());
        request.setQueryString(uriComponents.getQuery());
        MockHttpServletResponse response = new MockHttpServletResponse();

        BlogLanguageRewriteRule rewriteRule = new BlogLanguageRewriteRule(blogService);
        BlogLanguageRewriteMatch rewriteMatch = (BlogLanguageRewriteMatch) rewriteRule.matches(request,
                response);
        try {
            rewriteMatch.execute(request, response);
        } catch (ServletException e) {
            throw new ServiceException(e);
        } catch (IOException e) {
            throw new ServiceException(e);
        }

        request.setRequestURI(rewriteMatch.getMatchingUrl());

        HandlerExecutionChain handler;
        try {
            handler = mapping.getHandler(request);
        } catch (Exception e) {
            throw new ServiceException(e);
        }

        if (!(handler.getHandler() instanceof HandlerMethod)) {
            continue;
        }

        HandlerMethod method = (HandlerMethod) handler.getHandler();
        if (!method.getBeanType().equals(ArticleDescribeController.class)
                && !method.getBeanType().equals(PageDescribeController.class)) {
            continue;
        }

        // Last path mean code of post
        String code = uriComponents.getPathSegments().get(uriComponents.getPathSegments().size() - 1);
        Post post = postRepository.findOneByCodeAndLanguage(code, rewriteMatch.getBlogLanguage().getLanguage());
        if (post == null) {
            logger.debug("Post not found [{}]", code);
            continue;
        }

        logger.info("Update the PageView. Post ID [{}]: {} -> {}", post.getId(), post.getViews(), item.get(1));
        post.setViews(Long.parseLong((String) item.get(1)));
        postRepository.saveAndFlush(post);
    }
}