Example usage for org.springframework.web.servlet.mvc.method RequestMappingInfo getPatternsCondition

List of usage examples for org.springframework.web.servlet.mvc.method RequestMappingInfo getPatternsCondition

Introduction

In this page you can find the example usage for org.springframework.web.servlet.mvc.method RequestMappingInfo getPatternsCondition.

Prototype

public PatternsRequestCondition getPatternsCondition() 

Source Link

Document

Return the URL patterns of this RequestMappingInfo ; or instance with 0 patterns (never null ).

Usage

From source file:de.codecentric.boot.admin.web.PrefixHandlerMapping.java

private List<String> getPatterns(RequestMappingInfo mapping) {
    List<String> newPatterns = new ArrayList<String>(mapping.getPatternsCondition().getPatterns().size());
    for (String pattern : mapping.getPatternsCondition().getPatterns()) {
        newPatterns.add(prefix + pattern);
    }//w  w  w .j  av a 2  s . c o  m
    return newPatterns;
}

From source file:com.orange.clara.cloud.servicedbdumper.interceptor.AddAdminUrlsInterceptor.java

private void loadMappedRequestFromRequestMappingInfoSet(Set<RequestMappingInfo> requestMappingInfoSet) {
    for (RequestMappingInfo requestMappingInfo : requestMappingInfoSet) {
        String patternUrl = this.stringifyPatternsCondition(requestMappingInfo.getPatternsCondition());
        if (patternUrl.contains("{") || patternUrl.contains("}") || !patternUrl.startsWith(DEFAULT_ADMIN_URL)
                || patternUrl.equals(DEFAULT_ADMIN_URL)) {
            continue;
        }/*  w w w  .  j a  v  a2 s . co  m*/
        String name = patternUrl.replace(DEFAULT_ADMIN_URL + "/", "");
        name = name.replace("/", "-");
        MappedRequestInfo mappedRequestInfo = new MappedRequestInfo(name, patternUrl);
        if (mappedRequests.contains(mappedRequestInfo) || mappedRequestInfo.getName().equals("welcome")) {
            continue;
        }
        mappedRequests.add(mappedRequestInfo);
    }
}

From source file:springfox.documentation.spring.web.scanners.ApiDescriptionReader.java

public List<ApiDescription> read(RequestMappingContext outerContext) {
    RequestMappingInfo requestMappingInfo = outerContext.getRequestMappingInfo();
    HandlerMethod handlerMethod = outerContext.getHandlerMethod();
    PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
    ApiSelector selector = outerContext.getDocumentationContext().getApiSelector();

    List<ApiDescription> apiDescriptionList = newArrayList();
    for (String path : matchingPaths(selector, patternsCondition)) {
        String methodName = handlerMethod.getMethod().getName();
        RequestMappingContext operationContext = outerContext.copyPatternUsing(path);

        List<Operation> operations = operationReader.read(operationContext);
        if (operations.size() > 0) {
            operationContext.apiDescriptionBuilder().operations(operations)
                    .pathDecorator(/*from ww  w  . ja  v  a 2  s.co m*/
                            pluginsManager.decorator(new PathContext(outerContext, from(operations).first())))
                    .path(path).description(methodName).hidden(false);
            ApiDescription apiDescription = operationContext.apiDescriptionBuilder().build();
            lookup.add(outerContext.getHandlerMethod().getMethod(), apiDescription);
            apiDescriptionList.add(apiDescription);
        }
    }
    return apiDescriptionList;
}

From source file:de.appsolve.padelcampus.admin.controller.general.AdminGeneralModulesController.java

