Search in sources :

Example 21 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project commons by terran4j.

the class Request method exe.

public Response exe() throws HttpException {
    // 获取实际的 URL。
    String actualURL = getActualURL();
    HttpRequest request = new HttpRequest(actualURL);
    // 获取实际的入参。
    final Map<String, String> actualParams = getActualParams();
    request.setParam(actualParams);
    RequestMethod method = RequestMethod.GET;
    String methodName = action.getMethod();
    if (StringUtils.hasText(methodName)) {
        method = RequestMethod.valueOf(methodName);
        if (method == null) {
            String msg = String.format("http method[%s] not supported in action: %s", methodName, action.getId());
            throw new UnsupportedOperationException(msg);
        }
    }
    request.setMethod(method);
    Map<String, String> actualHeaders = getActualHeaders();
    request.setHeaders(actualHeaders);
    List<HttpClientListener> listeners = action.getHttpClient().getListeners();
    for (HttpClientListener listener : listeners) {
        listener.beforeExecute(request);
    }
    String response = request.execute();
    for (HttpClientListener listener : listeners) {
        response = listener.afterExecute(request, response);
    }
    JsonElement result;
    try {
        result = parser.parse(response);
    } catch (JsonSyntaxException e) {
        // 不是 json 串,就按普通的字符串来处理。
        result = new JsonPrimitive(response);
    }
    List<Write> writes = action.getWrites();
    if (writes != null && writes.size() > 0) {
        JsonObject resultObject = null;
        if (result.isJsonObject()) {
            resultObject = result.getAsJsonObject();
        }
        context.push(new JsonValueSource(resultObject));
        for (Write write : writes) {
            write.doWrite(session, context);
        }
        context.pop();
    }
    return new Response(result, session);
}
Also used : RequestMethod(org.springframework.web.bind.annotation.RequestMethod) JsonValueSource(com.terran4j.commons.util.value.JsonValueSource)

Example 22 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerMapping method initCorsConfiguration.

@Override
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
    HandlerMethod handlerMethod = createHandlerMethod(handler, method);
    Class<?> beanType = handlerMethod.getBeanType();
    CrossOrigin typeAnnotation = AnnotatedElementUtils.findMergedAnnotation(beanType, CrossOrigin.class);
    CrossOrigin methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, CrossOrigin.class);
    if (typeAnnotation == null && methodAnnotation == null) {
        return null;
    }
    CorsConfiguration config = new CorsConfiguration();
    updateCorsConfig(config, typeAnnotation);
    updateCorsConfig(config, methodAnnotation);
    if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
        for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {
            config.addAllowedMethod(allowedMethod.name());
        }
    }
    return config.applyPermitDefaultValues();
}
Also used : CrossOrigin(org.springframework.web.bind.annotation.CrossOrigin) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 23 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerMapping method initCorsConfiguration.

@Override
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
    HandlerMethod handlerMethod = createHandlerMethod(handler, method);
    Class<?> beanType = handlerMethod.getBeanType();
    CrossOrigin typeAnnotation = AnnotatedElementUtils.findMergedAnnotation(beanType, CrossOrigin.class);
    CrossOrigin methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, CrossOrigin.class);
    if (typeAnnotation == null && methodAnnotation == null) {
        return null;
    }
    CorsConfiguration config = new CorsConfiguration();
    updateCorsConfig(config, typeAnnotation);
    updateCorsConfig(config, methodAnnotation);
    if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
        for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {
            config.addAllowedMethod(allowedMethod.name());
        }
    }
    return config.applyPermitDefaultValues();
}
Also used : CrossOrigin(org.springframework.web.bind.annotation.CrossOrigin) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 24 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerMappingTests method assertComposedAnnotationMapping.

private RequestMappingInfo assertComposedAnnotationMapping(String methodName, String path, RequestMethod requestMethod) throws Exception {
    Class<?> clazz = ComposedAnnotationController.class;
    Method method = ClassUtils.getMethod(clazz, methodName, (Class<?>[]) null);
    RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, clazz);
    assertThat(info).isNotNull();
    Set<PathPattern> paths = info.getPatternsCondition().getPatterns();
    assertThat(paths.size()).isEqualTo(1);
    assertThat(paths.iterator().next().getPatternString()).isEqualTo(path);
    Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
    assertThat(methods.size()).isEqualTo(1);
    assertThat(methods.iterator().next()).isEqualTo(requestMethod);
    return info;
}
Also used : PathPattern(org.springframework.web.util.pattern.PathPattern) RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Method(java.lang.reflect.Method) RequestMethod(org.springframework.web.bind.annotation.RequestMethod)

Example 25 with RequestMethod

use of org.springframework.web.bind.annotation.RequestMethod in project disconf by knightliao.

the class RoleResourceMgrImpl method checkUserPermission.

/**
 * @param pattern
 * @param method
 *
 * @return
 */
@Override
public boolean checkUserPermission(String pattern, RequestMethod method) {
    // 获取用户角色
    Visitor visitor = ThreadContext.getSessionVisitor();
    if (visitor == null) {
        return false;
    }
    String urlPattarn = pattern;
    if (!urlPattarn.endsWith(RoleResourceConstant.URL_SPLITOR)) {
        urlPattarn += RoleResourceConstant.URL_SPLITOR;
    }
    Integer roleId = visitor.getRoleId();
    Map<String, Map<RequestMethod, List<Integer>>> roleResMap = getAllAsMap();
    Map<RequestMethod, List<Integer>> methodMap = roleResMap.get(urlPattarn);
    if (methodMap == null) {
        return false;
    }
    List<Integer> roleIdList = methodMap.get(method);
    if (roleIdList == null) {
        return false;
    }
    if (!roleIdList.contains(roleId)) {
        return false;
    }
    return true;
}
Also used : Visitor(com.baidu.disconf.web.service.user.dto.Visitor) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RequestMethod (org.springframework.web.bind.annotation.RequestMethod)26 HandlerMethod (org.springframework.web.method.HandlerMethod)6 RequestMappingInfo (org.springframework.web.servlet.mvc.method.RequestMappingInfo)6 Method (java.lang.reflect.Method)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 List (java.util.List)3 Visitor (com.baidu.disconf.web.service.user.dto.Visitor)2 ApiParamObject (com.terran4j.commons.api2doc.domain.ApiParamObject)2 ValueSource (com.terran4j.commons.util.value.ValueSource)2 LinkedHashMap (java.util.LinkedHashMap)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 Test (org.junit.jupiter.api.Test)2 CrossOrigin (org.springframework.web.bind.annotation.CrossOrigin)2 CorsConfiguration (org.springframework.web.cors.CorsConfiguration)2 Application (cn.edu.zjnu.acm.judge.Application)1 RoleResource (com.baidu.disconf.web.service.roleres.bo.RoleResource)1 AccessDeniedException (com.baidu.dsp.common.exception.AccessDeniedException)1