http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java deleted file mode 100644 index 716645c..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java +++ /dev/null @@ -1,169 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - - -package org.apache.james.webadmin; - -import java.util.Objects; -import java.util.Optional; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Preconditions; - -public class WebAdminConfiguration { - - public static final boolean DEFAULT_CORS_DISABLED = false; - public static final String CORS_ALL_ORIGINS = "*"; - - public static WebAdminConfiguration testingConfiguration() { - return WebAdminConfiguration.builder() - .enabled() - .port(new RandomPort()) - .build(); - } - - public static final WebAdminConfiguration DISABLED_CONFIGURATION = WebAdminConfiguration.builder() - .disabled() - .build(); - - public static Builder builder() { - return new Builder(); - } - - public static class Builder { - private Optional<Boolean> enabled = Optional.empty(); - private Optional<Port> port = Optional.empty(); - private Optional<Boolean> enableCORS = Optional.empty(); - private Optional<TlsConfiguration> tlsConfiguration = Optional.empty(); - private Optional<String> urlCORSOrigin = Optional.empty(); - - public Builder tls(TlsConfiguration tlsConfiguration) { - this.tlsConfiguration = Optional.of(tlsConfiguration); - return this; - } - - public Builder tls(Optional<TlsConfiguration> tlsConfiguration) { - this.tlsConfiguration = tlsConfiguration; - return this; - } - - public Builder port(Port port) { - this.port = Optional.of(port); - return this; - } - - public Builder enable(boolean isEnabled) { - this.enabled = Optional.of(isEnabled); - return this; - } - public Builder enabled() { - return enable(true); - } - - public Builder disabled() { - return enable(false); - } - - public Builder urlCORSOrigin(String origin) { - this.urlCORSOrigin = Optional.ofNullable(origin); - return this; - } - - public Builder enableCORS(boolean isEnabled) { - this.enableCORS = Optional.of(isEnabled); - return this; - } - - public Builder CORSenabled() { - return enableCORS(true); - } - - public Builder CORSdisabled() { - return enableCORS(false); - } - - public WebAdminConfiguration build() { - Preconditions.checkState(enabled.isPresent(), "You need to explicitly enable or disable WebAdmin server"); - Preconditions.checkState(!enabled.get() || port.isPresent(), "You need to specify a port for WebAdminConfiguration"); - return new WebAdminConfiguration(enabled.get(), - port, - tlsConfiguration, - enableCORS.orElse(DEFAULT_CORS_DISABLED), - urlCORSOrigin.orElse(CORS_ALL_ORIGINS)); - } - } - - private final boolean enabled; - private final Optional<Port> port; - private final Optional<TlsConfiguration> tlsConfiguration; - private final boolean enableCORS; - private final String urlCORSOrigin; - - @VisibleForTesting - WebAdminConfiguration(boolean enabled, Optional<Port> port, Optional<TlsConfiguration> tlsConfiguration, boolean enableCORS, String urlCORSOrigin) { - this.enabled = enabled; - this.port = port; - this.tlsConfiguration = tlsConfiguration; - this.enableCORS = enableCORS; - this.urlCORSOrigin = urlCORSOrigin; - } - - public boolean isEnabled() { - return enabled; - } - - public String getUrlCORSOrigin() { - return urlCORSOrigin; - } - - public Port getPort() { - return port.orElseThrow(() -> new IllegalStateException("No port was specified")); - } - - public TlsConfiguration getTlsConfiguration() { - return tlsConfiguration.orElseThrow(() -> new IllegalStateException("No tls configuration")); - } - - public boolean isEnableCORS() { - return enableCORS; - } - - public boolean isTlsEnabled() { - return tlsConfiguration.isPresent(); - } - - @Override - public final boolean equals(Object o) { - if (o instanceof WebAdminConfiguration) { - WebAdminConfiguration that = (WebAdminConfiguration) o; - - return Objects.equals(this.enabled, that.enabled) - && Objects.equals(this.port, that.port) - && Objects.equals(this.tlsConfiguration, that.tlsConfiguration) - && Objects.equals(this.enableCORS, that.enableCORS) - && Objects.equals(this.urlCORSOrigin, that.urlCORSOrigin); - } - return false; - } - - @Override - public final int hashCode() { - return Objects.hash(enabled, port, tlsConfiguration, enableCORS, urlCORSOrigin); - } -}
http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java deleted file mode 100644 index abce524..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java +++ /dev/null @@ -1,129 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin; - -import java.io.IOException; -import java.util.Set; - -import javax.annotation.PreDestroy; -import javax.inject.Inject; - -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.HierarchicalConfiguration; -import org.apache.james.lifecycle.api.Configurable; -import org.apache.james.metrics.api.MetricFactory; -import org.apache.james.webadmin.authentication.AuthenticationFilter; -import org.apache.james.webadmin.authentication.NoAuthenticationFilter; -import org.apache.james.webadmin.metric.MetricPostFilter; -import org.apache.james.webadmin.metric.MetricPreFilter; -import org.apache.james.webadmin.routes.CORSRoute; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.ImmutableSet; - -import spark.Service; - -public class WebAdminServer implements Configurable { - - private static final Logger LOGGER = LoggerFactory.getLogger(WebAdminServer.class); - public static final HierarchicalConfiguration NO_CONFIGURATION = null; - public static final int DEFAULT_PORT = 8080; - - private final WebAdminConfiguration configuration; - private final Set<Routes> routesList; - private final Service service; - private final AuthenticationFilter authenticationFilter; - private final MetricFactory metricFactory; - - // Spark do not allow to retrieve allocated port when using a random port. Thus we generate the port. - @Inject - private WebAdminServer(WebAdminConfiguration configuration, Set<Routes> routesList, AuthenticationFilter authenticationFilter, - MetricFactory metricFactory) { - this.configuration = configuration; - this.routesList = routesList; - this.authenticationFilter = authenticationFilter; - this.metricFactory = metricFactory; - this.service = Service.ignite(); - } - - @VisibleForTesting - public WebAdminServer(MetricFactory metricFactory, Routes... routes) throws IOException { - this(WebAdminConfiguration.testingConfiguration(), - ImmutableSet.copyOf(routes), - new NoAuthenticationFilter(), - metricFactory); - } - - @Override - public void configure(HierarchicalConfiguration config) throws ConfigurationException { - if (configuration.isEnabled()) { - service.port(configuration.getPort().toInt()); - configureHTTPS(); - configureCORS(); - configureMetrics(); - service.before(authenticationFilter); - routesList.forEach(routes -> routes.define(service)); - service.awaitInitialization(); - LOGGER.info("Web admin server started"); - } - } - - private void configureMetrics() { - service.before(new MetricPreFilter(metricFactory)); - service.after(new MetricPostFilter()); - } - - private void configureHTTPS() { - if (configuration.isTlsEnabled()) { - TlsConfiguration tlsConfiguration = configuration.getTlsConfiguration(); - service.secure(tlsConfiguration.getKeystoreFilePath(), - tlsConfiguration.getKeystorePassword(), - tlsConfiguration.getTruststoreFilePath(), - tlsConfiguration.getTruststorePassword()); - LOGGER.info("Web admin set up to use HTTPS"); - } - } - - private void configureCORS() { - if (configuration.isEnabled()) { - service.before(new CORSFilter(configuration.getUrlCORSOrigin())); - new CORSRoute().define(service); - LOGGER.info("Web admin set up to enable CORS from " + configuration.getUrlCORSOrigin()); - } - } - - @PreDestroy - public void destroy() { - if (configuration.isEnabled()) { - service.stop(); - LOGGER.info("Web admin server stopped"); - } - } - - public void await() { - service.awaitInitialization(); - } - - public Port getPort() { - return configuration.getPort(); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/authentication/AuthenticationFilter.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/authentication/AuthenticationFilter.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/authentication/AuthenticationFilter.java deleted file mode 100644 index f73b818..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/authentication/AuthenticationFilter.java +++ /dev/null @@ -1,25 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.authentication; - -import spark.Filter; - -public interface AuthenticationFilter extends Filter { -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/authentication/JwtFilter.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/authentication/JwtFilter.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/authentication/JwtFilter.java deleted file mode 100644 index 4832d6f..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/authentication/JwtFilter.java +++ /dev/null @@ -1,76 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.authentication; - -import static spark.Spark.halt; - -import java.util.Optional; - -import javax.inject.Inject; - -import org.apache.james.jwt.JwtTokenVerifier; - -import spark.Request; -import spark.Response; - -public class JwtFilter implements AuthenticationFilter { - public static final String AUTHORIZATION_HEADER_PREFIX = "Bearer "; - public static final String AUTHORIZATION_HEADER_NAME = "Authorization"; - public static final String OPTIONS = "OPTIONS"; - - private final JwtTokenVerifier jwtTokenVerifier; - - @Inject - public JwtFilter(JwtTokenVerifier jwtTokenVerifier) { - this.jwtTokenVerifier = jwtTokenVerifier; - } - - @Override - public void handle(Request request, Response response) throws Exception { - if (request.requestMethod() != OPTIONS) { - Optional<String> bearer = Optional.ofNullable(request.headers(AUTHORIZATION_HEADER_NAME)) - .filter(value -> value.startsWith(AUTHORIZATION_HEADER_PREFIX)) - .map(value -> value.substring(AUTHORIZATION_HEADER_PREFIX.length())); - - checkHeaderPresent(bearer); - checkValidSignature(bearer); - checkIsAdmin(bearer); - } - } - - private void checkHeaderPresent(Optional<String> bearer) { - if (!bearer.isPresent()) { - halt(401, "No Bearer header."); - } - } - - private void checkValidSignature(Optional<String> bearer) { - if (!jwtTokenVerifier.verify(bearer.get())) { - halt(401, "Invalid Bearer header."); - } - } - - private void checkIsAdmin(Optional<String> bearer) { - if (!jwtTokenVerifier.hasAttribute("admin", true, bearer.get())) { - halt(401, "Non authorized user."); - } - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/authentication/NoAuthenticationFilter.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/authentication/NoAuthenticationFilter.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/authentication/NoAuthenticationFilter.java deleted file mode 100644 index 45a2f5b..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/authentication/NoAuthenticationFilter.java +++ /dev/null @@ -1,31 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.authentication; - -import spark.Request; -import spark.Response; - -public class NoAuthenticationFilter implements AuthenticationFilter { - - @Override - public void handle(Request request, Response response) throws Exception { - - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/AddUserRequest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/AddUserRequest.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/AddUserRequest.java deleted file mode 100644 index 1d102c2..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/AddUserRequest.java +++ /dev/null @@ -1,39 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.dto; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.google.common.base.Preconditions; - -public class AddUserRequest { - - private final char[] password; - - @JsonCreator - public AddUserRequest(@JsonProperty("password") char[] password) { - Preconditions.checkNotNull(password); - this.password = password; - } - - public char[] getPassword() { - return password; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/MailboxResponse.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/MailboxResponse.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/MailboxResponse.java deleted file mode 100644 index b552f45..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/MailboxResponse.java +++ /dev/null @@ -1,33 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.dto; - -public class MailboxResponse { - - private final String mailboxName; - - public MailboxResponse(String mailboxName) { - this.mailboxName = mailboxName; - } - - public String getMailboxName() { - return mailboxName; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/QuotaDTO.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/QuotaDTO.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/QuotaDTO.java deleted file mode 100644 index b3403ae..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/QuotaDTO.java +++ /dev/null @@ -1,73 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - - -package org.apache.james.webadmin.dto; - -import org.apache.james.mailbox.model.Quota; - -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; -import com.google.common.base.Preconditions; - -@JsonDeserialize(builder = QuotaDTO.Builder.class) -public class QuotaDTO { - public static Builder builder() { - return new Builder(); - } - - @JsonPOJOBuilder(withPrefix="") - public static class Builder { - private long count; - private long size; - - public Builder count(long count) { - this.count = count; - return this; - } - - public Builder size(long size) { - this.size = size; - return this; - } - - public QuotaDTO build() { - return new QuotaDTO(count, size); - } - - } - - private final long count; - private final long size; - - private QuotaDTO(long count, long size) { - Preconditions.checkArgument(count >= Quota.UNLIMITED); - Preconditions.checkArgument(size >= Quota.UNLIMITED); - this.count = count; - this.size = size; - } - - public long getCount() { - return count; - } - - public long getSize() { - return size; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/QuotaRequest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/QuotaRequest.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/QuotaRequest.java deleted file mode 100644 index 7ae9e85..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/QuotaRequest.java +++ /dev/null @@ -1,39 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.dto; - -import com.google.common.base.Preconditions; - -public class QuotaRequest { - public static QuotaRequest parse(String serialized) { - return new QuotaRequest(Long.valueOf(serialized)); - } - - private final long value; - - public QuotaRequest(long value) { - Preconditions.checkArgument(value >= 0); - this.value = value; - } - - public long getValue() { - return value; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/UserResponse.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/UserResponse.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/UserResponse.java deleted file mode 100644 index 5fe1846..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/dto/UserResponse.java +++ /dev/null @@ -1,33 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.dto; - -public class UserResponse { - - private final String username; - - public UserResponse(String username) { - this.username = username; - } - - public String getUsername() { - return username; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/metric/MetricPostFilter.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/metric/MetricPostFilter.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/metric/MetricPostFilter.java deleted file mode 100644 index 4d9b8b7..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/metric/MetricPostFilter.java +++ /dev/null @@ -1,37 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.metric; - -import org.apache.james.metrics.api.TimeMetric; - -import spark.Filter; -import spark.Request; -import spark.Response; - -public class MetricPostFilter implements Filter { - - @Override - public void handle(Request request, Response response) throws Exception { - if (request.attribute(MetricPreFilter.METRICS) instanceof TimeMetric) { - TimeMetric timeMetric = request.attribute(MetricPreFilter.METRICS); - timeMetric.stopAndPublish(); - } - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/metric/MetricPreFilter.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/metric/MetricPreFilter.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/metric/MetricPreFilter.java deleted file mode 100644 index 1ed9d76..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/metric/MetricPreFilter.java +++ /dev/null @@ -1,41 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - - -package org.apache.james.webadmin.metric; - -import org.apache.james.metrics.api.MetricFactory; - -import spark.Filter; -import spark.Request; -import spark.Response; - -public class MetricPreFilter implements Filter { - public static final String METRICS = "metrics"; - private final MetricFactory metricFactory; - - public MetricPreFilter(MetricFactory metricFactory) { - this.metricFactory = metricFactory; - } - - @Override - public void handle(Request request, Response response) throws Exception { - request.attribute(METRICS, metricFactory.timer("webAdmin")); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/CORSRoute.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/CORSRoute.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/CORSRoute.java deleted file mode 100644 index 39e9bd7..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/CORSRoute.java +++ /dev/null @@ -1,42 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.routes; - -import org.apache.james.webadmin.Routes; - -import spark.Service; - -public class CORSRoute implements Routes { - - @Override - public void define(Service service) { - service.options("/*", (request, response) -> { - String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers"); - if (accessControlRequestHeaders != null) { - response.header("Access-Control-Allow-Headers", accessControlRequestHeaders); - } - String accessControlRequestMethod = request.headers("Access-Control-Request-Method"); - if (accessControlRequestMethod != null) { - response.header("Access-Control-Allow-Methods", accessControlRequestMethod); - } - return ""; - }); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/DomainRoutes.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/DomainRoutes.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/DomainRoutes.java deleted file mode 100644 index 2008278..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/DomainRoutes.java +++ /dev/null @@ -1,118 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.routes; - -import static org.apache.james.webadmin.Constants.SEPARATOR; - -import javax.inject.Inject; - -import org.apache.james.domainlist.api.DomainList; -import org.apache.james.domainlist.api.DomainListException; -import org.apache.james.webadmin.Constants; -import org.apache.james.webadmin.Routes; -import org.apache.james.webadmin.utils.JsonTransformer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; - -import spark.Request; -import spark.Response; -import spark.Service; - -public class DomainRoutes implements Routes { - - private static final String DOMAIN_NAME = ":domainName"; - private static final Logger LOGGER = LoggerFactory.getLogger(DomainRoutes.class); - - public static final String DOMAINS = "/domains"; - public static final String SPECIFIC_DOMAIN = DOMAINS + SEPARATOR + DOMAIN_NAME; - public static final int MAXIMUM_DOMAIN_SIZE = 256; - - - private final DomainList domainList; - private final JsonTransformer jsonTransformer; - - @Inject - public DomainRoutes(DomainList domainList, JsonTransformer jsonTransformer) { - this.domainList = domainList; - this.jsonTransformer = jsonTransformer; - } - - @Override - public void define(Service service) { - service.get(DOMAINS, - (request, response) -> domainList.getDomains(), - jsonTransformer); - - service.get(SPECIFIC_DOMAIN, this::exists); - - service.put(SPECIFIC_DOMAIN, this::addDomain); - - service.delete(SPECIFIC_DOMAIN, this::removeDomain); - } - - private String removeDomain(Request request, Response response) { - try { - String domain = request.params(DOMAIN_NAME); - removeDomain(domain); - } catch (DomainListException e) { - LOGGER.info("{} did not exists", request.params(DOMAIN_NAME)); - } - response.status(204); - return Constants.EMPTY_BODY; - } - - private void removeDomain(String domain) throws DomainListException { - Preconditions.checkArgument(!Strings.isNullOrEmpty(domain)); - domainList.removeDomain(domain); - } - - private String addDomain(Request request, Response response) { - try { - addDomain(request.params(DOMAIN_NAME)); - response.status(204); - } catch (DomainListException e) { - LOGGER.info("{} already exists", request.params(DOMAIN_NAME)); - response.status(204); - } catch (IllegalArgumentException e) { - LOGGER.info("Invalid request for domain creation"); - response.status(400); - } - return Constants.EMPTY_BODY; - } - - private void addDomain(String domain) throws DomainListException { - Preconditions.checkArgument(!Strings.isNullOrEmpty(domain)); - Preconditions.checkArgument(!domain.contains("@")); - Preconditions.checkArgument(domain.length() < MAXIMUM_DOMAIN_SIZE); - domainList.addDomain(domain); - } - - private String exists(Request request, Response response) throws DomainListException { - if (!domainList.containsDomain(request.params(DOMAIN_NAME))) { - response.status(404); - } else { - response.status(204); - } - return Constants.EMPTY_BODY; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/GlobalQuotaRoutes.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/GlobalQuotaRoutes.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/GlobalQuotaRoutes.java deleted file mode 100644 index eeca83a..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/GlobalQuotaRoutes.java +++ /dev/null @@ -1,130 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.routes; - -import javax.inject.Inject; - -import org.apache.james.mailbox.model.Quota; -import org.apache.james.mailbox.quota.MaxQuotaManager; -import org.apache.james.webadmin.Constants; -import org.apache.james.webadmin.Routes; -import org.apache.james.webadmin.dto.QuotaDTO; -import org.apache.james.webadmin.dto.QuotaRequest; -import org.apache.james.webadmin.utils.JsonExtractException; -import org.apache.james.webadmin.utils.JsonExtractor; -import org.apache.james.webadmin.utils.JsonTransformer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import spark.Service; - -public class GlobalQuotaRoutes implements Routes { - - public static final String QUOTA_ENDPOINT = "/quota"; - public static final String COUNT_ENDPOINT = QUOTA_ENDPOINT + "/count"; - public static final String SIZE_ENDPOINT = QUOTA_ENDPOINT + "/size"; - private static final Logger LOGGER = LoggerFactory.getLogger(Routes.class); - - private final MaxQuotaManager maxQuotaManager; - private final JsonTransformer jsonTransformer; - private final JsonExtractor<QuotaDTO> jsonExtractor; - - @Inject - public GlobalQuotaRoutes(MaxQuotaManager maxQuotaManager, JsonTransformer jsonTransformer) { - this.maxQuotaManager = maxQuotaManager; - this.jsonTransformer = jsonTransformer; - this.jsonExtractor = new JsonExtractor<>(QuotaDTO.class); - } - - @Override - public void define(Service service) { - service.get(COUNT_ENDPOINT, (request, response) -> { - long value = maxQuotaManager.getDefaultMaxMessage(); - response.status(200); - return value; - }, jsonTransformer); - - service.delete(COUNT_ENDPOINT, (request, response) -> { - maxQuotaManager.setDefaultMaxMessage(Quota.UNLIMITED); - response.status(204); - return Constants.EMPTY_BODY; - }); - - service.put(COUNT_ENDPOINT, (request, response) -> { - try { - QuotaRequest quotaRequest = QuotaRequest.parse(request.body()); - maxQuotaManager.setDefaultMaxMessage(quotaRequest.getValue()); - response.status(204); - } catch (IllegalArgumentException e) { - LOGGER.info("Invalid quota. Need to be an integer value greater than 0"); - response.status(400); - } - return Constants.EMPTY_BODY; - }); - - service.get(SIZE_ENDPOINT, (request, response) -> { - long value = maxQuotaManager.getDefaultMaxStorage(); - response.status(200); - return value; - }, jsonTransformer); - - service.delete(SIZE_ENDPOINT, (request, response) -> { - maxQuotaManager.setDefaultMaxStorage(Quota.UNLIMITED); - response.status(204); - return Constants.EMPTY_BODY; - }); - - service.put(SIZE_ENDPOINT, (request, response) -> { - try { - QuotaRequest quotaRequest = QuotaRequest.parse(request.body()); - maxQuotaManager.setDefaultMaxStorage(quotaRequest.getValue()); - response.status(204); - } catch (IllegalArgumentException e) { - LOGGER.info("Invalid quota. Need to be an integer value greater than 0"); - response.status(400); - } - return Constants.EMPTY_BODY; - }); - - service.get(QUOTA_ENDPOINT, (request, response) -> { - QuotaDTO quotaDTO = QuotaDTO.builder() - .count(maxQuotaManager.getDefaultMaxMessage()) - .size(maxQuotaManager.getDefaultMaxStorage()).build(); - response.status(200); - return quotaDTO; - }, jsonTransformer); - - service.put(QUOTA_ENDPOINT, ((request, response) -> { - try { - QuotaDTO quotaDTO = jsonExtractor.parse(request.body()); - maxQuotaManager.setDefaultMaxMessage(quotaDTO.getCount()); - maxQuotaManager.setDefaultMaxStorage(quotaDTO.getSize()); - response.status(204); - } catch (JsonExtractException e) { - LOGGER.info("Malformed JSON", e); - response.status(400); - } catch (IllegalArgumentException e) { - LOGGER.info("Quota should be positive or unlimited (-1)", e); - response.status(400); - } - return Constants.EMPTY_BODY; - })); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/UserMailboxesRoutes.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/UserMailboxesRoutes.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/UserMailboxesRoutes.java deleted file mode 100644 index 717ef2b..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/UserMailboxesRoutes.java +++ /dev/null @@ -1,128 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.routes; - -import javax.inject.Inject; - -import org.apache.james.webadmin.Constants; -import org.apache.james.webadmin.Routes; -import org.apache.james.webadmin.service.UserMailboxesService; -import org.apache.james.webadmin.utils.JsonTransformer; -import org.apache.james.webadmin.utils.MailboxHaveChildrenException; -import org.apache.james.webadmin.validation.MailboxName; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import spark.Service; - -public class UserMailboxesRoutes implements Routes { - - private static final Logger LOGGER = LoggerFactory.getLogger(UserMailboxesRoutes.class); - - public static final String MAILBOX_NAME = ":mailboxName"; - public static final String MAILBOXES = "mailboxes"; - private static final String USER_NAME = ":userName"; - public static final String USER_MAILBOXES_BASE = UserRoutes.USERS + Constants.SEPARATOR + USER_NAME + Constants.SEPARATOR + MAILBOXES; - public static final String SPECIFIC_MAILBOX = USER_MAILBOXES_BASE + Constants.SEPARATOR + MAILBOX_NAME; - - private final UserMailboxesService userMailboxesService; - private final JsonTransformer jsonTransformer; - - @Inject - public UserMailboxesRoutes(UserMailboxesService userMailboxesService, JsonTransformer jsonTransformer) { - this.userMailboxesService = userMailboxesService; - this.jsonTransformer = jsonTransformer; - } - - @Override - public void define(Service service) { - - service.put(SPECIFIC_MAILBOX, (request, response) -> { - try { - userMailboxesService.createMailbox(request.params(USER_NAME), new MailboxName(request.params(MAILBOX_NAME))); - response.status(204); - } catch (IllegalStateException e) { - LOGGER.info("Invalid put on user mailbox", e); - response.status(404); - } catch (IllegalArgumentException e) { - LOGGER.info("Attempt to create an invalid mailbox"); - response.status(400); - } - return Constants.EMPTY_BODY; - }); - - service.delete(SPECIFIC_MAILBOX, (request, response) -> { - try { - userMailboxesService.deleteMailbox(request.params(USER_NAME), new MailboxName(request.params(MAILBOX_NAME))); - response.status(204); - } catch (IllegalStateException e) { - LOGGER.info("Invalid delete on user mailbox", e); - response.status(404); - } catch (MailboxHaveChildrenException e) { - LOGGER.info("Attempt to delete a mailbox with children"); - response.status(409); - } catch (IllegalArgumentException e) { - LOGGER.info("Attempt to create an invalid mailbox"); - response.status(400); - } - return Constants.EMPTY_BODY; - }); - - service.delete(USER_MAILBOXES_BASE, (request, response) -> { - try { - userMailboxesService.deleteMailboxes(request.params(USER_NAME)); - response.status(204); - } catch (IllegalStateException e) { - LOGGER.info("Invalid delete on user mailboxes", e); - response.status(404); - } - return Constants.EMPTY_BODY; - }); - - service.get(SPECIFIC_MAILBOX, (request, response) -> { - try { - if (userMailboxesService.testMailboxExists(request.params(USER_NAME), new MailboxName(request.params(MAILBOX_NAME)))) { - response.status(204); - } else { - response.status(404); - } - } catch (IllegalStateException e) { - LOGGER.info("Invalid get on user mailbox", e); - response.status(404); - } catch (IllegalArgumentException e) { - LOGGER.info("Attempt to create an invalid mailbox"); - response.status(400); - } - return Constants.EMPTY_BODY; - }); - - service.get(USER_MAILBOXES_BASE, (request, response) -> { - response.status(200); - try { - return userMailboxesService.listMailboxes(request.params(USER_NAME)); - } catch (IllegalStateException e) { - LOGGER.info("Invalid get on user mailboxes", e); - response.status(404); - return Constants.EMPTY_BODY; - } - }, jsonTransformer); - - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/UserRoutes.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/UserRoutes.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/UserRoutes.java deleted file mode 100644 index 0ead90a..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/routes/UserRoutes.java +++ /dev/null @@ -1,104 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.routes; - -import static org.apache.james.webadmin.Constants.SEPARATOR; - -import javax.inject.Inject; - -import org.apache.james.user.api.UsersRepositoryException; -import org.apache.james.webadmin.Constants; -import org.apache.james.webadmin.Routes; -import org.apache.james.webadmin.dto.AddUserRequest; -import org.apache.james.webadmin.service.UserService; -import org.apache.james.webadmin.utils.JsonExtractException; -import org.apache.james.webadmin.utils.JsonExtractor; -import org.apache.james.webadmin.utils.JsonTransformer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import spark.Request; -import spark.Response; -import spark.Service; - -public class UserRoutes implements Routes { - - private static final String USER_NAME = ":userName"; - private static final Logger LOGGER = LoggerFactory.getLogger(UserRoutes.class); - - public static final String USERS = "/users"; - - private final UserService userService; - private final JsonTransformer jsonTransformer; - private final JsonExtractor<AddUserRequest> jsonExtractor; - - @Inject - public UserRoutes(UserService userService, JsonTransformer jsonTransformer) { - this.userService = userService; - this.jsonTransformer = jsonTransformer; - this.jsonExtractor = new JsonExtractor<>(AddUserRequest.class); - } - - @Override - public void define(Service service) { - service.get(USERS, - (request, response) -> userService.getUsers(), - jsonTransformer); - - service.put(USERS + SEPARATOR + USER_NAME, this::upsertUser); - - service.delete(USERS + SEPARATOR + USER_NAME, this::removeUser); - } - - private String removeUser(Request request, Response response) { - String username = request.params(USER_NAME); - try { - userService.removeUser(username); - response.status(204); - return Constants.EMPTY_BODY; - } catch (UsersRepositoryException e) { - response.status(204); - return "The user " + username + " does not exists"; - } catch (IllegalArgumentException e) { - LOGGER.info("Invalid user path", e); - response.status(400); - return Constants.EMPTY_BODY; - } - } - - private String upsertUser(Request request, Response response) throws UsersRepositoryException { - try { - return userService.upsertUser(request.params(USER_NAME), - jsonExtractor.parse(request.body()).getPassword(), - response); - } catch (JsonExtractException e) { - LOGGER.info("Error while deserializing addUser request", e); - response.status(400); - return Constants.EMPTY_BODY; - } catch (IllegalArgumentException e) { - LOGGER.info("Invalid user path", e); - response.status(400); - return Constants.EMPTY_BODY; - } - } - - - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/service/UserMailboxesService.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/service/UserMailboxesService.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/service/UserMailboxesService.java deleted file mode 100644 index 469bdfb..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/service/UserMailboxesService.java +++ /dev/null @@ -1,143 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.service; - -import java.util.List; -import java.util.stream.Stream; - -import javax.inject.Inject; - -import org.apache.james.mailbox.MailboxManager; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.exception.MailboxExistsException; -import org.apache.james.mailbox.exception.MailboxNotFoundException; -import org.apache.james.mailbox.model.MailboxMetaData; -import org.apache.james.mailbox.model.MailboxPath; -import org.apache.james.mailbox.model.MailboxQuery; -import org.apache.james.user.api.UsersRepository; -import org.apache.james.user.api.UsersRepositoryException; -import org.apache.james.webadmin.dto.MailboxResponse; -import org.apache.james.webadmin.utils.MailboxHaveChildrenException; -import org.apache.james.webadmin.validation.MailboxName; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.github.fge.lambdas.Throwing; -import com.github.steveash.guavate.Guavate; -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; - - -public class UserMailboxesService { - - private static final Logger LOGGER = LoggerFactory.getLogger(UserMailboxesService.class); - private static final String USER_NAME = "webAdmin"; - - private final MailboxManager mailboxManager; - private final UsersRepository usersRepository; - - @Inject - public UserMailboxesService(MailboxManager mailboxManager, UsersRepository usersRepository) { - this.mailboxManager = mailboxManager; - this.usersRepository = usersRepository; - } - - public void createMailbox(String username, MailboxName mailboxName) throws MailboxException, UsersRepositoryException { - usernamePreconditions(username); - MailboxSession mailboxSession = mailboxManager.createSystemSession(USER_NAME, LOGGER); - try { - mailboxManager.createMailbox( - convertToMailboxPath(username, mailboxName.asString(), mailboxSession), - mailboxSession); - } catch (MailboxExistsException e) { - LOGGER.info("Attempt to create mailbox {} for user {} that already exists", mailboxName, username); - } - } - - public void deleteMailboxes(String username) throws MailboxException, UsersRepositoryException { - usernamePreconditions(username); - MailboxSession mailboxSession = mailboxManager.createSystemSession(USER_NAME, LOGGER); - listUserMailboxes(username, mailboxSession) - .map(MailboxMetaData::getPath) - .forEach(Throwing.consumer(mailboxPath -> deleteMailbox(mailboxSession, mailboxPath))); - } - - public List<MailboxResponse> listMailboxes(String username) throws MailboxException, UsersRepositoryException { - usernamePreconditions(username); - MailboxSession mailboxSession = mailboxManager.createSystemSession(USER_NAME, LOGGER); - return listUserMailboxes(username, mailboxSession) - .map(mailboxMetaData -> new MailboxResponse(mailboxMetaData.getPath().getName())) - .collect(Guavate.toImmutableList()); - } - - public boolean testMailboxExists(String username, MailboxName mailboxName) throws MailboxException, UsersRepositoryException { - usernamePreconditions(username); - MailboxSession mailboxSession = mailboxManager.createSystemSession(USER_NAME, LOGGER); - return mailboxManager.mailboxExists( - convertToMailboxPath(username, mailboxName.asString(), mailboxSession), - mailboxSession); - } - - public void deleteMailbox(String username, MailboxName mailboxName) throws MailboxException, UsersRepositoryException, MailboxHaveChildrenException { - usernamePreconditions(username); - MailboxSession mailboxSession = mailboxManager.createSystemSession(USER_NAME, LOGGER); - MailboxPath mailboxPath = convertToMailboxPath(username, mailboxName.asString(), mailboxSession); - listChildren(mailboxPath, mailboxSession) - .forEach(Throwing.consumer(path -> deleteMailbox(mailboxSession, path))); - } - - private Stream<MailboxPath> listChildren(MailboxPath mailboxPath, MailboxSession mailboxSession) throws MailboxException { - return mailboxManager.search(createUserMailboxesQuery(mailboxPath.getUser()), mailboxSession) - .stream() - .map(MailboxMetaData::getPath) - .filter(path -> path.getHierarchyLevels(mailboxSession.getPathDelimiter()).contains(mailboxPath)); - } - - private void deleteMailbox(MailboxSession mailboxSession, MailboxPath mailboxPath) throws MailboxException { - try { - mailboxManager.deleteMailbox(mailboxPath, mailboxSession); - } catch (MailboxNotFoundException e) { - LOGGER.info("Attempt to delete mailbox {} for user {} that does not exists", mailboxPath.getName(), mailboxPath.getUser()); - } - } - - private void usernamePreconditions(String username) throws UsersRepositoryException { - Preconditions.checkArgument(!Strings.isNullOrEmpty(username)); - Preconditions.checkState(usersRepository.contains(username)); - } - - private MailboxPath convertToMailboxPath(String username, String mailboxName, MailboxSession mailboxSession) { - return new MailboxPath(mailboxSession.getPersonalSpace(), username, mailboxName); - } - - private Stream<MailboxMetaData> listUserMailboxes(String username, MailboxSession mailboxSession) throws MailboxException { - return mailboxManager.search(createUserMailboxesQuery(username), mailboxSession) - .stream(); - } - - private MailboxQuery createUserMailboxesQuery(String username) { - return MailboxQuery.builder() - .username(username) - .privateUserMailboxes() - .build(); - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/service/UserService.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/service/UserService.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/service/UserService.java deleted file mode 100644 index ee0952d..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/service/UserService.java +++ /dev/null @@ -1,94 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.service; - -import java.util.List; -import java.util.Optional; -import java.util.stream.Stream; - -import javax.inject.Inject; - -import org.apache.james.user.api.UsersRepository; -import org.apache.james.user.api.UsersRepositoryException; -import org.apache.james.user.api.model.User; -import org.apache.james.util.streams.Iterators; -import org.apache.james.webadmin.dto.UserResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.github.steveash.guavate.Guavate; -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; - -import spark.Response; - -public class UserService { - - private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class); - private static final String EMPTY_BODY = ""; - public static final int MAXIMUM_MAIL_ADDRESS_LENGTH = 255; - - private final UsersRepository usersRepository; - - @Inject - public UserService(UsersRepository usersRepository) { - this.usersRepository = usersRepository; - } - - public List<UserResponse> getUsers() throws UsersRepositoryException { - return Optional.ofNullable(usersRepository.list()) - .map(Iterators::toStream) - .orElse(Stream.of()) - .map(UserResponse::new) - .collect(Guavate.toImmutableList()); - } - - public void removeUser(String username) throws UsersRepositoryException { - usernamePreconditions(username); - usersRepository.removeUser(username); - } - - public String upsertUser(String username, char[] password, Response response) throws UsersRepositoryException { - usernamePreconditions(username); - User user = usersRepository.getUserByName(username); - try { - upsert(user, username, password); - response.status(204); - } catch (UsersRepositoryException e) { - LOGGER.info("Error creating or updating user : {}", e.getMessage()); - response.status(409); - } - return EMPTY_BODY; - } - - private void usernamePreconditions(String username) { - Preconditions.checkArgument(!Strings.isNullOrEmpty(username)); - Preconditions.checkArgument(username.length() < MAXIMUM_MAIL_ADDRESS_LENGTH); - } - - private void upsert(User user, String username, char[] password) throws UsersRepositoryException { - if (user == null) { - usersRepository.addUser(username, new String(password)); - } else { - user.setPassword(new String(password)); - usersRepository.updateUser(user); - } - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonExtractException.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonExtractException.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonExtractException.java deleted file mode 100644 index dc6b578..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonExtractException.java +++ /dev/null @@ -1,27 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.utils; - -public class JsonExtractException extends Exception { - - public JsonExtractException(Throwable throwable) { - super(throwable); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonExtractor.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonExtractor.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonExtractor.java deleted file mode 100644 index f4eb420..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonExtractor.java +++ /dev/null @@ -1,44 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.utils; - -import java.io.IOException; - -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JsonExtractor<Request> { - - private final ObjectMapper objectMapper; - private final Class<Request> type; - - public JsonExtractor(Class<Request> type) { - this.objectMapper = new ObjectMapper(); - this.type = type; - } - - public Request parse(String text) throws JsonExtractException { - try { - return objectMapper.readValue(text, type); - } catch (IOException e) { - throw new JsonExtractException(e); - } - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonTransformer.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonTransformer.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonTransformer.java deleted file mode 100644 index 3c32f0c..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonTransformer.java +++ /dev/null @@ -1,41 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.utils; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; - -import spark.ResponseTransformer; - -public class JsonTransformer implements ResponseTransformer { - - private final ObjectMapper objectMapper; - - public JsonTransformer() { - objectMapper = new ObjectMapper(); - objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); - } - - @Override - public String render(Object o) throws JsonProcessingException { - return objectMapper.writeValueAsString(o); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/MailboxHaveChildrenException.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/MailboxHaveChildrenException.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/MailboxHaveChildrenException.java deleted file mode 100644 index ae8213e..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/MailboxHaveChildrenException.java +++ /dev/null @@ -1,27 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.utils; - -public class MailboxHaveChildrenException extends Exception { - - public MailboxHaveChildrenException(String mailboxName) { - super(mailboxName + "have children"); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/validation/MailboxName.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/validation/MailboxName.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/validation/MailboxName.java deleted file mode 100644 index fdd079d..0000000 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/validation/MailboxName.java +++ /dev/null @@ -1,40 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin.validation; - -import com.google.common.base.CharMatcher; -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; - -public class MailboxName { - - public static final CharMatcher INVALID_CHARS_MATCHER = CharMatcher.anyOf("%*&#"); - private final String mailboxName; - - public MailboxName(String mailboxName) { - Preconditions.checkArgument(!Strings.isNullOrEmpty(mailboxName)); - Preconditions.checkArgument(INVALID_CHARS_MATCHER.matchesNoneOf(mailboxName)); - this.mailboxName = mailboxName; - } - - public String asString() { - return mailboxName; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/cca0f398/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java b/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java deleted file mode 100644 index 2712daa..0000000 --- a/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.webadmin; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import org.junit.Test; - -import nl.jqno.equalsverifier.EqualsVerifier; - -public class FixedPortTest { - - @Test - public void toIntShouldThrowOnNegativePort() { - assertThatThrownBy(() -> new FixedPort(-1)).isInstanceOf(IllegalArgumentException.class); - } - - @Test - public void toIntShouldThrowOnNullPort() { - assertThatThrownBy(() -> new FixedPort(0)).isInstanceOf(IllegalArgumentException.class); - } - - @Test - public void toIntShouldThrowOnTooBigNumbers() { - assertThatThrownBy(() -> new FixedPort(65536)).isInstanceOf(IllegalArgumentException.class); - } - - @Test - public void toIntShouldReturnedDesiredPort() { - int expectedPort = 452; - assertThat(new FixedPort(expectedPort).toInt()).isEqualTo(expectedPort); - } - - @Test - public void shouldMatchBeanContract() { - EqualsVerifier.forClass(FixedPort.class).verify(); - } - -} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org