private void checkTitleRequirements(Module module, BindingResult result) {
    switch (module.getModuleType()) {
    case Blog:/* w  w  w .ja v a2  s .c  om*/
    case Page:
    case Events:

        //make sure title does not conflict with existing request mappings
        for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMapping.getHandlerMethods()
                .entrySet()) {
            RequestMappingInfo requestMappingInfo = entry.getKey();
            List<String> matchingPatterns = requestMappingInfo.getPatternsCondition()
                    .getMatchingPatterns("/" + module.getUrlTitle());
            for (String pattern : matchingPatterns) {
                if (!pattern.equals("/{moduleId}")) {
                    result.rejectValue("title", "ModuleWithUrlTitleAlreadyExists",
                            new Object[] { module.getUrlTitle() }, "ModuleWithUrlTitleAlreadyExists");
                    return;
                }
            }
        }

        Module existingModule = moduleDAO.findByUrlTitle(module.getUrlTitle());
        if (existingModule != null && !existingModule.equals(module)) {
            result.rejectValue("title", "ModuleWithUrlTitleAlreadyExists",
                    new Object[] { module.getUrlTitle() }, "ModuleWithUrlTitleAlreadyExists");
        }
        break;
    }
}

From source file:org.jm.swagger.SpringMVCAPIReader.java

/**
 * This method should create a {@link Documentation} object that should represent a similiar object to 
 * http://petstore.swagger.wordnik.com/api/pet.json
 * //from   w  ww  .  j ava 2 s .  com
 * 
 * @param mapping
 * @param classRequestMapping
 * @return
 */
public Documentation processMethods(RequestMappingHandlerMapping mapping, String classRequestMapping) {

    Documentation documentation = new Documentation(this.apiVersion, this.swaggerVersion, this.basePath,
            this.resourcePath);

    for (Entry<RequestMappingInfo, HandlerMethod> entry : mapping.getHandlerMethods().entrySet()) {

        RequestMappingInfo requestMappingInfo = entry.getKey();
        HandlerMethod handlerMethod = entry.getValue();

        // find bean that has a matching classRequestMapping value
        RequestMapping requestMapping = handlerMethod.getBeanType().getAnnotation(RequestMapping.class);
        if (requestMapping.value()[0].contains(classRequestMapping)) {

            String path = null;
            for (String pt : requestMappingInfo.getPatternsCondition().getPatterns()) {
                path = pt;
            }

            //TODO - how to get description for the method
            DocumentationEndPoint documentationEndPoint = new DocumentationEndPoint(path + "{format}",
                    "description");

            DocumentationOperation documentationOperation = processRequestMapping(requestMappingInfo,
                    handlerMethod.getMethod().getName());
            List<DocumentationParameter> documentationParamaters = convertHandlerMethod(handlerMethod);
            documentationOperation.setParameters(documentationParamaters);

            documentationEndPoint.addOperation(documentationOperation);
            documentation.addApi(documentationEndPoint);
        }
    }

    return documentation;
}

From source file:org.springbyexample.mvc.method.annotation.ServiceHandlerMapping.java

/**
 * Register handler./* ww  w  . j a va 2 s .c  o m*/
 */
