Search in sources :

Example 21 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project keycloak by keycloak.

the class HttpBasicAuthenticator method authenticate.

@Override
public void authenticate(final AuthenticationFlowContext context) {
    final HttpRequest httpRequest = context.getHttpRequest();
    final HttpHeaders httpHeaders = httpRequest.getHttpHeaders();
    final String[] usernameAndPassword = getUsernameAndPassword(httpHeaders);
    context.attempted();
    if (usernameAndPassword != null) {
        final RealmModel realm = context.getRealm();
        final String username = usernameAndPassword[0];
        final UserModel user = context.getSession().users().getUserByUsername(realm, username);
        // to allow success/failure logging for brute force
        context.getEvent().detail(Details.USERNAME, username);
        context.getAuthenticationSession().setAuthNote(AbstractUsernameFormAuthenticator.ATTEMPTED_USERNAME, username);
        if (user != null) {
            final String password = usernameAndPassword[1];
            final boolean valid = context.getSession().userCredentialManager().isValid(realm, user, UserCredentialModel.password(password));
            if (valid) {
                if (isTemporarilyDisabledByBruteForce(context, user)) {
                    userDisabledAction(context, realm, user, Errors.USER_TEMPORARILY_DISABLED);
                } else if (user.isEnabled()) {
                    userSuccessAction(context, user);
                } else {
                    userDisabledAction(context, realm, user, Errors.USER_DISABLED);
                }
            } else {
                notValidCredentialsAction(context, realm, user);
            }
        } else {
            nullUserAction(context, realm, username);
        }
    }
}
Also used : HttpRequest(org.jboss.resteasy.spi.HttpRequest) RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) HttpHeaders(javax.ws.rs.core.HttpHeaders)

Example 22 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project ddf by codice.

the class CatalogServiceImplTest method testAddDocumentWithAttributeOverrides.

@Test
@SuppressWarnings({ "unchecked" })
public void testAddDocumentWithAttributeOverrides() throws Exception {
    CatalogFramework framework = givenCatalogFramework();
    AttributeDescriptor descriptor = new AttributeDescriptorImpl("custom.attribute", true, true, false, false, BasicTypes.STRING_TYPE);
    HttpHeaders headers = createHeaders(Collections.singletonList(MediaType.APPLICATION_JSON));
    BundleContext bundleContext = mock(BundleContext.class);
    Collection<ServiceReference<InputTransformer>> serviceReferences = new ArrayList<>();
    ServiceReference serviceReference = mock(ServiceReference.class);
    InputTransformer inputTransformer = mock(InputTransformer.class);
    when(inputTransformer.transform(any())).thenReturn(new MetacardImpl());
    when(bundleContext.getService(serviceReference)).thenReturn(inputTransformer);
    serviceReferences.add(serviceReference);
    when(bundleContext.getServiceReferences(InputTransformer.class, "(id=xml)")).thenReturn(serviceReferences);
    when(attributeRegistry.lookup("custom.attribute")).thenReturn(Optional.of(descriptor));
    CatalogServiceImpl catalogService = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry) {

        @Override
        protected BundleContext getBundleContext() {
            return bundleContext;
        }
    };
    UuidGenerator uuidGenerator = mock(UuidGenerator.class);
    when(uuidGenerator.generateUuid()).thenReturn(UUID.randomUUID().toString());
    catalogService.setUuidGenerator(uuidGenerator);
    addMatchingService(catalogService, Collections.singletonList(getSimpleTransformer()));
    List<Attachment> attachments = new ArrayList<>();
    ContentDisposition contentDisposition = new ContentDisposition("form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
    Attachment attachment = new Attachment("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), contentDisposition);
    attachments.add(attachment);
    ContentDisposition contentDisposition2 = new ContentDisposition("form-data; name=custom.attribute; ");
    Attachment attachment2 = new Attachment(descriptor.getName(), new ByteArrayInputStream("CustomValue".getBytes()), contentDisposition2);
    attachments.add(attachment2);
    MultipartBody multipartBody = new MultipartBody(attachments);
    String response = catalogService.addDocument(headers.getRequestHeader(HttpHeaders.CONTENT_TYPE), multipartBody, null, new ByteArrayInputStream("".getBytes()));
    LOGGER.debug(ToStringBuilder.reflectionToString(response));
    ArgumentCaptor<CreateStorageRequest> captor = ArgumentCaptor.forClass(CreateStorageRequest.class);
    verify(framework, times(1)).create(captor.capture());
    assertThat(captor.getValue().getContentItems().get(0).getMetacard().getMetacardType().getAttributeDescriptor(descriptor.getName()), equalTo(descriptor));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) UuidGenerator(org.codice.ddf.platform.util.uuidgenerator.UuidGenerator) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) ServiceReference(org.osgi.framework.ServiceReference) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) CatalogFramework(ddf.catalog.CatalogFramework) BundleContext(org.osgi.framework.BundleContext) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest) Test(org.junit.Test)

