Search in sources :

Example 1 with SecurityQuestionTO

use of org.apache.syncope.common.lib.to.SecurityQuestionTO in project syncope by apache.

the class SecurityQuestionsResource method newResourceResponse.

@Override
protected AbstractResource.ResourceResponse newResourceResponse(final IResource.Attributes attributes) {
    LOG.debug("List available security questions");
    AbstractResource.ResourceResponse response = new AbstractResource.ResourceResponse();
    response.setContentType(MediaType.APPLICATION_JSON);
    try {
        HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
        if (!xsrfCheck(request)) {
            LOG.error("XSRF TOKEN does not match");
            response.setError(Response.Status.BAD_REQUEST.getStatusCode(), "XSRF TOKEN does not match");
            return response;
        }
        final List<SecurityQuestionTO> securityQuestionTOs = SyncopeEnduserSession.get().getService(SecurityQuestionService.class).list();
        response.setWriteCallback(new AbstractResource.WriteCallback() {

            @Override
            public void writeData(final IResource.Attributes attributes) throws IOException {
                attributes.getResponse().write(MAPPER.writeValueAsString(securityQuestionTOs));
            }
        });
        response.setTextEncoding(StandardCharsets.UTF_8.name());
        response.setStatusCode(Response.Status.OK.getStatusCode());
    } catch (Exception e) {
        LOG.error("Error retrieving security questions", e);
        response.setError(Response.Status.BAD_REQUEST.getStatusCode(), new StringBuilder().append("ErrorMessage{{ ").append(e.getMessage()).append(" }}").toString());
    }
    return response;
}
Also used : SecurityQuestionService(org.apache.syncope.common.rest.api.service.SecurityQuestionService) AbstractResource(org.apache.wicket.request.resource.AbstractResource) IOException(java.io.IOException) IOException(java.io.IOException) SecurityQuestionTO(org.apache.syncope.common.lib.to.SecurityQuestionTO) HttpServletRequest(javax.servlet.http.HttpServletRequest) IResource(org.apache.wicket.request.resource.IResource)

Example 2 with SecurityQuestionTO

use of org.apache.syncope.common.lib.to.SecurityQuestionTO in project syncope by apache.

the class SecurityQuestionITCase method create.

@Test
public void create() {
    SecurityQuestionTO securityQuestionTO = new SecurityQuestionTO();
    securityQuestionTO.setContent("What is your favorite pet's name?");
    Response response = securityQuestionService.create(securityQuestionTO);
    SecurityQuestionTO actual = getObject(response.getLocation(), SecurityQuestionService.class, SecurityQuestionTO.class);
    assertNotNull(actual);
    assertNotNull(actual.getKey());
    securityQuestionTO.setKey(actual.getKey());
    assertEquals(actual, securityQuestionTO);
}
Also used : Response(javax.ws.rs.core.Response) SecurityQuestionTO(org.apache.syncope.common.lib.to.SecurityQuestionTO) Test(org.junit.jupiter.api.Test)

Example 3 with SecurityQuestionTO

use of org.apache.syncope.common.lib.to.SecurityQuestionTO in project syncope by apache.

the class SecurityQuestionByUsernameResource method newResourceResponse.

@Override
protected AbstractResource.ResourceResponse newResourceResponse(final IResource.Attributes attributes) {
    LOG.debug("List available security questions");
    ResourceResponse response = new AbstractResource.ResourceResponse();
    response.setContentType(MediaType.APPLICATION_JSON);
    try {
        HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
        if (!xsrfCheck(request)) {
            LOG.error("XSRF TOKEN does not match");
            response.setError(Response.Status.BAD_REQUEST.getStatusCode(), "XSRF TOKEN does not match");
            return response;
        }
        PageParameters parameters = attributes.getParameters();
        StringValue username = parameters.get("username");
        if (!username.isEmpty()) {
            final SecurityQuestionTO securityQuestionTO = SyncopeEnduserSession.get().getService(SecurityQuestionService.class).readByUser(username.toString());
            response.setWriteCallback(new AbstractResource.WriteCallback() {

                @Override
                public void writeData(final IResource.Attributes attributes) throws IOException {
                    attributes.getResponse().write(MAPPER.writeValueAsString(securityQuestionTO));
                }
            });
        }
        response.setContentType(MediaType.APPLICATION_JSON);
        response.setTextEncoding(StandardCharsets.UTF_8.name());
        response.setStatusCode(Response.Status.OK.getStatusCode());
    } catch (Exception e) {
        LOG.error("Error retrieving security questions", e);
        response.setError(Response.Status.BAD_REQUEST.getStatusCode(), new StringBuilder().append("ErrorMessage{{ ").append(e.getMessage()).append(" }}").toString());
    }
    return response;
}
Also used : SecurityQuestionService(org.apache.syncope.common.rest.api.service.SecurityQuestionService) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) AbstractResource(org.apache.wicket.request.resource.AbstractResource) IOException(java.io.IOException) IOException(java.io.IOException) SecurityQuestionTO(org.apache.syncope.common.lib.to.SecurityQuestionTO) HttpServletRequest(javax.servlet.http.HttpServletRequest) StringValue(org.apache.wicket.util.string.StringValue) IResource(org.apache.wicket.request.resource.IResource)

Example 4 with SecurityQuestionTO

use of org.apache.syncope.common.lib.to.SecurityQuestionTO in project syncope by apache.

the class SecurityQuestionDataBinderImpl method getSecurityQuestionTO.

@Override
public SecurityQuestionTO getSecurityQuestionTO(final SecurityQuestion securityQuestion) {
    SecurityQuestionTO securityQuestionTO = new SecurityQuestionTO();
    BeanUtils.copyProperties(securityQuestion, securityQuestionTO);
    return securityQuestionTO;
}
Also used : SecurityQuestionTO(org.apache.syncope.common.lib.to.SecurityQuestionTO)

Example 5 with SecurityQuestionTO

use of org.apache.syncope.common.lib.to.SecurityQuestionTO in project syncope by apache.

the class SecurityQuestionITCase method delete.

@Test
public void delete() {
    SecurityQuestionTO securityQuestion = new SecurityQuestionTO();
    securityQuestion.setContent("What is your first pet's name?");
    Response response = securityQuestionService.create(securityQuestion);
    securityQuestion = getObject(response.getLocation(), SecurityQuestionService.class, SecurityQuestionTO.class);
    securityQuestionService.delete(securityQuestion.getKey());
    try {
        securityQuestionService.read(securityQuestion.getKey());
        fail("This should not happen");
    } catch (SyncopeClientException e) {
        assertEquals(ClientExceptionType.NotFound, e.getType());
    }
}
Also used : Response(javax.ws.rs.core.Response) SecurityQuestionService(org.apache.syncope.common.rest.api.service.SecurityQuestionService) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SecurityQuestionTO(org.apache.syncope.common.lib.to.SecurityQuestionTO) Test(org.junit.jupiter.api.Test)

Aggregations

SecurityQuestionTO (org.apache.syncope.common.lib.to.SecurityQuestionTO)10 Test (org.junit.jupiter.api.Test)5 SecurityQuestionService (org.apache.syncope.common.rest.api.service.SecurityQuestionService)3 IOException (java.io.IOException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Response (javax.ws.rs.core.Response)2 AbstractResource (org.apache.wicket.request.resource.AbstractResource)2 IResource (org.apache.wicket.request.resource.IResource)2 URI (java.net.URI)1 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)1 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)1 SecurityQuestion (org.apache.syncope.core.persistence.api.entity.user.SecurityQuestion)1 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)1 StringValue (org.apache.wicket.util.string.StringValue)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1