private void registerHandler(Class<?> marshallingServiceClass, Method method) {
    ApplicationContext ctx = getApplicationContext();

    RestResource restResource = AnnotationUtils.findAnnotation(marshallingServiceClass, RestResource.class);
    Class<?> serviceClass = restResource.service();

    RestRequestResource restRequestResource = AnnotationUtils.findAnnotation(method, RestRequestResource.class);
    boolean export = (restRequestResource != null ? restRequestResource.export() : true);
    boolean relative = (restRequestResource != null ? restRequestResource.relative() : true);

    if (export) {
        Class<?> handlerServiceClass = serviceClass;
        String methodName = method.getName();
        Class<?>[] paramTypes = method.getParameterTypes();

        if (restRequestResource != null) {
            // explicit service specified
            if (restRequestResource.service() != ServiceValueConstants.DEFAULT_SERVICE_CLASS) {
                handlerServiceClass = restRequestResource.service();
            }

            // explicit method name specified
            if (StringUtils.hasText(restRequestResource.methodName())) {
                methodName = restRequestResource.methodName();
            }
        }

        Object handler = ctx.getBean(handlerServiceClass);
        Method serviceMethod = ClassUtils.getMethod(handlerServiceClass, methodName, paramTypes);
        RequestMappingInfo mapping = getMappingForMethod(method, marshallingServiceClass);

        if (relative) {
            List<String> patterns = new ArrayList<String>();

            for (String pattern : mapping.getPatternsCondition().getPatterns()) {
                // add REST resource path prefix to URI,
                // if relative path is just '/' add an empty string
                patterns.add(restResource.path() + (!"/".equals(pattern) ? pattern : ""));
            }

            // create a new mapping based on the patterns (patterns are unmodifiable in existing RequestMappingInfo)
            mapping = new RequestMappingInfo(
                    new PatternsRequestCondition(patterns.toArray(ArrayUtils.EMPTY_STRING_ARRAY),
                            getUrlPathHelper(), getPathMatcher(), useSuffixPatternMatch(),
                            useTrailingSlashMatch()),
                    mapping.getMethodsCondition(), mapping.getParamsCondition(), mapping.getHeadersCondition(),
                    mapping.getConsumesCondition(), mapping.getProducesCondition(),
                    mapping.getCustomCondition());
        }

        // need to set param types to use in createHandlerMethod before calling registerHandlerMethod
        restBridgedMethod = BridgeMethodResolver.findBridgedMethod(method);

        // mapping is key, HandlerMethod is value
        registerHandlerMethod(handler, serviceMethod, mapping);

        processConverters(restRequestResource, mapping, serviceMethod);
    }
}

From source file:org.springbyexample.mvc.method.annotation.ServiceHandlerMapping.java

/**
 * Process and setup any converter handlers if one is configured on <code>RestRequestResource</code>.
 *///from   w  w  w  .j  a va2s  . c  o  m
private void processConverters(RestRequestResource restRequestResource, RequestMappingInfo mapping,
        Method serviceMethod) {
    ApplicationContext ctx = getApplicationContext();
    Class<?> converterClass = (restRequestResource != null ? restRequestResource.converter() : null);

    if (converterClass != null && converterClass != ServiceValueConstants.DEFAULT_CONVERTER_CLASS) {
        @SuppressWarnings("rawtypes")
        ListConverter converter = (ListConverter) ctx.getBean(converterClass);

        String[] pathPatterns = mapping.getPatternsCondition().getPatterns()
                .toArray(ArrayUtils.EMPTY_STRING_ARRAY);

        String methodSuffix = StringUtils.capitalize(converterHandlerInfo.getPropertyName());
        String getterMethodName = "get" + methodSuffix;
        final String setterMethodName = "set" + methodSuffix;

        final Class<?> returnTypeClass = serviceMethod.getReturnType();
        Method getResultsMethod = ReflectionUtils.findMethod(returnTypeClass, getterMethodName);
        final Class<?> resultReturnTypeClass = getResultsMethod.getReturnType();
        Method setResultsMethod = ReflectionUtils.findMethod(returnTypeClass, setterMethodName,
                resultReturnTypeClass);
        final AtomicReference<Method> altSetResultsMethod = new AtomicReference<Method>();

        // issue with ReflectionUtils, setterResultsMethod sometimes null from the command line (not getter?)
        if (setResultsMethod == null) {
            ReflectionUtils.doWithMethods(returnTypeClass, new MethodCallback() {
                @Override
                public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                    if (setterMethodName.equals(method.getName())) {
                        altSetResultsMethod.set(method);
                        logger.debug(
                                "Unable to use ReflectionUtils to find setter. returnTypeClass={}  method={} resultReturnTypeClass={}",
                                new Object[] { returnTypeClass, method, resultReturnTypeClass });
                    }
                }
            });
        }

        HandlerInterceptor interceptor = new ConverterHandlerInterceptor(converter, returnTypeClass,
                getResultsMethod, (setResultsMethod != null ? setResultsMethod : altSetResultsMethod.get()));

        MappedInterceptor mappedInterceptor = new MappedInterceptor(pathPatterns, interceptor);
        setInterceptors(new Object[] { mappedInterceptor });

        logger.info("Registered converter post handler for {} with {}.", pathPatterns,
                converterClass.getCanonicalName());
    }
}