Search in sources :

Example 1 with NoSuchRequestHandlingMethodException

use of org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException in project ORCID-Source by ORCID.

the class ClaimControllerTest method testClaim.

@Test
@Transactional
public void testClaim() {
    String email = "public_0000-0000-0000-0001@test.orcid.org";
    SecurityContextHolder.getContext().setAuthentication(null);
    when(profileEntityCacheManager.retrieve(Mockito.anyString())).thenReturn(getProfileEntityToTestClam(false));
    when(encryptionManager.decryptForExternalUse(any(String.class))).thenReturn(email);
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE)).thenReturn(null);
    when(request.getLocale()).thenReturn(java.util.Locale.US);
    String orcid = "0000-0000-0000-0001";
    when(emailManager.findOrcidIdByEmail(email)).thenReturn(orcid);
    when(profileEntityManager.claimProfileAndUpdatePreferences(any(String.class), any(String.class), any(Locale.class), any(Claim.class))).thenReturn(true);
    Claim claim = new Claim();
    claim.setActivitiesVisibilityDefault(org.orcid.pojo.ajaxForm.Visibility.valueOf(Visibility.PRIVATE));
    claim.setPassword(Text.valueOf("passwordTest1"));
    claim.setPasswordConfirm(Text.valueOf("passwordTest1"));
    Checkbox checked = new Checkbox();
    checked.setValue(true);
    claim.setSendChangeNotifications(checked);
    claim.setSendOrcidNews(checked);
    claim.setTermsOfUse(checked);
    try {
        claim = claimController.submitClaimJson(request, response, email, claim);
        assertNotNull(claim);
        assertTrue(claim.getErrors().isEmpty());
        assertTrue("Value was: " + claim.getUrl(), claim.getUrl().endsWith("/my-orcid?recordClaimed"));
    } catch (NoSuchRequestHandlingMethodException e) {
        fail();
    } catch (UnsupportedEncodingException e) {
        fail();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Locale(org.orcid.jaxb.model.v3.dev1.common.Locale) NoSuchRequestHandlingMethodException(org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException) Checkbox(org.orcid.pojo.ajaxForm.Checkbox) HttpServletResponse(javax.servlet.http.HttpServletResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Claim(org.orcid.pojo.ajaxForm.Claim) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with NoSuchRequestHandlingMethodException

use of org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException in project com.revolsys.open by revolsys.

the class AnnotationHandlerMethodResolver method resolveHandlerMethod.

public WebMethodHandler resolveHandlerMethod(final HttpServletRequest request) throws ServletException {
    final String lookupPath = this.adapter.urlPathHelper.getLookupPathForRequest(request);
    final Comparator<String> pathComparator = this.adapter.pathMatcher.getPatternComparator(lookupPath);
    final Map<RequestMappingInfo, WebMethodHandler> targetHandlerMethods = new LinkedHashMap<>();
    final Set<String> allowedMethods = new LinkedHashSet<>(7);
    String resolvedMethodName = null;
    for (final WebMethodHandler webMethodHandler : this.handlerMethods) {
        final Method handlerMethod = webMethodHandler.getMethod();
        final RequestMappingInfo mappingInfo = new RequestMappingInfo();
        final RequestMapping mapping = AnnotationUtils.findAnnotation(handlerMethod, RequestMapping.class);
        mappingInfo.paths = mapping.value();
        if (!hasTypeLevelMapping() || !Arrays.equals(mapping.method(), this.typeLevelMapping.method())) {
            mappingInfo.methods = mapping.method();
        }
        boolean match = false;
        if (mappingInfo.paths.length > 0) {
            final List<String> matchedPaths = new ArrayList<>(mappingInfo.paths.length);
            for (final String methodLevelPattern : mappingInfo.paths) {
                final String matchedPattern = getMatchedPattern(methodLevelPattern, lookupPath, request);
                if (matchedPattern != null) {
                    if (mappingInfo.matches(request)) {
                        match = true;
                        matchedPaths.add(matchedPattern);
                    } else {
                        for (final RequestMethod requestMethod : mappingInfo.methods) {
                            allowedMethods.add(requestMethod.toString());
                        }
                        break;
                    }
                }
            }
            Collections.sort(matchedPaths, pathComparator);
            mappingInfo.matchedPaths = matchedPaths;
        } else {
            // No paths specified: parameter match sufficient.
            match = mappingInfo.matches(request);
            if (match && mappingInfo.methods.length == 0 && resolvedMethodName != null && !resolvedMethodName.equals(handlerMethod.getName())) {
                match = false;
            } else {
                for (final RequestMethod requestMethod : mappingInfo.methods) {
                    allowedMethods.add(requestMethod.toString());
                }
            }
        }
        if (match) {
            WebMethodHandler oldMappedMethod = targetHandlerMethods.put(mappingInfo, webMethodHandler);
            if (oldMappedMethod != null && oldMappedMethod != webMethodHandler) {
                if (this.adapter.methodNameResolver != null && mappingInfo.paths.length == 0) {
                    if (!oldMappedMethod.getMethod().getName().equals(handlerMethod.getName())) {
                        if (resolvedMethodName == null) {
                            resolvedMethodName = this.adapter.methodNameResolver.getHandlerMethodName(request);
                        }
                        if (!resolvedMethodName.equals(oldMappedMethod.getMethod().getName())) {
                            oldMappedMethod = null;
                        }
                        if (!resolvedMethodName.equals(handlerMethod.getName())) {
                            if (oldMappedMethod != null) {
                                targetHandlerMethods.put(mappingInfo, oldMappedMethod);
                                oldMappedMethod = null;
                            } else {
                                targetHandlerMethods.remove(mappingInfo);
                            }
                        }
                    }
                }
                if (oldMappedMethod != null) {
                    throw new IllegalStateException("Ambiguous handler methods mapped for HTTP path '" + lookupPath + "': {" + oldMappedMethod + ", " + handlerMethod + "}. If you intend to handle the same path in multiple methods, then factor " + "them out into a dedicated handler class with that path mapped at the type level!");
                }
            }
        }
    }
    if (!targetHandlerMethods.isEmpty()) {
        final List<RequestMappingInfo> matches = new ArrayList<>(targetHandlerMethods.keySet());
        final RequestMappingInfoComparator requestMappingInfoComparator = new RequestMappingInfoComparator(pathComparator);
        Collections.sort(matches, requestMappingInfoComparator);
        final RequestMappingInfo bestMappingMatch = matches.get(0);
        final String bestMatchedPath = bestMappingMatch.bestMatchedPath();
        if (bestMatchedPath != null) {
            extractHandlerMethodUriTemplates(bestMatchedPath, lookupPath, request);
        }
        return targetHandlerMethods.get(bestMappingMatch);
    } else {
        if (!allowedMethods.isEmpty()) {
            throw new HttpRequestMethodNotSupportedException(request.getMethod(), StringUtils.toStringArray(allowedMethods));
        } else {
            throw new NoSuchRequestHandlingMethodException(lookupPath, request.getMethod(), request.getParameterMap());
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NoSuchRequestHandlingMethodException(org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) LinkedHashMap(java.util.LinkedHashMap) RequestMapping(com.revolsys.ui.web.annotation.RequestMapping)

Aggregations

NoSuchRequestHandlingMethodException (org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException)2 RequestMapping (com.revolsys.ui.web.annotation.RequestMapping)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Test (org.junit.Test)1 Locale (org.orcid.jaxb.model.v3.dev1.common.Locale)1 Checkbox (org.orcid.pojo.ajaxForm.Checkbox)1 Claim (org.orcid.pojo.ajaxForm.Claim)1 Transactional (org.springframework.transaction.annotation.Transactional)1 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)1 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)1