Search in sources :

Example 31 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class ValidationExceptionMapperTest method testToResponse.

@Test
public void testToResponse() throws Exception {
    final ExceptionMapper<ValidationException> mapper = new ValidationExceptionMapper();
    final Map<String, List<ValidationResult>> validationErrors = ImmutableMap.of("foo", ImmutableList.of(new ValidationResult.ValidationFailed("foo failed")), "bar", ImmutableList.of(new ValidationResult.ValidationFailed("bar failed"), new ValidationResult.ValidationFailed("baz failed")));
    @SuppressWarnings("ThrowableInstanceNeverThrown") final ValidationException exception = new ValidationException(validationErrors);
    final Response response = mapper.toResponse(exception);
    assertThat(response.getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
    assertThat(response.getMediaType()).isEqualTo(MediaType.APPLICATION_JSON_TYPE);
    assertThat(response.hasEntity()).isTrue();
    assertThat(response.getEntity()).isInstanceOf(ValidationApiError.class);
    final ValidationApiError responseEntity = (ValidationApiError) response.getEntity();
    assertThat(responseEntity.getMessage()).startsWith("Validation failed!");
    assertThat(responseEntity.getValidationErrors()).containsKeys("foo", "bar");
    assertThat(responseEntity.getValidationErrors().get("foo")).hasSize(1);
    assertThat(responseEntity.getValidationErrors().get("bar")).hasSize(2);
}
Also used : Response(javax.ws.rs.core.Response) ValidationException(org.graylog2.plugin.database.ValidationException) ValidationApiError(org.graylog2.plugin.rest.ValidationApiError) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Test(org.junit.Test)

Example 32 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class BundleImporter method createInputs.

private void createInputs(final String bundleId, final Set<Input> inputs, final String userName) throws org.graylog2.plugin.inputs.Extractor.ReservedFieldException, org.graylog2.ConfigurationException, NoSuchInputTypeException, ValidationException, ExtractorFactory.NoSuchExtractorException, NotFoundException, ConfigurationException {
    for (final Input input : inputs) {
        final MessageInput messageInput = createMessageInput(bundleId, input, userName);
        createdInputs.put(messageInput.getId(), messageInput);
        // Launch input. (this will run async and clean up itself in case of an error.)
        inputLauncher.launch(messageInput);
    }
}
Also used : MessageInput(org.graylog2.plugin.inputs.MessageInput) MessageInput(org.graylog2.plugin.inputs.MessageInput)

Example 33 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class BundleImporter method createMessageInput.

private MessageInput createMessageInput(final String bundleId, final Input inputDescription, final String userName) throws NoSuchInputTypeException, ConfigurationException, ValidationException, NotFoundException, org.graylog2.ConfigurationException, ExtractorFactory.NoSuchExtractorException, org.graylog2.plugin.inputs.Extractor.ReservedFieldException {
    final Configuration inputConfig = new Configuration(inputDescription.getConfiguration());
    final DateTime createdAt = Tools.nowUTC();
    final MessageInput messageInput = messageInputFactory.create(inputDescription.getType(), inputConfig);
    messageInput.setTitle(inputDescription.getTitle());
    messageInput.setGlobal(inputDescription.isGlobal());
    messageInput.setCreatorUserId(userName);
    messageInput.setCreatedAt(createdAt);
    messageInput.setContentPack(bundleId);
    messageInput.checkConfiguration();
    // Don't run if exclusive and another instance is already running.
    if (messageInput.isExclusive() && inputRegistry.hasTypeRunning(messageInput.getClass())) {
        LOG.error("Input type <{}> of input <{}> is exclusive and already has input running.", messageInput.getClass(), messageInput.getTitle());
    }
    final String id = inputDescription.getId();
    final org.graylog2.inputs.Input mongoInput;
    if (id == null) {
        mongoInput = inputService.create(buildMongoDbInput(inputDescription, userName, createdAt, bundleId));
    } else {
        mongoInput = inputService.create(id, buildMongoDbInput(inputDescription, userName, createdAt, bundleId));
    }
    // Persist input.
    final String persistId = inputService.save(mongoInput);
    messageInput.setPersistId(persistId);
    messageInput.initialize();
    addStaticFields(messageInput, inputDescription.getStaticFields());
    addExtractors(messageInput, inputDescription.getExtractors(), userName);
    return messageInput;
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) MessageInput(org.graylog2.plugin.inputs.MessageInput) DateTime(org.joda.time.DateTime)

Example 34 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class Configuration method validateNetworkInterfaces.

@ValidatorMethod
@SuppressWarnings("unused")
public void validateNetworkInterfaces() throws ValidationException {
    final URI restListenUri = getRestListenUri();
    final URI webListenUri = getWebListenUri();
    if (restListenUri.getPort() == webListenUri.getPort() && !restListenUri.getHost().equals(webListenUri.getHost()) && (WILDCARD_IP_ADDRESS.equals(restListenUri.getHost()) || WILDCARD_IP_ADDRESS.equals(webListenUri.getHost()))) {
        throw new ValidationException("Wildcard IP addresses cannot be used if the Graylog REST API and web interface listen on the same port.");
    }
}
Also used : ValidationException(com.github.joschi.jadconfig.ValidationException) Tools.normalizeURI(org.graylog2.plugin.Tools.normalizeURI) URI(java.net.URI) ValidatorMethod(com.github.joschi.jadconfig.ValidatorMethod)

Example 35 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class EsNodeProviderTest method zenUnicastHostsAreTrimmed.

@Test
public void zenUnicastHostsAreTrimmed() throws IOException, ValidationException, RepositoryException {
    Map<String, String> settings = ImmutableMap.of("password_secret", "thisisatest", "retention_strategy", "delete", "root_password_sha2", "thisisatest", "elasticsearch_discovery_zen_ping_unicast_hosts", " example.com,   example.net ");
    final ElasticsearchConfiguration config = new ElasticsearchConfiguration();
    new JadConfig(new InMemoryRepository(settings), config).process();
    final Settings nodeSettings = EsNodeProvider.readNodeSettings(config, nodeId);
    assertThat(nodeSettings.getAsArray("discovery.zen.ping.unicast.hosts")).contains("example.com", "example.net");
}
Also used : JadConfig(com.github.joschi.jadconfig.JadConfig) InMemoryRepository(com.github.joschi.jadconfig.repositories.InMemoryRepository) ElasticsearchConfiguration(org.graylog2.configuration.ElasticsearchConfiguration) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)47 AuditEvent (org.graylog2.audit.jersey.AuditEvent)47 Timed (com.codahale.metrics.annotation.Timed)36 ValidationException (org.graylog2.plugin.database.ValidationException)32 ApiResponses (io.swagger.annotations.ApiResponses)29 Path (javax.ws.rs.Path)29 BadRequestException (javax.ws.rs.BadRequestException)25 PUT (javax.ws.rs.PUT)24 Produces (javax.ws.rs.Produces)23 Consumes (javax.ws.rs.Consumes)22 POST (javax.ws.rs.POST)21 URI (java.net.URI)18 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)13 Stream (org.graylog2.plugin.streams.Stream)12 User (org.graylog2.plugin.database.users.User)11 MessageInput (org.graylog2.plugin.inputs.MessageInput)11 NotFoundException (org.graylog2.database.NotFoundException)10 List (java.util.List)7 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)7 Dashboard (org.graylog2.dashboards.Dashboard)7