-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from TAMULib/simple-resolver-api
Simple entity resolver API
- Loading branch information
Showing
28 changed files
with
598 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
== Simple Entity Resolver API | ||
|
||
Endpoints for basic Entity resolving | ||
|
||
=== Create with URL | ||
|
||
==== Request | ||
include::{snippets}/create-with-url/http-request.adoc[] | ||
|
||
==== Query Parameters | ||
include::{snippets}/create-with-url/request-parameters.adoc[] | ||
|
||
==== Response | ||
include::{snippets}/create-with-url/http-response.adoc[] | ||
|
||
|
||
=== Resolve Id | ||
|
||
==== Request | ||
include::{snippets}/resolve-id/http-request.adoc[] | ||
|
||
==== Path Parameters | ||
include::{snippets}/resolve-id/path-parameters.adoc[] | ||
|
||
==== Response | ||
include::{snippets}/resolve-id/http-response.adoc[] | ||
|
||
|
||
=== Resolve URL | ||
|
||
==== Request | ||
include::{snippets}/resolve-url/http-request.adoc[] | ||
|
||
==== Query Parameters | ||
include::{snippets}/resolve-url/request-parameters.adoc[] | ||
|
||
==== Response | ||
include::{snippets}/resolve-url/http-response.adoc[] | ||
|
||
|
||
=== Delete by Id | ||
|
||
==== Request | ||
include::{snippets}/delete-by-id/http-request.adoc[] | ||
|
||
==== Path Parameters | ||
include::{snippets}/delete-by-id/path-parameters.adoc[] | ||
|
||
==== Response | ||
include::{snippets}/delete-by-id/http-response.adoc[] |
31 changes: 31 additions & 0 deletions
31
src/main/java/edu/tamu/eider/app/config/AppRepositoryConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package edu.tamu.eider.app.config; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration; | ||
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
|
||
import edu.tamu.eider.app.model.Entity; | ||
import edu.tamu.eider.app.model.Identifier; | ||
import edu.tamu.eider.app.model.IdentifierType; | ||
import edu.tamu.eider.app.model.Name; | ||
import edu.tamu.eider.app.web.converter.PlainTextHttpMessageConverter; | ||
|
||
@Configuration | ||
public class AppRepositoryConfig implements RepositoryRestConfigurer { | ||
|
||
@Override | ||
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { | ||
config.exposeIdsFor(Entity.class, Identifier.class, IdentifierType.class, Name.class); | ||
RepositoryRestConfigurer.super.configureRepositoryRestConfiguration(config); | ||
} | ||
|
||
@Override | ||
public void configureHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) { | ||
messageConverters.add(0, new PlainTextHttpMessageConverter()); | ||
RepositoryRestConfigurer.super.configureHttpMessageConverters(messageConverters); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
src/main/java/edu/tamu/eider/app/config/RepositoryConfig.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,5 @@ public class IdentifierType { | |
|
||
@Size(max = 250) | ||
private String name; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,5 @@ public Name(String name, String notes, Entity entity) { | |
this.notes = notes; | ||
this.entity = entity; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/edu/tamu/eider/app/web/EntityRedirectController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package edu.tamu.eider.app.web; | ||
|
||
import static org.springframework.web.bind.annotation.RequestMethod.HEAD; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.rest.webmvc.RepositoryRestController; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.server.ResponseStatusException; | ||
import org.springframework.web.servlet.view.RedirectView; | ||
|
||
import edu.tamu.eider.app.model.Entity; | ||
import edu.tamu.eider.app.model.repo.EntityRepository; | ||
|
||
@RepositoryRestController | ||
public class EntityRedirectController { | ||
|
||
@Autowired | ||
private EntityRepository entityRepo; | ||
|
||
@RequestMapping(method = HEAD, path = "/entity/{uuid}") | ||
public RedirectView headRedirectEntity(@PathVariable(required = true) final UUID uuid) { | ||
return redirectEntity(uuid); | ||
} | ||
|
||
@GetMapping("/entity/{uuid}/redirect") | ||
public RedirectView getRedirectEntity(@PathVariable(required = true) final UUID uuid) { | ||
return redirectEntity(uuid); | ||
} | ||
|
||
private RedirectView redirectEntity(final UUID uuid) { | ||
Optional<Entity> entityOption = entityRepo.findById(uuid); | ||
if (entityOption.isPresent()) { | ||
return new RedirectView(entityOption.get().getUrl().toString()); | ||
} else { | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Unable to find Entity for provided id"); | ||
} | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/edu/tamu/eider/app/web/EntityResolverController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package edu.tamu.eider.app.web; | ||
|
||
import static org.springframework.web.bind.annotation.RequestMethod.DELETE; | ||
import static org.springframework.web.bind.annotation.RequestMethod.GET; | ||
import static org.springframework.web.bind.annotation.RequestMethod.POST; | ||
|
||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.rest.webmvc.RepositoryRestController; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import edu.tamu.eider.app.model.Entity; | ||
import edu.tamu.eider.app.model.repo.EntityRepository; | ||
|
||
@RepositoryRestController | ||
public class EntityResolverController { | ||
|
||
@Autowired | ||
private EntityRepository entityRepo; | ||
|
||
@RequestMapping(method = POST, path = "/entity", produces = MediaType.TEXT_PLAIN_VALUE) | ||
public @ResponseBody ResponseEntity<String> createWithUrl(@RequestParam(required = true) URL url) throws URISyntaxException { | ||
Optional<Entity> existingEntity = entityRepo.findByUrl(url); | ||
if (existingEntity.isPresent()) { | ||
return ResponseEntity.ok() | ||
.contentType(MediaType.TEXT_PLAIN) | ||
.body(existingEntity.get().getId().toString()); | ||
} | ||
Entity entity = new Entity(); | ||
entity.setUrl(url); | ||
entity = entityRepo.save(entity); | ||
return ResponseEntity.created(url.toURI()) | ||
.contentType(MediaType.TEXT_PLAIN) | ||
.body(entity.getId().toString()); | ||
} | ||
|
||
@RequestMapping(method = GET, path = "/entity/{uuid}", produces = MediaType.TEXT_PLAIN_VALUE) | ||
public @ResponseBody ResponseEntity<String> resolveId(@PathVariable(required = true) String uuid) { | ||
return ResponseEntity.ok() | ||
.contentType(MediaType.TEXT_PLAIN) | ||
.body(entityRepo.findById(UUID.fromString(uuid)) | ||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, String.format("Entity with id {} not found", uuid))) | ||
.getUrl().toString()); | ||
} | ||
|
||
@RequestMapping(method = GET, path = "/entity", produces = MediaType.TEXT_PLAIN_VALUE) | ||
public @ResponseBody ResponseEntity<String> resolveUrl(@RequestParam(required = true) URL url) { | ||
return ResponseEntity.ok() | ||
.contentType(MediaType.TEXT_PLAIN) | ||
.body(entityRepo.findByUrl(url) | ||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, String.format("Entity with url {} not found", url))) | ||
.getId().toString()); | ||
} | ||
|
||
@RequestMapping(method = DELETE, path = "/entity/{uuid}", produces = MediaType.TEXT_PLAIN_VALUE) | ||
public @ResponseBody ResponseEntity<Void> deleteById(@PathVariable(required = true) String uuid) { | ||
Entity entity = entityRepo.findById(UUID.fromString(uuid)) | ||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, String.format("Entity with id {} not found", uuid))); | ||
entityRepo.delete(entity); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
} |
Oops, something went wrong.