Search in sources :

Example 1 with HttpMethod

use of com.enonic.xp.web.HttpMethod in project xp by enonic.

the class ControllerScriptImpl method doExecute.

private PortalResponse doExecute(final PortalRequest portalRequest) {
    Tracer.withCurrent(this::addTraceInfo);
    final HttpMethod method = portalRequest.getMethod();
    final boolean isHead = method == HttpMethod.HEAD;
    String runMethod = isHead ? "get" : method.toString().toLowerCase();
    final boolean exists = this.scriptExports.hasMethod(runMethod);
    if (!exists) {
        if (this.scriptExports.hasMethod(ALL_SCRIPT_METHOD_NAME)) {
            runMethod = ALL_SCRIPT_METHOD_NAME;
        } else {
            return new PortalResponseSerializer(null, HttpStatus.METHOD_NOT_ALLOWED).serialize();
        }
    }
    final PortalRequestMapper requestMapper = new PortalRequestMapper(portalRequest);
    final ScriptValue result = this.scriptExports.executeMethod(runMethod, requestMapper);
    return new PortalResponseSerializer(result).serialize();
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalRequestMapper(com.enonic.xp.portal.impl.mapper.PortalRequestMapper) HttpMethod(com.enonic.xp.web.HttpMethod)

Example 2 with HttpMethod

use of com.enonic.xp.web.HttpMethod 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)

Example 3 with HttpMethod

use of com.enonic.xp.web.HttpMethod in project xp by enonic.

the class BaseWebHandler method handle.

@Override
public WebResponse handle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) throws Exception {
    if (canHandle(webRequest)) {
        final HttpMethod method = webRequest.getMethod();
        checkMethodAllowed(method);
        final WebResponse response = doHandle(webRequest, webResponse, webHandlerChain);
        if (HttpMethod.OPTIONS == method && response.getStatus() == HttpStatus.METHOD_NOT_ALLOWED) {
            return handleDefaultOptions();
        }
        return response;
    } else {
        return webHandlerChain.handle(webRequest, webResponse);
    }
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) HttpMethod(com.enonic.xp.web.HttpMethod)

Aggregations

HttpMethod (com.enonic.xp.web.HttpMethod)3 Content (com.enonic.xp.content.Content)1 PortalRequest (com.enonic.xp.portal.PortalRequest)1 ContentResolverResult (com.enonic.xp.portal.impl.ContentResolverResult)1 PortalRequestMapper (com.enonic.xp.portal.impl.mapper.PortalRequestMapper)1 ScriptValue (com.enonic.xp.script.ScriptValue)1 Site (com.enonic.xp.site.Site)1 ControllerMappingDescriptor (com.enonic.xp.site.mapping.ControllerMappingDescriptor)1 WebException (com.enonic.xp.web.WebException)1 WebResponse (com.enonic.xp.web.WebResponse)1