Search in sources :

Example 81 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class BaseSiteHandler method doCreatePortalRequest.

protected PortalRequest doCreatePortalRequest(final WebRequest webRequest, final String baseUri, final String baseSubPath) {
    final RepositoryId repositoryId = findRepository(baseSubPath);
    final Branch branch = findBranch(baseSubPath);
    final ContentPath contentPath = findContentPath(baseSubPath);
    final PortalRequest portalRequest = new PortalRequest(webRequest);
    portalRequest.setBaseUri(baseUri);
    portalRequest.setRepositoryId(repositoryId);
    portalRequest.setBranch(branch);
    portalRequest.setContentPath(contentPath);
    return portalRequest;
}
Also used : Branch(com.enonic.xp.branch.Branch) ContentPath(com.enonic.xp.content.ContentPath) RepositoryId(com.enonic.xp.repository.RepositoryId) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 82 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class AbstractUrlParamsTest method setup.

@BeforeEach
public void setup() {
    this.portalRequest = new PortalRequest();
    this.portalRequest.setBranch(Branch.from("draft"));
    this.portalRequest.setApplicationKey(ApplicationKey.from("myapplication"));
    this.portalRequest.setBaseUri("/site");
    this.portalRequest.setContentPath(ContentPath.from("context/path"));
}
Also used : PortalRequest(com.enonic.xp.portal.PortalRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 83 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class ExceptionRendererImpl method render.

@Override
public PortalResponse render(final WebRequest webRequest, final WebException cause) {
    String tip = null;
    final ExceptionInfo info = toErrorInfo(cause);
    logIfNeeded(info);
    if (webRequest instanceof PortalRequest) {
        PortalRequest portalRequest = (PortalRequest) webRequest;
        final HttpStatus httpStatus = cause.getStatus();
        if (httpStatus != null) {
            final String handlerMethod = "handle" + httpStatus.value();
            final PortalResponse statusCustomError = renderCustomError(portalRequest, cause, handlerMethod);
            if (statusCustomError != null) {
                logIfNeeded(toErrorInfo(cause));
                return statusCustomError;
            }
        }
        final PortalResponse idProviderError = renderIdProviderError(portalRequest, cause);
        if (idProviderError != null) {
            logIfNeeded(toErrorInfo(cause));
            return idProviderError;
        }
        final PortalResponse defaultCustomError = renderCustomError(portalRequest, cause, DEFAULT_HANDLER);
        if (defaultCustomError != null) {
            logIfNeeded(toErrorInfo(cause));
            return defaultCustomError;
        }
        if ("/site".equals(portalRequest.getBaseUri()) && ContentConstants.BRANCH_MASTER.equals(portalRequest.getBranch()) && HttpStatus.NOT_FOUND.equals(cause.getStatus())) {
            tip = "Tip: Did you remember to publish the site?";
        }
    }
    return renderInternalErrorPage(webRequest, tip, cause);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) HttpStatus(com.enonic.xp.web.HttpStatus) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 84 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class PortalRequestSerializer method serialize.

public PortalRequest serialize() {
    final PortalRequest req = new PortalRequest(sourceRequest);
    if ((value == null) || !value.isObject()) {
        return req;
    }
    req.setBaseUri(sourceRequest.getBaseUri());
    req.setContentPath(sourceRequest.getContentPath());
    req.setContent(sourceRequest.getContent());
    req.setSite(sourceRequest.getSite());
    req.setPageTemplate(sourceRequest.getPageTemplate());
    req.setComponent(sourceRequest.getComponent());
    req.setApplicationKey(sourceRequest.getApplicationKey());
    req.setPageDescriptor(sourceRequest.getPageDescriptor());
    req.setControllerScript(sourceRequest.getControllerScript());
    req.setRepositoryId(sourceRequest.getRepositoryId());
    populateMethod(req, value.getMember("method"));
    populateScheme(req, value.getMember("scheme"));
    populateHost(req, value.getMember("host"));
    populatePort(req, value.getMember("port"));
    populatePath(req, value.getMember("path"));
    populateUrl(req, value.getMember("url"));
    populateRemoteAddress(req, value.getMember("remoteAddress"));
    populateMode(req, value.getMember("mode"));
    populateValidTicket(req, value.getMember("validTicket"));
    populateBranch(req, value.getMember("branch"));
    populateContentType(req, value.getMember("contentType"));
    populateBody(req, value.getMember("body"));
    populateHeaders(req, value.getMember("headers"));
    populateCookies(req, value.getMember("cookies"));
    populateParams(req, value.getMember("params"));
    return req;
}
Also used : PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 85 with PortalRequest

use of com.enonic.xp.portal.PortalRequest in project xp by enonic.

the class MappingHandler method handle.

@Override
public WebResponse handle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) throws Exception {
    if (!(webRequest instanceof PortalRequest) || webRequest.getEndpointPath() != null) {
        return webHandlerChain.handle(webRequest, webResponse);
    }
    final PortalRequest request = (PortalRequest) webRequest;
    if (request.getMode() == RenderMode.ADMIN || !request.isSiteBase()) {
        return webHandlerChain.handle(webRequest, webResponse);
    }
    WebHandlerHelper.checkAdminAccess(request);
    final HttpMethod method = webRequest.getMethod();
    if (!HttpMethod.standard().contains(method)) {
        throw new WebException(HttpStatus.METHOD_NOT_ALLOWED, String.format("Method %s not allowed", method));
    }
    final ContentResolverResult resolvedContent = contentResolver.resolve(request);
    final Site site = resolvedContent.getNearestSite();
    if (site == null) {
        return webHandlerChain.handle(request, webResponse);
    }
    final Content content = resolvedContent.getContent();
    final Optional<ControllerMappingDescriptor> resolve = controllerMappingsResolver.resolve(resolvedContent.getSiteRelativePath(), request.getParams(), content, site.getSiteConfigs());
    if (resolve.isPresent()) {
        final ControllerMappingDescriptor mapping = resolve.get();
        request.setContent(content);
        request.setSite(site);
        request.setContextPath(request.getBaseUri() + "/" + request.getBranch() + site.getPath());
        request.setApplicationKey(mapping.getApplication());
        if (mapping.isController()) {
            return handleController(request, mapping);
        } else {
            return handleFilter(request, webResponse, webHandlerChain, mapping);
        }
    } else {
        return webHandlerChain.handle(request, webResponse);
    }
}
Also used : ContentResolverResult(com.enonic.xp.portal.impl.ContentResolverResult) Site(com.enonic.xp.site.Site) WebException(com.enonic.xp.web.WebException) Content(com.enonic.xp.content.Content) ControllerMappingDescriptor(com.enonic.xp.site.mapping.ControllerMappingDescriptor) HttpMethod(com.enonic.xp.web.HttpMethod) PortalRequest(com.enonic.xp.portal.PortalRequest)

Aggregations

PortalRequest (com.enonic.xp.portal.PortalRequest)103 Test (org.junit.jupiter.api.Test)47 BeforeEach (org.junit.jupiter.api.BeforeEach)35 PortalResponse (com.enonic.xp.portal.PortalResponse)25 Site (com.enonic.xp.site.Site)19 Content (com.enonic.xp.content.Content)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 ApplicationKey (com.enonic.xp.app.ApplicationKey)10 ContentPath (com.enonic.xp.content.ContentPath)10 ContentService (com.enonic.xp.content.ContentService)10 ControllerScriptFactory (com.enonic.xp.portal.controller.ControllerScriptFactory)10 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)9 ResourceKey (com.enonic.xp.resource.ResourceKey)9 ResourceService (com.enonic.xp.resource.ResourceService)9 ContentId (com.enonic.xp.content.ContentId)7 Matcher (java.util.regex.Matcher)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)7 Assertions.fail (org.junit.jupiter.api.Assertions.fail)7 Mockito.when (org.mockito.Mockito.when)7