-
Notifications
You must be signed in to change notification settings - Fork 3
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 #45 from TAMULib/B-03696-redis-resource-store
Redis resource store
- Loading branch information
Showing
32 changed files
with
766 additions
and
51 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
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,51 @@ | ||
package edu.tamu.iiif.config; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = "iiif") | ||
public class AdminConfig { | ||
|
||
private List<Credentials> admins = new ArrayList<Credentials>(); | ||
|
||
public List<Credentials> getAdmins() { | ||
return admins; | ||
} | ||
|
||
public void setAdmins(List<Credentials> admins) { | ||
this.admins = admins; | ||
} | ||
|
||
public static class Credentials { | ||
|
||
private String username; | ||
|
||
private String password; | ||
|
||
public Credentials() { | ||
|
||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
} | ||
|
||
} |
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,61 @@ | ||
package edu.tamu.iiif.config; | ||
|
||
import static org.springframework.http.HttpMethod.DELETE; | ||
import static org.springframework.http.HttpMethod.POST; | ||
import static org.springframework.http.HttpMethod.PUT; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
|
||
import edu.tamu.iiif.config.AdminConfig.Credentials; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
||
@Autowired | ||
private AdminConfig adminConfig; | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
// @formatter:off | ||
http | ||
.csrf() | ||
.disable() | ||
.authorizeRequests() | ||
.antMatchers(POST, "/resources") | ||
.hasRole("ADMIN") | ||
.antMatchers(PUT, "/resources") | ||
.hasRole("ADMIN") | ||
.antMatchers(DELETE, "/resources/*") | ||
.hasRole("ADMIN") | ||
.anyRequest() | ||
.permitAll() | ||
.and() | ||
.httpBasic() | ||
.and() | ||
.sessionManagement() | ||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS); | ||
// @formatter:on | ||
} | ||
|
||
@Autowired | ||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { | ||
for (Credentials adminCredentials : adminConfig.getAdmins()) { | ||
// @formatter:off | ||
auth | ||
.inMemoryAuthentication() | ||
.withUser(adminCredentials.getUsername()) | ||
.password(adminCredentials.getPassword()) | ||
.roles("ADMIN"); | ||
// @formatter:on | ||
} | ||
|
||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/edu/tamu/iiif/controller/ResourceController.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,79 @@ | ||
package edu.tamu.iiif.controller; | ||
|
||
import static org.springframework.http.HttpStatus.MOVED_PERMANENTLY; | ||
import static org.springframework.web.bind.annotation.RequestMethod.POST; | ||
import static org.springframework.web.bind.annotation.RequestMethod.PUT; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
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.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.servlet.view.RedirectView; | ||
|
||
import edu.tamu.iiif.exception.InvalidUrlException; | ||
import edu.tamu.iiif.exception.NotFoundException; | ||
import edu.tamu.iiif.model.RedisResource; | ||
import edu.tamu.iiif.model.repo.RedisResourceRepo; | ||
|
||
@RestController | ||
@RequestMapping("/resources") | ||
public class ResourceController { | ||
|
||
@Autowired | ||
private RedisResourceRepo redisResourceRepo; | ||
|
||
@GetMapping(value = "/{id}", produces = "text/plain") | ||
public String getResourceUrl(@PathVariable String id) throws NotFoundException { | ||
Optional<RedisResource> redisFileResolver = Optional.ofNullable(redisResourceRepo.findOne(id)); | ||
if (redisFileResolver.isPresent()) { | ||
return redisFileResolver.get().getUrl(); | ||
} | ||
throw new NotFoundException(String.format("Unable to resolve resource with id %s", id)); | ||
} | ||
|
||
@GetMapping(value = "/{id}/redirect") | ||
public RedirectView redirectToResource(@PathVariable String id) throws IOException, NotFoundException { | ||
Optional<RedisResource> redisFileResolver = Optional.ofNullable(redisResourceRepo.findOne(id)); | ||
if (redisFileResolver.isPresent()) { | ||
RedirectView redirect = new RedirectView(redisFileResolver.get().getUrl()); | ||
redirect.setStatusCode(MOVED_PERMANENTLY); | ||
return redirect; | ||
} | ||
throw new NotFoundException(String.format("Unable to resolve resource with id %s", id)); | ||
|
||
} | ||
|
||
@GetMapping(value = "/lookup", produces = "text/plain") | ||
public String getResourceId(@RequestParam(value = "uri", required = true) String uri) throws NotFoundException { | ||
Optional<RedisResource> redisFileResolver = redisResourceRepo.findByUrl(uri); | ||
if (redisFileResolver.isPresent()) { | ||
return redisFileResolver.get().getId(); | ||
} | ||
throw new NotFoundException(String.format("No resourse found with uri %s", uri)); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.CREATED) | ||
@RequestMapping(method = { POST, PUT }, produces = "text/plain") | ||
public String addResource(@RequestParam(value = "uri", required = true) String uri) throws InvalidUrlException { | ||
return redisResourceRepo.getOrCreate(uri).getId(); | ||
} | ||
|
||
@DeleteMapping(value = "/{id}", produces = "text/plain") | ||
public String removeResource(@PathVariable String id) throws NotFoundException { | ||
Optional<RedisResource> redisFileResolver = Optional.ofNullable(redisResourceRepo.findOne(id)); | ||
if (redisFileResolver.isPresent()) { | ||
redisResourceRepo.delete(redisFileResolver.get()); | ||
return "Success"; | ||
} | ||
throw new NotFoundException(String.format("Unable to resolve resource with id %s", id)); | ||
} | ||
|
||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/edu/tamu/iiif/exception/InvalidUrlException.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,13 @@ | ||
package edu.tamu.iiif.exception; | ||
|
||
import java.io.IOException; | ||
|
||
public class InvalidUrlException extends IOException { | ||
|
||
private static final long serialVersionUID = -6351161650912426116L; | ||
|
||
public InvalidUrlException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
Oops, something went wrong.