Example usage for org.springframework.web.context.request NativeWebRequest getNativeRequest

List of usage examples for org.springframework.web.context.request NativeWebRequest getNativeRequest

Introduction

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

Prototype

@Nullable
<T> T getNativeRequest(@Nullable Class<T> requiredType);

Source Link

Document

Return the underlying native request object, if available.

Usage

From source file:org.infoscoop.api.oauth2.provider.ISOAuth2ExceptionRenderer.java

private HttpInputMessage createHttpInputMessage(NativeWebRequest webRequest) throws Exception {
    HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    return new ServletServerHttpRequest(servletRequest);
}

From source file:org.qcri.micromappers.config.social.CustomConnectController.java

/**
 * Returns a RedirectView with the URL to redirect to after a connection is created or deleted.
 * Defaults to "/connect/{providerId}" relative to DispatcherServlet's path. 
 * May be overridden to handle custom redirection needs.
 * @param providerId the ID of the provider for which a connection was created or deleted.
 * @param request the NativeWebRequest used to access the servlet path when constructing the redirect path.
 * @return a RedirectView to the page to be displayed after a connection is created or deleted
 *///w w  w.  j  a va  2 s  .c om
protected RedirectView connectionStatusRedirect(String providerId, NativeWebRequest request,
        boolean isDuplicate) {
    HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
    String path = "/connect/" + providerId + getPathForDuplicate(isDuplicate)
            + getPathExtension(servletRequest);
    if (prependServletPath(servletRequest)) {
        path = servletRequest.getServletPath() + path;
    }
    return new RedirectView(path, true);
}

From source file:org.simbasecurity.manager.service.rest.resolver.JSonArgumentResolver.java

