Search in sources :

Example 46 with WebException

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

the class ServiceHandlerTest method testForbiddenService.

@Test
public void testForbiddenService() throws Exception {
    final DescriptorKey serviceDescriptorKey = DescriptorKey.from("demo:test");
    final Set<PrincipalKey> allowedPrincipals = Collections.singleton(PrincipalKey.from("role:system.admin"));
    final ServiceDescriptor serviceDescriptor = ServiceDescriptor.create().key(serviceDescriptorKey).setAllowedPrincipals(allowedPrincipals).build();
    Mockito.when(this.serviceDescriptorService.getByKey(serviceDescriptorKey)).thenReturn(serviceDescriptor);
    this.request.setEndpointPath("/_/service/demo/test");
    boolean forbiddenErrorThrown = false;
    try {
        this.handler.handle(this.request, PortalResponse.create().build(), null);
    } catch (WebException e) {
        if (HttpStatus.UNAUTHORIZED == e.getStatus()) {
            forbiddenErrorThrown = true;
        }
    }
    assertTrue(forbiddenErrorThrown);
}
Also used : WebException(com.enonic.xp.web.WebException) ServiceDescriptor(com.enonic.xp.service.ServiceDescriptor) DescriptorKey(com.enonic.xp.page.DescriptorKey) PrincipalKey(com.enonic.xp.security.PrincipalKey) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 47 with WebException

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

the class ExceptionMapperImpl method throwIfNeeded.

@Override
public void throwIfNeeded(final WebResponse res) throws WebException {
    final HttpStatus status = res.getStatus();
    final Object body = res.getBody();
    if (isError(status) && (body == null)) {
        throw new WebException(status, status.getReasonPhrase());
    }
}
Also used : WebException(com.enonic.xp.web.WebException) HttpStatus(com.enonic.xp.web.HttpStatus)

Aggregations

WebException (com.enonic.xp.web.WebException)47 Test (org.junit.jupiter.api.Test)39 PortalResponse (com.enonic.xp.portal.PortalResponse)14 ContentPath (com.enonic.xp.content.ContentPath)12 Site (com.enonic.xp.site.Site)10 PortalRequest (com.enonic.xp.portal.PortalRequest)8 BaseHandlerTest (com.enonic.xp.web.handler.BaseHandlerTest)7 ContentNotFoundException (com.enonic.xp.content.ContentNotFoundException)6 HttpStatus (com.enonic.xp.web.HttpStatus)5 MediaType (com.google.common.net.MediaType)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 ApplicationKey (com.enonic.xp.app.ApplicationKey)4 Branch (com.enonic.xp.branch.Branch)4 Content (com.enonic.xp.content.Content)4 ContentId (com.enonic.xp.content.ContentId)4 ContentService (com.enonic.xp.content.ContentService)4 PropertyTree (com.enonic.xp.data.PropertyTree)4 RenderMode (com.enonic.xp.portal.RenderMode)4 ErrorHandlerScript (com.enonic.xp.portal.impl.error.ErrorHandlerScript)4 ErrorHandlerScriptFactory (com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory)4