Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

public class BootstrAPI {

public static final String ALL = "all";
public static final String _ALL = "_all";
public static final String _ROOT = "/";

public static final String APPLICATION = "application";
public static final String APPLICATIONS = "applications";
public static final String APPLICATION_LINK = "application-link";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.deftdevs.bootstrapi.commons.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Map;

import static com.deftdevs.bootstrapi.commons.constants.BootstrAPI.AUTHENTICATION;

@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement(name = AUTHENTICATION)
public class AuthenticationModel {

@XmlElement
private Map<String, AbstractAuthenticationIdpModel> idps;

@XmlElement
private AuthenticationSsoModel sso;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.deftdevs.bootstrapi.commons.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import static com.deftdevs.bootstrapi.commons.constants.BootstrAPI.MAIL_SERVER;

@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement(name = MAIL_SERVER)
public class MailServerModel {

@XmlElement
private MailServerSmtpModel smtp;

@XmlElement
private MailServerPopModel pop;

// Example instances for documentation and tests

public static final MailServerModel EXAMPLE_1 = new MailServerModel(
MailServerSmtpModel.EXAMPLE_1,
MailServerPopModel.EXAMPLE_1
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@AllArgsConstructor
@XmlRootElement(name = BootstrAPI.SETTINGS)
@XmlAccessorType(XmlAccessType.FIELD)
public class SettingsModel {
public class SettingsGeneralModel {

@XmlElement
private URI baseUrl;
Expand All @@ -46,15 +46,15 @@ public String getMode() {

// Example instances for documentation and tests

public static final SettingsModel EXAMPLE_1 = SettingsModel.builder()
public static final SettingsGeneralModel EXAMPLE_1 = SettingsGeneralModel.builder()
.title("Example")
.baseUrl(URI.create("https://example.com"))
.mode("private")
.contactMessage("Test Message")
.externalUserManagement(true)
.build();

public static final SettingsModel EXAMPLE_1_NO_MODE = SettingsModel.builder()
public static final SettingsGeneralModel EXAMPLE_1_NO_MODE = SettingsGeneralModel.builder()
.title("Example")
.baseUrl(URI.create("https://example.com"))
.mode(null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.deftdevs.bootstrapi.commons.model.type;

import java.util.Map;

public interface _AllModelAccessor {

Map<String, _AllModelStatus> getStatus();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.deftdevs.bootstrapi.commons.model.type;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement(name = "status")
public class _AllModelStatus {

@XmlElement
private int status;

@XmlElement
private String message;

@XmlElement
private String details;

public static _AllModelStatus success() {
return new _AllModelStatus(Response.Status.OK.getStatusCode(), "Success", null);
}

public static _AllModelStatus error(Response.Status status, String message, String details) {
return new _AllModelStatus(status.getStatusCode(), message, details);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.deftdevs.bootstrapi.commons.rest;

import com.deftdevs.bootstrapi.commons.model.SettingsGeneralModel;
import com.deftdevs.bootstrapi.commons.rest.api.SettingsGeneralResource;
import com.deftdevs.bootstrapi.commons.service.api.SettingsGeneralService;

import javax.ws.rs.core.Response;

public abstract class AbstractSettingsGeneralResourceImpl<B extends SettingsGeneralModel, S extends SettingsGeneralService<B>>
implements SettingsGeneralResource<B> {

private final S settingsService;

public AbstractSettingsGeneralResourceImpl(
final S settingsService) {

this.settingsService = settingsService;
}

@Override
public Response getSettings() {
final B settingsModel = settingsService.getSettingsGeneral();
return Response.ok(settingsModel).build();
}

@Override
public Response setSettings(B settingsModel) {
final B updatedSettingsGeneralModel = settingsService.setSettingsGeneral(settingsModel);
return Response.ok(updatedSettingsGeneralModel).build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.deftdevs.bootstrapi.commons.rest;

import com.deftdevs.bootstrapi.commons.model.type._AllModelAccessor;
import com.deftdevs.bootstrapi.commons.model.type._AllModelStatus;
import com.deftdevs.bootstrapi.commons.rest.api._AllResource;
import com.deftdevs.bootstrapi.commons.service.api._AllService;

import javax.ws.rs.core.Response;
import java.util.Map;

public abstract class _AbstractAllResourceImpl<_AllModel extends _AllModelAccessor>
implements _AllResource<_AllModel> {

private final _AllService<_AllModel> allService;

public _AbstractAllResourceImpl(
final _AllService<_AllModel> allService) {

this.allService = allService;
}

public Response setAll(
final _AllModel allModel) {

final _AllModel result = allService.setAll(allModel);
final int overallStatus = computeOverallStatus(result.getStatus());
return Response.status(overallStatus).entity(result).build();
}

private static int computeOverallStatus(
final Map<String, _AllModelStatus> statusMap) {

if (statusMap == null || statusMap.isEmpty()) {
return Response.Status.OK.getStatusCode();
}

return statusMap.values().stream()
.mapToInt(_AllModelStatus::getStatus)
.max()
.orElse(Response.Status.OK.getStatusCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.deftdevs.bootstrapi.commons.constants.BootstrAPI;
import com.deftdevs.bootstrapi.commons.model.ErrorCollection;
import com.deftdevs.bootstrapi.commons.model.SettingsModel;
import com.deftdevs.bootstrapi.commons.model.SettingsGeneralModel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -12,14 +12,14 @@
import javax.ws.rs.PUT;
import javax.ws.rs.core.Response;

public interface SettingsResource<B extends SettingsModel> {
public interface SettingsGeneralResource<B extends SettingsGeneralModel> {

@GET
@Operation(
summary = BootstrAPI.SETTINGS_GENERAL_GET_SUMMARY,
responses = {
@ApiResponse(
responseCode = "200", content = @Content(schema = @Schema(implementation = SettingsModel.class)),
responseCode = "200", content = @Content(schema = @Schema(implementation = SettingsGeneralModel.class)),
description = BootstrAPI.SETTINGS_GENERAL_GET_RESPONSE_DESCRIPTION
),
@ApiResponse(
Expand All @@ -35,7 +35,7 @@ public interface SettingsResource<B extends SettingsModel> {
summary = BootstrAPI.SETTINGS_GENERAL_PUT_SUMMARY,
responses = {
@ApiResponse(
responseCode = "200", content = @Content(schema = @Schema(implementation = SettingsModel.class)),
responseCode = "200", content = @Content(schema = @Schema(implementation = SettingsGeneralModel.class)),
description = BootstrAPI.SETTINGS_GENERAL_PUT_RESPONSE_DESCRIPTION
),
@ApiResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.deftdevs.bootstrapi.commons.rest.api;

import com.deftdevs.bootstrapi.commons.constants.BootstrAPI;
import com.deftdevs.bootstrapi.commons.model.ErrorCollection;
import com.deftdevs.bootstrapi.commons.model.type._AllModelAccessor;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;

import javax.validation.constraints.NotNull;
import javax.ws.rs.PUT;
import javax.ws.rs.core.Response;

public interface _AllResource<_AllModel extends _AllModelAccessor> {

@PUT
@Operation(
summary = "Apply a complete configuration",
responses = {
@ApiResponse(
responseCode = "200",
description = "Configuration applied successfully"
),
@ApiResponse(
responseCode = "default", content = @Content(schema = @Schema(implementation = ErrorCollection.class)),
description = BootstrAPI.ERROR_COLLECTION_RESPONSE_DESCRIPTION
),
}
)
Response setAll(
@NotNull final _AllModel bean);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.deftdevs.bootstrapi.commons.service;

import com.deftdevs.bootstrapi.commons.model.type._AllModelAccessor;
import com.deftdevs.bootstrapi.commons.model.type._AllModelStatus;
import com.deftdevs.bootstrapi.commons.service.api._AllService;

import javax.ws.rs.core.Response;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

public abstract class _AbstractAllServiceImpl<_AllModel extends _AllModelAccessor> implements _AllService<_AllModel> {

protected <T> _AllModelStatus setEntity(
final String entityType,
final T entity,
final Function<T, T> updateFunction,
final Consumer<T> resultConsumer,
final Map<String, _AllModelStatus> statusMap) {

if (entity == null) {
return null;
}

try {
final T updatedEntity = updateFunction.apply(entity);
resultConsumer.accept(updatedEntity);
final _AllModelStatus status = _AllModelStatus.success();
statusMap.put(entityType, status);
return status;
} catch (Exception e) {
final _AllModelStatus status = _AllModelStatus.error(
resolveStatus(e),
String.format("Failed to apply %s configuration", entityType),
e.getMessage()
);
statusMap.put(entityType, status);
return status;
}
}

@SuppressWarnings("unchecked")
protected <T> _AllModelStatus setEntities(
final String entityType,
final Map<String, T> entityMap,
final Function<Map<String, T>, Map<String, ? extends T>> updateFunction,
final Consumer<Map<String, T>> resultConsumer,
final Map<String, _AllModelStatus> statusMap) {

if (entityMap == null || entityMap.isEmpty()) {
return null;
}

try {
final Map<String, ? extends T> updatedEntities = updateFunction.apply(entityMap);
resultConsumer.accept((Map<String, T>) updatedEntities);
final _AllModelStatus status = _AllModelStatus.success();
statusMap.put(entityType, status);
return status;
} catch (Exception e) {
final _AllModelStatus status = _AllModelStatus.error(
resolveStatus(e),
String.format("Failed to apply %s configuration", entityType),
e.getMessage()
);
statusMap.put(entityType, status);
return status;
}
}

private static Response.Status resolveStatus(final Exception e) {
if (e instanceof com.deftdevs.bootstrapi.commons.exception.web.BadRequestException) {
return Response.Status.BAD_REQUEST;
}
if (e instanceof com.deftdevs.bootstrapi.commons.exception.web.NotFoundException) {
return Response.Status.NOT_FOUND;
}
return Response.Status.INTERNAL_SERVER_ERROR;
}

}
Loading