Search in sources :

Example 21 with UrlPathHelper

use of org.springframework.web.util.UrlPathHelper in project ma-core-public by infiniteautomation.

the class MangoRestSpringConfiguration method getUrlPathHelper.

/**
 * Create a Path helper that will not URL Decode
 * the context path and request URI but will
 * decode the path variables...
 */
public UrlPathHelper getUrlPathHelper() {
    UrlPathHelper helper = new UrlPathHelper();
    helper.setUrlDecode(false);
    return helper;
}
Also used : UrlPathHelper(org.springframework.web.util.UrlPathHelper)

Example 22 with UrlPathHelper

use of org.springframework.web.util.UrlPathHelper in project BroadleafCommerce by BroadleafCommerce.

the class AdminBasicEntityController method viewEntityForm.

/**
 * Renders the main entity form for the specified entity
 *
 * @param request
 * @param response
 * @param model
 * @param pathVars
 * @param id
 * @return the return view path
 * @throws Exception
 */
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String viewEntityForm(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable("id") String id) throws Exception {
    String sectionKey = getSectionKey(pathVars);
    String sectionClassName = getClassNameForSection(sectionKey);
    List<SectionCrumb> crumbs = getSectionCrumbs(request, sectionKey, id);
    PersistencePackageRequest ppr = getSectionPersistencePackageRequest(sectionClassName, crumbs, pathVars);
    ClassMetadata cmd = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
    Entity entity = service.getRecord(ppr, id, cmd, false).getDynamicResultSet().getRecords()[0];
    Map<String, DynamicResultSet> subRecordsMap = getViewSubRecords(request, pathVars, cmd, entity, crumbs);
    EntityForm entityForm = formService.createEntityForm(cmd, entity, subRecordsMap, crumbs);
    if (isAddRequest(entity)) {
        modifyAddEntityForm(entityForm, pathVars);
    } else {
        modifyEntityForm(entityForm, pathVars);
    }
    if (request.getParameter("headerFlash") != null) {
        model.addAttribute("headerFlash", request.getParameter("headerFlash"));
    }
    // Set the sectionKey again incase this is a typed entity
    entityForm.setSectionKey(sectionKey);
    // Build the current url in the cast that this is a typed entity
    String originatingUri = new UrlPathHelper().getOriginatingRequestUri(request);
    int startIndex = request.getContextPath().length();
    // Remove the context path from servlet path
    String currentUrl = originatingUri.substring(startIndex);
    model.addAttribute("entity", entity);
    model.addAttribute("entityForm", entityForm);
    model.addAttribute("currentUrl", currentUrl);
    setModelAttributes(model, sectionKey);
    // We want to replace author ids with their names
    addAuditableDisplayFields(entityForm);
    if (isAjaxRequest(request)) {
        entityForm.setReadOnly();
        model.addAttribute("viewType", "modal/entityView");
        model.addAttribute("modalHeaderType", ModalHeaderType.VIEW_ENTITY.getType());
        return "modules/modalContainer";
    } else {
        model.addAttribute("useAjaxUpdate", true);
        model.addAttribute("viewType", "entityEdit");
        return "modules/defaultContainer";
    }
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) EntityForm(org.broadleafcommerce.openadmin.web.form.entity.EntityForm) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) UrlPathHelper(org.springframework.web.util.UrlPathHelper) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 23 with UrlPathHelper

use of org.springframework.web.util.UrlPathHelper in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method handleMatchUriTemplateVariablesDecode.

@SuppressWarnings("unchecked")
// SPR-9098
@PathPatternsParameterizedTest
void handleMatchUriTemplateVariablesDecode(TestRequestMappingInfoHandlerMapping mapping) {
    RequestMappingInfo key = RequestMappingInfo.paths("/{group}/{identifier}").build();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/group/a%2Fb");
    UrlPathHelper pathHelper = new UrlPathHelper();
    pathHelper.setUrlDecode(false);
    String lookupPath = pathHelper.getLookupPathForRequest(request);
    mapping.setUrlPathHelper(pathHelper);
    mapping.handleMatch(key, lookupPath, request);
    String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
    Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name);
    assertThat(uriVariables).isNotNull();
    assertThat(uriVariables.get("group")).isEqualTo("group");
    assertThat(uriVariables.get("identifier")).isEqualTo("a/b");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) UrlPathHelper(org.springframework.web.util.UrlPathHelper) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 24 with UrlPathHelper

use of org.springframework.web.util.UrlPathHelper in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method handleMatchMatrixVariablesDecoding.

// SPR-10140, SPR-16867
@PathPatternsParameterizedTest
void handleMatchMatrixVariablesDecoding(TestRequestMappingInfoHandlerMapping mapping) {
    if (mapping.getPatternParser() == null) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setUrlDecode(false);
        urlPathHelper.setRemoveSemicolonContent(false);
        mapping.setUrlPathHelper(urlPathHelper);
    }
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/cars;mvar=a%2Fb");
    handleMatch(mapping, request, "/{cars}", request.getRequestURI());
    MultiValueMap<String, String> matrixVariables = getMatrixVariables(request, "cars");
    Map<String, String> uriVariables = getUriTemplateVariables(request);
    assertThat(matrixVariables).isNotNull();
    assertThat(matrixVariables.get("mvar")).isEqualTo(Collections.singletonList("a/b"));
    assertThat(uriVariables.get("cars")).isEqualTo("cars");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) UrlPathHelper(org.springframework.web.util.UrlPathHelper) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 25 with UrlPathHelper

use of org.springframework.web.util.UrlPathHelper in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method handleMatchUriTemplateVariables.

@SuppressWarnings("unchecked")
@PathPatternsParameterizedTest
void handleMatchUriTemplateVariables(TestRequestMappingInfoHandlerMapping mapping) {
    RequestMappingInfo key = RequestMappingInfo.paths("/{path1}/{path2}").build();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
    String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
    mapping.handleMatch(key, lookupPath, request);
    String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
    Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name);
    assertThat(uriVariables).isNotNull();
    assertThat(uriVariables.get("path1")).isEqualTo("1");
    assertThat(uriVariables.get("path2")).isEqualTo("2");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) UrlPathHelper(org.springframework.web.util.UrlPathHelper) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Aggregations

UrlPathHelper (org.springframework.web.util.UrlPathHelper)32 Test (org.junit.jupiter.api.Test)10 PathMatcher (org.springframework.util.PathMatcher)5 Map (java.util.Map)4 Test (org.junit.Test)4 MultiValueMap (org.springframework.util.MultiValueMap)4 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)4 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)4 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)3 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)3 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)3 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)3 Collections (java.util.Collections)2 List (java.util.List)2 BiConsumer (java.util.function.BiConsumer)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 BDDMockito.given (org.mockito.BDDMockito.given)2