use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class ExceptionRendererImpl method doRenderCustomError.
private PortalResponse doRenderCustomError(final PortalRequest req, final WebException cause, final String handlerMethod) {
final PortalError portalError = PortalError.create().status(cause.getStatus()).message(cause.getMessage()).exception(cause).request(req).build();
final Site siteInRequest = req.getSite();
final Site site = siteInRequest != null ? siteInRequest : callAsContentAdmin(() -> this.contentService.findNearestSiteByPath(req.getContentPath()));
if (site != null) {
req.setSite(site);
try {
for (SiteConfig siteConfig : site.getSiteConfigs()) {
final ApplicationKey applicationKey = siteConfig.getApplicationKey();
for (final String scriptPath : SITE_ERROR_SCRIPT_PATHS) {
final PortalResponse response = renderApplicationCustomError(applicationKey, scriptPath, portalError, handlerMethod);
if (response != null) {
if (response.isPostProcess()) {
req.setApplicationKey(applicationKey);
}
return response;
}
}
}
} finally {
req.setSite(siteInRequest);
}
} else if (req.getApplicationKey() != null) {
final ApplicationKey applicationKey = req.getApplicationKey();
final PortalResponse response = renderApplicationCustomError(applicationKey, GENERIC_ERROR_SCRIPT_PATH, portalError, handlerMethod);
if (response != null) {
if (response.isPostProcess()) {
req.setApplicationKey(applicationKey);
}
return response;
}
}
return null;
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class IdentityHandler method doHandle.
@Override
protected PortalResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) throws Exception {
final String restPath = findRestPath(webRequest);
final Matcher matcher = PATTERN.matcher(restPath);
if (!matcher.find()) {
throw WebException.notFound("Not a valid idprovider url pattern");
}
final IdProviderKey idProviderKey = IdProviderKey.from(matcher.group(ID_PROVIDER_GROUP_INDEX));
final VirtualHost virtualHost = VirtualHostHelper.getVirtualHost(webRequest.getRawRequest());
if (!(virtualHost == null || virtualHost.getIdProviderKeys().contains(idProviderKey))) {
throw WebException.forbidden(String.format("'%s' id provider is forbidden", idProviderKey));
}
String idProviderFunction = matcher.group(2);
final PortalRequest portalRequest = webRequest instanceof PortalRequest ? (PortalRequest) webRequest : new PortalRequest(webRequest);
portalRequest.setContextPath(findPreRestPath(portalRequest) + "/" + matcher.group(ID_PROVIDER_GROUP_INDEX));
if (idProviderFunction != null) {
checkTicket(portalRequest);
}
if (idProviderFunction == null) {
idProviderFunction = webRequest.getMethod().toString().toLowerCase();
}
final IdentityHandlerWorker worker = new IdentityHandlerWorker(portalRequest);
worker.idProviderKey = idProviderKey;
worker.idProviderFunction = idProviderFunction;
worker.contentResolver = new ContentResolver(contentService);
worker.idProviderControllerService = this.idProviderControllerService;
final Trace trace = Tracer.newTrace("portalRequest");
if (trace == null) {
return worker.execute();
}
trace.put("path", webRequest.getPath());
trace.put("method", webRequest.getMethod().toString());
trace.put("host", webRequest.getHost());
trace.put("httpRequest", webRequest);
trace.put("httpResponse", webResponse);
trace.put("context", ContextAccessor.current());
return Tracer.traceEx(trace, () -> {
final PortalResponse response = worker.execute();
addTraceInfo(trace, response);
return response;
});
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class ExceptionMapperTest method assertThrowIfNeeded.
private void assertThrowIfNeeded(final HttpStatus status) {
final PortalResponse response = PortalResponse.create().status(status).build();
try {
this.mapper.throwIfNeeded(response);
fail("Should throw exception");
} catch (final WebException e) {
assertEquals(status, e.getStatus());
}
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class ExceptionMapperTest method throwIfNeeded_notNeeded.
@Test
public void throwIfNeeded_notNeeded() {
final PortalResponse response = PortalResponse.create().status(HttpStatus.OK).build();
this.mapper.throwIfNeeded(response);
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class MappingHandlerTest method methodNotAllowed.
@Test
public void methodNotAllowed() {
final PortalResponse response = PortalResponse.create().build();
this.request.setBaseUri("/admin/site");
this.request.setContentPath(ContentPath.from("/site/content"));
this.request.setMethod(HttpMethod.LOCK);
final WebException webException = assertThrows(WebException.class, () -> this.handler.handle(this.request, response, null));
assertEquals(HttpStatus.METHOD_NOT_ALLOWED, webException.getStatus());
}
Aggregations