Example 23 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project ddf by codice.

the class CatalogServiceImplTest method testAddDocumentNullMessage.

@Test
public void testAddDocumentNullMessage() {
    CatalogFramework framework = mock(CatalogFramework.class);
    CatalogServiceImpl catalogService = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry);
    HttpHeaders headers = mock(HttpHeaders.class);
    try {
        catalogService.addDocument(headers.getRequestHeader(HttpHeaders.CONTENT_TYPE), mock(MultipartBody.class), null, null);
    } catch (CatalogServiceException e) {
        assertEquals(e.getMessage(), "No content found, cannot do CREATE.");
    }
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) CatalogFramework(ddf.catalog.CatalogFramework) Test(org.junit.Test)

Example 24 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project ddf by codice.

the class CatalogServiceImplTest method mcardIdTest.

private String mcardIdTest(Metacard metacard, UuidGenerator uuidGenerator) throws Exception {
    CatalogFramework framework = mock(CatalogFramework.class);
    when(framework.create(isA(CreateStorageRequest.class))).thenAnswer(args -> {
        ContentItem item = ((CreateStorageRequest) args.getArguments()[0]).getContentItems().get(0);
        item.getMetacard().setAttribute(new AttributeImpl(Core.ID, item.getId()));
        return new CreateResponseImpl(null, new HashMap<>(), Collections.singletonList(item.getMetacard()));
    });
    HttpHeaders headers = createHeaders(Collections.singletonList(MediaType.APPLICATION_JSON));
    BundleContext bundleContext = mock(BundleContext.class);
    Collection<ServiceReference<InputTransformer>> serviceReferences = new ArrayList<>();
    ServiceReference serviceReference = mock(ServiceReference.class);
    InputTransformer inputTransformer = mock(InputTransformer.class);
    when(inputTransformer.transform(any())).thenReturn(metacard);
    when(bundleContext.getService(serviceReference)).thenReturn(inputTransformer);
    serviceReferences.add(serviceReference);
    when(bundleContext.getServiceReferences(InputTransformer.class, "(id=xml)")).thenReturn(serviceReferences);
    CatalogServiceImpl catalogService = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry) {

        @Override
        protected BundleContext getBundleContext() {
            return bundleContext;
        }
    };
    String generatedMcardId = UUID.randomUUID().toString();
    when(uuidGenerator.generateUuid()).thenReturn(generatedMcardId);
    catalogService.setUuidGenerator(uuidGenerator);
    when(attributeRegistry.lookup(Core.METADATA)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.METADATA)));
    addMatchingService(catalogService, Collections.singletonList(inputTransformer));
    List<Attachment> attachments = new ArrayList<>();
    ContentDisposition contentDisposition = new ContentDisposition("form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
    Attachment attachment = new Attachment("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), contentDisposition);
    attachments.add(attachment);
    ContentDisposition contentDisposition1 = new ContentDisposition("form-data; name=parse.metadata; filename=C:\\DDF\\metacard.xml");
    Attachment attachment1 = new Attachment("parse.metadata", new ByteArrayInputStream("Some Text Again".getBytes()), contentDisposition1);
    attachments.add(attachment1);
    MultipartBody multipartBody = new MultipartBody(attachments);
    return catalogService.addDocument(headers.getRequestHeader(HttpHeaders.CONTENT_TYPE), multipartBody, null, new ByteArrayInputStream("".getBytes()));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) CoreAttributes(ddf.catalog.data.impl.types.CoreAttributes) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) ServiceReference(org.osgi.framework.ServiceReference) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) CatalogFramework(ddf.catalog.CatalogFramework) ContentItem(ddf.catalog.content.data.ContentItem) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest) CreateResponseImpl(ddf.catalog.operation.impl.CreateResponseImpl) BundleContext(org.osgi.framework.BundleContext)

Example 25 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project ddf by codice.

the class CatalogServiceImplTest method createHeaders.

private HttpHeaders createHeaders(List<String> mimeTypeList) {
    HttpHeaders headers = mock(HttpHeaders.class);
    when(headers.getRequestHeader(HttpHeaders.CONTENT_TYPE)).thenReturn(mimeTypeList);
    return headers;
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders)

Aggregations

HttpHeaders (javax.ws.rs.core.HttpHeaders)95 Test (org.junit.Test)57 Message (org.apache.cxf.message.Message)31 Optional (java.util.Optional)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 UriInfo (javax.ws.rs.core.UriInfo)12 CatalogFramework (ddf.catalog.CatalogFramework)10 Locale (java.util.Locale)10 Response (javax.ws.rs.core.Response)10 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)10 JsonObject (com.google.gson.JsonObject)9 MediaType (javax.ws.rs.core.MediaType)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Map (java.util.Map)8 Cookie (javax.ws.rs.core.Cookie)8 IOException (java.io.IOException)7 Collections (java.util.Collections)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 URI (java.net.URI)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6