Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • dawn/java/dawn-java-common
1 result
Show changes
Commits on Source (2)
package edu.umd.dawn.common.entities;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Field;
/**
* Location entity
*/
@Data
@Builder
@Deprecated
public class Location {
@Field("id")
@NotEmpty
private String id;
@NotNull
private Double latitude;
@NotNull
private Double longitude;
@NotEmpty
private String address;
@Field("display_name")
@JsonProperty("display_name")
private String displayName;
}
package edu.umd.dawn.common.entities;
import edu.umd.dawn.common.exceptions.DawnException;
import edu.umd.dawn.common.exceptions.DawnExceptionParameters;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.FieldType;
/**
* User entity - shared across all services
*/
@Data
@Document("users")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
@Field(targetType = FieldType.STRING)
private String id;
private String name;
private String email;
private String salt;
private byte[] hash;
private String hashVersion;
private Integer role;
@Field("newsletter_opt_in")
private Boolean newsletterOptIn;
@Field("last_logged_in")
private LocalDateTime lastLoggedIn;
@Field("last_token_refresh")
private LocalDateTime lastTokenRefresh;
@Field("created_at")
private LocalDateTime createdAt;
@Field("default_location")
@Deprecated
private Location defaultLocation;
@Field("favorite_locations")
@Deprecated
private List<Location> favoriteLocations;
@Field("locations")
private List<String> locations;
@Field("default_location_v2")
private String defaultLocationV2;
private Set<String> migrations;
public void addLocation(String locationId) {
List<String> aList = new ArrayList<>(locations);
aList.add(locationId);
setLocations(aList);
}
/**
* Will throw if id does not exist
* @param id
*/
public String removeFavoriteLocation(String id) {
List<String> newList =
locations.stream().filter(location -> !location.equals(id)).collect(Collectors.toList());
if (newList.size() == locations.size()) {
throw new DawnException(
new DawnExceptionParameters(400, "BAD_REQUEST", "location id provided does not exist", ""));
}
setLocations(newList);
return id;
}
}
package edu.umd.dawn.common.enums; package edu.umd.dawn.common.enums;
import edu.umd.dawn.common.exceptions.DawnException;
import edu.umd.dawn.common.exceptions.BaseExceptions; import edu.umd.dawn.common.exceptions.BaseExceptions;
import edu.umd.dawn.common.exceptions.DawnException;
import java.util.Map; import java.util.Map;
public enum Product { public enum Product {
......
...@@ -10,15 +10,15 @@ public class BaseExceptions { ...@@ -10,15 +10,15 @@ public class BaseExceptions {
public static final DawnExceptionParameters BAD_REQUEST = public static final DawnExceptionParameters BAD_REQUEST =
new DawnExceptionParameters(400, "BAD_REQUEST", "Invalid request submitted", ""); new DawnExceptionParameters(400, "BAD_REQUEST", "Invalid request submitted", "");
public static final DawnExceptionParameters INTERNAL_SERVER_ERROR = public static final DawnExceptionParameters INTERNAL_SERVER_ERROR =
new DawnExceptionParameters(500, "INTERNAL_SERVER_ERROR", "Internal server error", ""); new DawnExceptionParameters(500, "INTERNAL_SERVER_ERROR", "Internal server error", "");
public static final DawnExceptionParameters OUT_OF_BOUNDS = public static final DawnExceptionParameters OUT_OF_BOUNDS =
new DawnExceptionParameters(500, "INTERNAL_SERVER_ERROR", "Internal server error", "out of bounds"); new DawnExceptionParameters(500, "INTERNAL_SERVER_ERROR", "Internal server error", "out of bounds");
public static final DawnExceptionParameters UNHANDLED_INTERNAL_SERVER_ERROR = public static final DawnExceptionParameters UNHANDLED_INTERNAL_SERVER_ERROR =
new DawnExceptionParameters(500, "INTERNAL_SERVER_ERROR", "Internal server error", "unhandled error"); new DawnExceptionParameters(500, "INTERNAL_SERVER_ERROR", "Internal server error", "unhandled error");
public static final DawnExceptionParameters SERVICE_UNAVAILABLE = public static final DawnExceptionParameters SERVICE_UNAVAILABLE =
new DawnExceptionParameters(503, "SERVICE_UNAVAILABLE", "service unavailable", ""); new DawnExceptionParameters(503, "SERVICE_UNAVAILABLE", "service unavailable", "");
public static final DawnExceptionParameters FORBIDDEN = public static final DawnExceptionParameters FORBIDDEN =
...@@ -36,6 +36,6 @@ public class BaseExceptions { ...@@ -36,6 +36,6 @@ public class BaseExceptions {
return new DawnExceptionParameters(400, "BAD_REQUEST", String.format("offset of %d is invalid", offset), ""); return new DawnExceptionParameters(400, "BAD_REQUEST", String.format("offset of %d is invalid", offset), "");
} }
public static final DawnExceptionParameters PRODUCT_UNSUPPORTED = new DawnExceptionParameters( public static final DawnExceptionParameters PRODUCT_UNSUPPORTED =
400, "BAD_REQUEST", "This operation is unsupported for provided product", ""); new DawnExceptionParameters(400, "BAD_REQUEST", "This operation is unsupported for provided product", "");
} }
...@@ -4,7 +4,6 @@ import java.io.PrintWriter; ...@@ -4,7 +4,6 @@ import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC; import org.slf4j.MDC;
import org.springframework.beans.ConversionNotSupportedException; import org.springframework.beans.ConversionNotSupportedException;
...@@ -36,21 +35,20 @@ import org.springframework.web.servlet.NoHandlerFoundException; ...@@ -36,21 +35,20 @@ import org.springframework.web.servlet.NoHandlerFoundException;
public class CustomExceptionHandler { public class CustomExceptionHandler {
private final Map<Class<?>, DawnExceptionParameters> MVC_EXCEPTION_MAP = Map.ofEntries( private final Map<Class<?>, DawnExceptionParameters> MVC_EXCEPTION_MAP = Map.ofEntries(
Map.entry(HttpRequestMethodNotSupportedException.class, BaseExceptions.METHOD_NOT_ALLOWED), Map.entry(HttpRequestMethodNotSupportedException.class, BaseExceptions.METHOD_NOT_ALLOWED),
Map.entry(HttpMediaTypeNotSupportedException.class, BaseExceptions.UNSUPPORTED_MEDIA_TYPE), Map.entry(HttpMediaTypeNotSupportedException.class, BaseExceptions.UNSUPPORTED_MEDIA_TYPE),
Map.entry(HttpMediaTypeNotAcceptableException.class, BaseExceptions.NOT_ACCEPTABLE), Map.entry(HttpMediaTypeNotAcceptableException.class, BaseExceptions.NOT_ACCEPTABLE),
Map.entry(MissingPathVariableException.class, BaseExceptions.INTERNAL_SERVER_ERROR), Map.entry(MissingPathVariableException.class, BaseExceptions.INTERNAL_SERVER_ERROR),
Map.entry(MissingServletRequestParameterException.class, BaseExceptions.BAD_REQUEST), Map.entry(MissingServletRequestParameterException.class, BaseExceptions.BAD_REQUEST),
Map.entry(ServletRequestBindingException.class, BaseExceptions.BAD_REQUEST), Map.entry(ServletRequestBindingException.class, BaseExceptions.BAD_REQUEST),
Map.entry(ConversionNotSupportedException.class, BaseExceptions.INTERNAL_SERVER_ERROR), Map.entry(ConversionNotSupportedException.class, BaseExceptions.INTERNAL_SERVER_ERROR),
Map.entry(TypeMismatchException.class, BaseExceptions.BAD_REQUEST), Map.entry(TypeMismatchException.class, BaseExceptions.BAD_REQUEST),
Map.entry(HttpMessageNotReadableException.class, BaseExceptions.BAD_REQUEST), Map.entry(HttpMessageNotReadableException.class, BaseExceptions.BAD_REQUEST),
Map.entry(HttpMessageNotWritableException.class, BaseExceptions.INTERNAL_SERVER_ERROR), Map.entry(HttpMessageNotWritableException.class, BaseExceptions.INTERNAL_SERVER_ERROR),
Map.entry(MethodArgumentNotValidException.class, BaseExceptions.BAD_REQUEST), Map.entry(MethodArgumentNotValidException.class, BaseExceptions.BAD_REQUEST),
Map.entry(MissingServletRequestPartException.class, BaseExceptions.BAD_REQUEST), Map.entry(MissingServletRequestPartException.class, BaseExceptions.BAD_REQUEST),
Map.entry(NoHandlerFoundException.class, BaseExceptions.NOT_FOUND), Map.entry(NoHandlerFoundException.class, BaseExceptions.NOT_FOUND),
Map.entry(AsyncRequestTimeoutException.class, BaseExceptions.SERVICE_UNAVAILABLE) Map.entry(AsyncRequestTimeoutException.class, BaseExceptions.SERVICE_UNAVAILABLE));
);
@Value("${config.serviceName}") @Value("${config.serviceName}")
private String source; private String source;
...@@ -107,7 +105,7 @@ public class CustomExceptionHandler { ...@@ -107,7 +105,7 @@ public class CustomExceptionHandler {
ArrayIndexOutOfBoundsException exception, WebRequest webRequest) { ArrayIndexOutOfBoundsException exception, WebRequest webRequest) {
return wrapExceptionAs(exception, BaseExceptions.OUT_OF_BOUNDS); return wrapExceptionAs(exception, BaseExceptions.OUT_OF_BOUNDS);
} }
@ExceptionHandler(DawnException.class) @ExceptionHandler(DawnException.class)
public ResponseEntity<Object> handleDawnExceptions(DawnException exception, WebRequest webRequest) { public ResponseEntity<Object> handleDawnExceptions(DawnException exception, WebRequest webRequest) {
return returnDawnException(exception); return returnDawnException(exception);
...@@ -153,6 +151,6 @@ public class CustomExceptionHandler { ...@@ -153,6 +151,6 @@ public class CustomExceptionHandler {
@ExceptionHandler(Throwable.class) @ExceptionHandler(Throwable.class)
public ResponseEntity<Object> handleUnHandledException(Throwable exception, WebRequest webRequest) { public ResponseEntity<Object> handleUnHandledException(Throwable exception, WebRequest webRequest) {
return wrapExceptionAs((Exception)exception, BaseExceptions.UNHANDLED_INTERNAL_SERVER_ERROR); return wrapExceptionAs((Exception) exception, BaseExceptions.UNHANDLED_INTERNAL_SERVER_ERROR);
} }
} }