private String getRequestBody(NativeWebRequest webRequest) {
    HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    String jsonBody = (String) servletRequest.getAttribute(JSON_REQUEST_BODY);
    if (jsonBody == null) {
        try {//from  www.j a  va2 s .c o  m
            String body = IOUtils.toString(servletRequest.getInputStream(), Charset.defaultCharset());
            servletRequest.setAttribute(JSON_REQUEST_BODY, body);
            return body;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return jsonBody;
}

From source file:org.springframework.cloud.function.web.flux.request.FluxHandlerMethodArgumentResolver.java

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    Object handler = webRequest.getAttribute(WebRequestConstants.HANDLER, NativeWebRequest.SCOPE_REQUEST);
    Class<?> type = inspector.getInputType(inspector.getName(handler));
    if (type == null) {
        type = Object.class;
    }/*from w  w w.  j av  a 2 s .co m*/
    List<Object> body;
    ContentCachingRequestWrapper nativeRequest = new ContentCachingRequestWrapper(
            webRequest.getNativeRequest(HttpServletRequest.class));
    if (logger.isDebugEnabled()) {
        logger.debug("Resolving request body into type: " + type);
    }
    if (isPlainText(webRequest) && CharSequence.class.isAssignableFrom(type)) {
        body = Arrays
                .asList(StreamUtils.copyToString(nativeRequest.getInputStream(), Charset.forName("UTF-8")));
    } else {
        try {
            body = mapper.readValue(nativeRequest.getInputStream(),
                    mapper.getTypeFactory().constructCollectionLikeType(ArrayList.class, type));
        } catch (JsonMappingException e) {
            nativeRequest.setAttribute(WebRequestConstants.INPUT_SINGLE, true);
            body = Arrays.asList(mapper.readValue(nativeRequest.getContentAsByteArray(), type));
        }
    }
    return new FluxRequest<Object>(body);
}

From source file:org.springframework.data.document.web.bind.annotation.support.HandlerMethodInvoker.java

@SuppressWarnings("unchecked")
private Object resolveRequestParam(String paramName, boolean required, String defaultValue,
        MethodParameter methodParam, NativeWebRequest webRequest, Object handlerForInitBinderCall)
        throws Exception {

    Class<?> paramType = methodParam.getParameterType();
    if (Map.class.isAssignableFrom(paramType) && paramName.length() == 0) {
        return resolveRequestParamMap((Class<? extends Map>) paramType, webRequest);
    }/* www .  ja va2s .  c om*/
    if (paramName.length() == 0) {
        paramName = getRequiredParameterName(methodParam);
    }
    Object paramValue = null;
    MultipartRequest multipartRequest = webRequest.getNativeRequest(MultipartRequest.class);
    if (multipartRequest != null) {
        List<MultipartFile> files = multipartRequest.getFiles(paramName);
        if (!files.isEmpty()) {
            if (files.size() == 1 && !paramType.isArray() && !Collection.class.isAssignableFrom(paramType)) {
                paramValue = files.get(0);
            } else {
                paramValue = files;
            }
        }
    }
    if (paramValue == null) {
        String[] paramValues = webRequest.getParameterValues(paramName);
        if (paramValues != null) {
            if (paramValues.length == 1 && !paramType.isArray()
                    && !Collection.class.isAssignableFrom(paramType)) {
                paramValue = paramValues[0];
            } else {
                paramValue = paramValues;
            }
        }
    }
    if (paramValue == null) {
        if (defaultValue != null) {
            paramValue = resolveDefaultValue(defaultValue);
        } else if (required) {
            raiseMissingParameterException(paramName, paramType);
        }
        paramValue = checkValue(paramName, paramValue, paramType);
    }
    WebDataBinder binder = createBinder(webRequest, null, paramName);
    initBinder(handlerForInitBinderCall, paramName, binder, webRequest);
    return binder.convertIfNecessary(paramValue, paramType, methodParam);
}

From source file:org.springframework.social.connect.web.ConnectController.java

/**
 * Returns a RedirectView with the URL to redirect to after a connection is created or deleted.
 * Defaults to "/connect/{providerId}" relative to DispatcherServlet's path. 
 * May be overridden to handle custom redirection needs.
 * @param providerId the ID of the provider for which a connection was created or deleted.
 * @param request the NativeWebRequest used to access the servlet path when constructing the redirect path.
 * @return a RedirectView to the page to be displayed after a connection is created or deleted
 *//*from   w ww  .j  a va  2  s  .  c  o m*/
protected RedirectView connectionStatusRedirect(String providerId, NativeWebRequest request) {
    HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
    String path = "/connect/" + providerId + getPathExtension(servletRequest);
    if (prependServletPath(servletRequest)) {
        path = servletRequest.getServletPath() + path;
    }
    return new RedirectView(path, true);
}

From source file:org.springframework.social.connect.web.ConnectSupport.java

protected String callbackUrl(NativeWebRequest request) {
    if (callbackUrl != null) {
        return callbackUrl;
    }/*from  ww  w  .j  a v  a 2 s .c o m*/
    HttpServletRequest nativeRequest = request.getNativeRequest(HttpServletRequest.class);
    if (applicationUrl != null) {
        return applicationUrl + connectPath(nativeRequest);
    } else {
        return nativeRequest.getRequestURL().toString();
    }
}

From source file:org.springframework.web.accept.PathExtensionContentNegotiationStrategy.java

@Override
protected String getMediaTypeKey(NativeWebRequest webRequest) {
    HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    if (servletRequest == null) {
        logger.warn("An HttpServletRequest is required to determine the media type key");
        return null;
    }//from  ww  w . ja  va  2  s .c o  m
    String path = urlPathHelper.getLookupPathForRequest(servletRequest);
    String filename = WebUtils.extractFullFilenameFromUrlPath(path);
    String extension = StringUtils.getFilenameExtension(filename);
    return (StringUtils.hasText(extension)) ? extension.toLowerCase(Locale.ENGLISH) : null;
}

From source file:org.springframework.web.bind.annotation.support.HandlerMethodInvoker.java

@SuppressWarnings("unchecked")
private Object resolveRequestParam(String paramName, boolean required, String defaultValue,
        MethodParameter methodParam, NativeWebRequest webRequest, Object handlerForInitBinderCall)
        throws Exception {

    Class<?> paramType = methodParam.getParameterType();
    if (Map.class.isAssignableFrom(paramType) && paramName.length() == 0) {
        return resolveRequestParamMap((Class<? extends Map<?, ?>>) paramType, webRequest);
    }/*from w w w .j  a v a 2  s.  co  m*/
    if (paramName.length() == 0) {
        paramName = getRequiredParameterName(methodParam);
    }
    Object paramValue = null;
    MultipartRequest multipartRequest = webRequest.getNativeRequest(MultipartRequest.class);
    if (multipartRequest != null) {
        List<MultipartFile> files = multipartRequest.getFiles(paramName);
        if (!files.isEmpty()) {
            paramValue = (files.size() == 1 ? files.get(0) : files);
        }
    }
    if (paramValue == null) {
        String[] paramValues = webRequest.getParameterValues(paramName);
        if (paramValues != null) {
            paramValue = (paramValues.length == 1 ? paramValues[0] : paramValues);
        }
    }
    if (paramValue == null) {
        if (defaultValue != null) {
            paramValue = resolveDefaultValue(defaultValue);
        } else if (required) {
            raiseMissingParameterException(paramName, paramType);
        }
        paramValue = checkValue(paramName, paramValue, paramType);
    }
    WebDataBinder binder = createBinder(webRequest, null, paramName);
    initBinder(handlerForInitBinderCall, paramName, binder, webRequest);
    return binder.convertIfNecessary(paramValue, paramType, methodParam);
}

From source file:org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.java

/**
 * Create a new {@link HttpInputMessage} from the given {@link NativeWebRequest}.
 * @param webRequest the web request to create an input message from
 * @return the input message/*from   w ww. j ava  2  s  .  co m*/
 */
protected ServletServerHttpRequest createInputMessage(NativeWebRequest webRequest) {
    HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    Assert.state(servletRequest != null, "No HttpServletRequest");
    return new ServletServerHttpRequest(servletRequest);
}