Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public JwtAuthenticationResponse createAuthenticationToken(

@GetMapping(value = "/authentication")
@PreAuthorize("isAuthenticated()")
public JwtAuthenticationResponse createAuthenticationToken() {
public JwtAuthenticationResponse getAuthenticationToken() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getPrincipal() instanceof UserDetails) {
final UserDetails userDetails = (UserDetails) authentication.getPrincipal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

public class BadRequestException extends SdvcException {
public class BadRequestException extends MMSException {

public BadRequestException(Object body) {
super(HttpStatus.BAD_REQUEST, body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

public class ConflictException extends SdvcException {
public class ConflictException extends MMSException {

public ConflictException(Object body) {
super(HttpStatus.CONFLICT, body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

public class DeletedException extends SdvcException {
public class DeletedException extends MMSException {

public DeletedException(Object body) {
super(HttpStatus.GONE, body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

public class ForbiddenException extends SdvcException {
public class ForbiddenException extends MMSException {

public ForbiddenException(Object body) {
super(HttpStatus.FORBIDDEN, body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

public class InternalErrorException extends SdvcException {
public class InternalErrorException extends MMSException {

public InternalErrorException(Object body) {
super(HttpStatus.INTERNAL_SERVER_ERROR, body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

import org.springframework.http.HttpStatus;

public abstract class SdvcException extends RuntimeException {
public abstract class MMSException extends RuntimeException {

private HttpStatus code;
private Object messageObject;

public SdvcException() {
public MMSException() {
}

public SdvcException(HttpStatus code, Object messageObject) {
public MMSException(HttpStatus code, Object messageObject) {
this.code = code;
this.messageObject = messageObject;
}

public SdvcException(int code, Object messageObject) {
public MMSException(int code, Object messageObject) {
this.code = HttpStatus.resolve(code);
this.messageObject = messageObject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

public class NotFoundException extends SdvcException {
public class NotFoundException extends MMSException {

public NotFoundException(Object body) {
super(HttpStatus.NOT_FOUND, body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

public class NotModifiedException extends SdvcException {
public class NotModifiedException extends MMSException {

public NotModifiedException(Object body) {
super(HttpStatus.NOT_MODIFIED, body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

public class UnauthorizedException extends SdvcException {
public class UnauthorizedException extends MMSException {

public UnauthorizedException(Object body) {
super(HttpStatus.UNAUTHORIZED, body);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openmbee.mms.core.services;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import org.openmbee.mms.core.objects.ElementsRequest;
Expand All @@ -9,6 +11,8 @@

public interface NodeService {

void readAsStream(String projectId, String refId, Map<String, String> params, OutputStream output, String accept) throws IOException;

ElementsResponse read(String projectId, String refId, String id, Map<String, String> params);

ElementsResponse read(String projectId, String refId, ElementsRequest req, Map<String, String> params);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openmbee.mms.crud.config;

import org.openmbee.mms.core.exceptions.SdvcException;
import java.util.Map;
import org.openmbee.mms.core.exceptions.MMSException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
Expand All @@ -14,8 +15,11 @@
@Order(Ordered.LOWEST_PRECEDENCE)
public class ExceptionHandlerConfig extends ResponseEntityExceptionHandler {

@ExceptionHandler(value = { SdvcException.class })
protected ResponseEntity<Object> handleSdvcException(SdvcException ex, WebRequest request) {
@ExceptionHandler(value = { MMSException.class })
protected ResponseEntity<Object> handleMMSException(MMSException ex, WebRequest request) {
if (ex.getMessageObject() instanceof String) {
ex.setMessageObject(Map.of("message", ex.getMessageObject()));
}
return handleExceptionInternal(ex, ex.getMessageObject(), new HttpHeaders(), ex.getCode(),
request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.UUID;
import org.openmbee.mms.core.config.Privileges;
import org.openmbee.mms.core.exceptions.BadRequestException;
import org.openmbee.mms.core.exceptions.SdvcException;
import org.openmbee.mms.core.exceptions.MMSException;
import org.openmbee.mms.core.objects.RefsRequest;
import org.openmbee.mms.core.objects.RefsResponse;
import org.openmbee.mms.core.objects.Rejection;
Expand Down Expand Up @@ -97,7 +97,7 @@ public RefsResponse createRefs(

permissionService.initBranchPerms(projectId, branch.getId(), true, auth.getName());
response.getRefs().add(res);
} catch (SdvcException e) {
} catch (MMSException e) {
response.addRejection(new Rejection(branch, e.getCode().value(), e.getMessageObject().toString()));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package org.openmbee.mms.crud.controllers.elements;

import io.swagger.v3.oas.annotations.Parameter;
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 io.swagger.v3.oas.annotations.tags.Tag;

import java.util.Map;

import org.openmbee.mms.core.objects.ElementsRequest;
import org.openmbee.mms.core.objects.ElementsResponse;
import org.openmbee.mms.core.services.CommitService;
import org.openmbee.mms.crud.controllers.BaseController;
import org.openmbee.mms.core.exceptions.BadRequestException;
import org.openmbee.mms.core.services.NodeService;
import org.openmbee.mms.core.pubsub.EmbeddedHookService;
import org.openmbee.mms.crud.hooks.ElementUpdateHook;
import org.openmbee.mms.crud.services.DefaultCommitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -21,32 +30,51 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

@RestController
@RequestMapping("/projects/{projectId}/refs/{refId}/elements")
@Tag(name = "Elements")
public class ElementsController extends BaseController {

private EmbeddedHookService embeddedHookService;
private CommitService commitService;

@Autowired
public void setEmbeddedHookService(EmbeddedHookService embeddedHookService) {
this.embeddedHookService = embeddedHookService;
}

@Autowired
public void setCommitService(@Qualifier("defaultCommitService")CommitService commitService) {
this.commitService = commitService;
}

@GetMapping
@PreAuthorize("@mss.hasBranchPrivilege(authentication, #projectId, #refId, 'BRANCH_READ', true)")
public ElementsResponse getAllElements(
@ApiResponse(responseCode = "200", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ElementsResponse.class)),
@Content(mediaType = "application/x-ndjson")
})
public ResponseEntity<StreamingResponseBody> getAllElements(
@PathVariable String projectId,
@PathVariable String refId,
@RequestParam(required = false) String commitId,
@RequestParam(required = false) Map<String, String> params) {
@RequestParam(required = false) Map<String, String> params,
@Parameter(hidden = true) @RequestHeader(value = "Accept", defaultValue = "application/json") String accept) {

NodeService nodeService = getNodeService(projectId);
return nodeService.read(projectId, refId, "", params);
if (commitId != null && !commitId.isEmpty()) {
commitService.getCommit(projectId, commitId); //check commit exists
}
StreamingResponseBody stream = outputStream -> nodeService.readAsStream(projectId, refId, params, outputStream, accept);
return ResponseEntity.ok()
.header("Content-Type", accept.equals("application/x-ndjson") ? accept : "application/json")
.body(stream);
}

@GetMapping(value = "/{elementId}", produces = MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -58,7 +86,6 @@ public ElementsResponse getElement(
@RequestParam(required = false) String commitId,
@RequestParam(required = false) Map<String, String> params) {


NodeService nodeService = getNodeService(projectId);
ElementsResponse res = nodeService.read(projectId, refId, elementId, params);
handleSingleResponse(res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.openmbee.mms.core.config.ProjectSchemas;
import org.openmbee.mms.core.dao.ProjectDAO;
import org.openmbee.mms.core.dao.ProjectIndex;
import org.openmbee.mms.core.exceptions.SdvcException;
import org.openmbee.mms.core.exceptions.MMSException;
import org.openmbee.mms.core.objects.ProjectsRequest;
import org.openmbee.mms.core.objects.ProjectsResponse;
import org.openmbee.mms.core.exceptions.DeletedException;
Expand Down Expand Up @@ -160,7 +160,7 @@ public ProjectsResponse createOrUpdateProjects(
permissionService.setProjectInherit(true, json.getProjectId());
}
}
} catch (SdvcException ex) {
} catch (MMSException ex) {
response.addRejection(new Rejection(json, ex.getCode().value(), ex.getMessageObject().toString()));
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,12 @@ public CommitsResponse getRefCommits(String projectId, String refId, Map<String,
public CommitsResponse getCommit(String projectId, String commitId) {
ContextHolder.setContext(projectId);
CommitsResponse res = new CommitsResponse();
try {
Optional<CommitJson> commit = commitIndex.findById(commitId);
if (commit.isPresent()) {
res.getCommits().add(commit.get());
} else {
throw new NotFoundException(res);
}
} catch (Exception e) {
e.printStackTrace();
throw new InternalErrorException(e);

Optional<CommitJson> commit = commitIndex.findById(commitId);
if (commit.isPresent()) {
res.getCommits().add(commit.get());
} else {
throw new NotFoundException("Commit not found");
}
return res;
}
Expand All @@ -111,21 +107,16 @@ public CommitsResponse getCommit(String projectId, String commitId) {
public CommitsResponse getElementCommits(String projectId, String refId, String elementId, Map<String, String> params) {
ContextHolder.setContext(projectId);
CommitsResponse res = new CommitsResponse();
try {
Optional<Branch> ref = branchRepository.findByBranchId(refId);
if (!ref.isPresent()) {
throw new NotFoundException("Branch not found");
}
List<Commit> refCommits = commitRepository.findByRefAndTimestampAndLimit(ref.get(), null, 0);
Set<String> commitIds = new HashSet<>();
for (Commit commit: refCommits) {
commitIds.add(commit.getDocId());
}
res.getCommits().addAll(commitIndex.elementHistory(elementId, commitIds));
} catch (Exception e) {
e.printStackTrace();
throw new InternalErrorException(e);
Optional<Branch> ref = branchRepository.findByBranchId(refId);
if (!ref.isPresent()) {
throw new NotFoundException("Branch not found");
}
List<Commit> refCommits = commitRepository.findByRefAndTimestampAndLimit(ref.get(), null, 0);
Set<String> commitIds = new HashSet<>();
for (Commit commit: refCommits) {
commitIds.add(commit.getDocId());
}
res.getCommits().addAll(commitIndex.elementHistory(elementId, commitIds));
return res;
}

Expand Down
Loading