#860 - Switch to URI.create() and toUri() to avoid checked exceptions.

`URI.create()` wraps `new URI()` and converts a `UriSyntaxException` into an `IllegalArgumentException`.

Also, take advantage of `toUri()`.
This commit is contained in:
Greg Turnquist
2019-03-08 09:21:53 -06:00
parent 122391e60d
commit c35a700eca
8 changed files with 105 additions and 189 deletions

View File

@@ -17,8 +17,6 @@ package org.springframework.hateoas;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -130,7 +128,7 @@ public class EmployeeController {
Link link = linkTo(methodOn(getClass()).findOne(newEmployeeId)).withSelfRel().expand();
return ResponseEntity.created(URI.create(link.getHref())).build();
return ResponseEntity.created(link.toUri()).build();
}
// end::new[]
@@ -146,7 +144,7 @@ public class EmployeeController {
Link link = linkTo(methodOn(getClass()).findOne(id)).withSelfRel().expand();
return ResponseEntity.noContent() //
.location(URI.create(link.getHref())) //
.location(link.toUri()) //
.build();
}
@@ -170,18 +168,9 @@ public class EmployeeController {
EMPLOYEES.put(id, newEmployee);
try {
return ResponseEntity //
.noContent() //
.location( //
new URI(findOne(id) //
.getLink(IanaLinkRelations.SELF) //
.map(link -> link.expand().getHref()) //
.orElse("") //
) //
).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity //
.noContent() //
.location(findOne(id).getRequiredLink(IanaLinkRelations.SELF).toUri()) //
.build();
}
}

View File

@@ -15,10 +15,10 @@ The following example shows how to use it:
Map<String, Object> parameters = new HashMap<>();
parameters.put("user", 27);
Traverson traverson = new Traverson(new URI("http://localhost:8080/api/"), MediaTypes.HAL_JSON);
String name = traverson.follow("movies", "movie", "actor").
withTemplateParameters(parameters).
toObject("$.name");
Traverson traverson = new Traverson(URI.create("http://localhost:8080/api/"), MediaTypes.HAL_JSON);
String name = traverson
.follow("movies", "movie", "actor").withTemplateParameters(parameters)
.toObject("$.name");
----
====

View File

@@ -22,8 +22,6 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -37,11 +35,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.hateoas.support.Employee;
@@ -267,15 +265,10 @@ public class CollectionJsonWebMvcIntegrationTest {
EMPLOYEES.put(newEmployeeId, employee.getContent());
try {
return ResponseEntity.created(new URI(findOne(newEmployeeId) //
.getLink(IanaLinkRelations.SELF.value()) //
.map(link -> link.expand().getHref()) //
.orElse(""))) //
.build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.created(findOne(newEmployeeId) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build();
}
@PutMapping("/employees/{id}")
@@ -283,16 +276,11 @@ public class CollectionJsonWebMvcIntegrationTest {
EMPLOYEES.put(id, employee.getContent());
try {
return ResponseEntity.noContent() //
.location(new URI(findOne(id) //
.getLink(IanaLinkRelations.SELF.value()) //
.map(link -> link.expand().getHref()) //
.orElse(""))) //
.build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.noContent() //
.location(findOne(id) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build();
}
@PatchMapping("/employees/{id}")
@@ -312,16 +300,11 @@ public class CollectionJsonWebMvcIntegrationTest {
EMPLOYEES.put(id, newEmployee);
try {
return ResponseEntity.noContent() //
.location(new URI(findOne(id) //
.getLink(IanaLinkRelations.SELF.value()) //
.map(link -> link.expand().getHref()) //
.orElse(""))) //
.build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.noContent() //
.location(findOne(id) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build();
}
}

View File

@@ -22,8 +22,6 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -35,11 +33,11 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.hateoas.support.Employee;
@@ -155,12 +153,11 @@ public class HalFormsValidationIntegrationTest {
EMPLOYEES.put(newEmployeeId, employee);
try {
return ResponseEntity.noContent().location(new URI(findOne(newEmployeeId).getLink(IanaLinkRelations.SELF.value())
.map(link -> link.expand().getHref()).orElse(""))).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.noContent() //
.location(findOne(newEmployeeId) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build();
}
@PutMapping("/employees/{id}")
@@ -168,17 +165,12 @@ public class HalFormsValidationIntegrationTest {
EMPLOYEES.put(id, employee);
try {
return ResponseEntity //
.noContent() //
.location( //
new URI(findOne(id).getLink(IanaLinkRelations.SELF.value()) //
.map(link -> link.expand().getHref()) //
.orElse("")) //
).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity //
.noContent() //
.location(findOne(id) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build();
}
@PatchMapping("/employees/{id}")
@@ -196,18 +188,12 @@ public class HalFormsValidationIntegrationTest {
EMPLOYEES.put(id, newEmployee);
try {
return ResponseEntity //
.noContent() //
.location( //
new URI(findOne(id) //
.getLink(IanaLinkRelations.SELF.value()) //
.map(link -> link.expand().getHref()) //
.orElse(""))) //
.build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity //
.noContent() //
.location(findOne(id) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build();
}
}

View File

@@ -23,7 +23,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -37,11 +36,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.hateoas.mediatype.collectionjson.CollectionJsonLinkDiscoverer;
@@ -507,11 +506,7 @@ public class MultiMediaTypeWebMvcIntegrationTest {
EMPLOYEES.put(newEmployeeId, employee.getContent());
try {
return ResponseEntity.created(toUri(newEmployeeId)).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.created(toUri(newEmployeeId)).build();
}
@PutMapping("/employees/{id}")
@@ -519,11 +514,7 @@ public class MultiMediaTypeWebMvcIntegrationTest {
EMPLOYEES.put(id, employee.getContent());
try {
return ResponseEntity.noContent().location(toUri(id)).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.noContent().location(toUri(id)).build();
}
@PatchMapping("/employees/{id}")
@@ -542,21 +533,14 @@ public class MultiMediaTypeWebMvcIntegrationTest {
EMPLOYEES.put(id, newEmployee);
try {
return ResponseEntity.noContent().location(toUri(id)).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.noContent().location(toUri(id)).build();
}
private URI toUri(Integer id) throws URISyntaxException {
private URI toUri(Integer id) {
String uri = findOne(id) //
.getLink(IanaLinkRelations.SELF.value()) //
.map(link -> link.expand().getHref()) //
.orElse("");
return new URI(uri);
return findOne(id) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri();
}
}

View File

@@ -55,10 +55,10 @@ public class WebFluxLinkBuilderTest {
* @see #728
*/
@Test
public void linkAtSameLevelAsExplicitServerExchangeShouldWork() throws URISyntaxException {
public void linkAtSameLevelAsExplicitServerExchangeShouldWork() {
when(this.exchange.getRequest()).thenReturn(this.request);
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/api"));
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/api"));
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
linkTo(methodOn(TestController.class).root(), this.exchange).withSelfRel().toMono() //
@@ -76,10 +76,10 @@ public class WebFluxLinkBuilderTest {
* @see #728
*/
@Test
public void linkAtSameLevelAsContextProvidedServerExchangeShouldWork() throws URISyntaxException {
public void linkAtSameLevelAsContextProvidedServerExchangeShouldWork() {
when(this.exchange.getRequest()).thenReturn(this.request);
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/api"));
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/api"));
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
linkTo(methodOn(TestController.class).root()).withSelfRel().toMono() //
@@ -97,11 +97,11 @@ public class WebFluxLinkBuilderTest {
* @see #728
*/
@Test
public void shallowLinkFromDeepExplicitServerExchangeShouldWork() throws URISyntaxException {
public void shallowLinkFromDeepExplicitServerExchangeShouldWork() {
when(this.exchange.getRequest()).thenReturn(this.request);
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/api/employees"));
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/api/employees"));
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
linkTo(methodOn(TestController.class).root(), this.exchange).withSelfRel().toMono() //
@@ -120,10 +120,10 @@ public class WebFluxLinkBuilderTest {
* @see #728
*/
@Test
public void shallowLinkFromDeepContextProvidedServerExchangeShouldWork() throws URISyntaxException {
public void shallowLinkFromDeepContextProvidedServerExchangeShouldWork() {
when(this.exchange.getRequest()).thenReturn(this.request);
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/api/employees"));
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/api/employees"));
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
linkTo(methodOn(TestController.class).root()).withSelfRel().toMono() //
@@ -142,10 +142,10 @@ public class WebFluxLinkBuilderTest {
* @see #728
*/
@Test
public void deepLinkFromShallowExplicitServerExchangeShouldWork() throws URISyntaxException {
public void deepLinkFromShallowExplicitServerExchangeShouldWork() {
when(this.exchange.getRequest()).thenReturn(this.request);
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/api"));
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/api"));
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
linkTo(methodOn(TestController.class).deep(), this.exchange).withSelfRel().toMono() //
@@ -163,10 +163,10 @@ public class WebFluxLinkBuilderTest {
* @see #728
*/
@Test
public void deepLinkFromShallowContextProvidedServerExchangeShouldWork() throws URISyntaxException {
public void deepLinkFromShallowContextProvidedServerExchangeShouldWork() {
when(this.exchange.getRequest()).thenReturn(this.request);
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/api"));
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/api"));
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
linkTo(methodOn(TestController.class).deep()).withSelfRel().toMono() //
@@ -185,10 +185,10 @@ public class WebFluxLinkBuilderTest {
* @see #728
*/
@Test
public void linkToRouteWithNoMappingShouldWork() throws URISyntaxException {
public void linkToRouteWithNoMappingShouldWork() {
when(this.exchange.getRequest()).thenReturn(this.request);
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/"));
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/"));
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
linkTo(methodOn(TestController2.class).root()).withSelfRel().toMono() //

View File

@@ -18,14 +18,12 @@ package org.springframework.hateoas.support;
import static org.springframework.hateoas.server.reactive.WebFluxLinkBuilder.*;
import static reactor.function.TupleUtils.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.net.URI;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.hateoas.Affordance;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
@@ -128,20 +126,17 @@ public class WebFluxEmployeeController {
@PostMapping("/employees")
public Mono<ResponseEntity<?>> newEmployee(@RequestBody Mono<EntityModel<Employee>> employee) {
return employee.flatMap(resource -> {
return employee //
.flatMap(resource -> {
int newEmployeeId = EMPLOYEES.size();
EMPLOYEES.put(newEmployeeId, resource.getContent());
return findOne(newEmployeeId);
}).map(findOne -> {
return ResponseEntity.created(URI.create(findOne //
.getLink(IanaLinkRelations.SELF) //
.map(link -> link.expand().getHref()) //
.orElse(""))) //
.build();
});
int newEmployeeId = EMPLOYEES.size();
EMPLOYEES.put(newEmployeeId, resource.getContent());
return findOne(newEmployeeId);
}) //
.map(findOne -> ResponseEntity.created(findOne //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri()) //
.build());
}
@PutMapping("/employees/{id}")
@@ -151,46 +146,34 @@ public class WebFluxEmployeeController {
return employee.flatMap(resource -> {
EMPLOYEES.put(id, resource.getContent());
return findOne(id);
}).map(findOne -> {
return ResponseEntity.noContent() //
.location(URI.create(findOne //
.getLink(IanaLinkRelations.SELF) //
.map(link -> link.expand().getHref()) //
.orElse(""))) //
.build();
});
}).map(findOne -> ResponseEntity.noContent() //
.location(findOne.getRequiredLink(IanaLinkRelations.SELF).toUri()).build());
}
@PatchMapping("/employees/{id}")
public Mono<ResponseEntity<?>> partiallyUpdateEmployee( //
@RequestBody Mono<EntityModel<Employee>> employee, @PathVariable Integer id) {
return employee.flatMap(resource -> {
return employee //
.flatMap(resource -> {
Employee oldEmployee = EMPLOYEES.get(id);
Employee newEmployee = oldEmployee;
Employee oldEmployee = EMPLOYEES.get(id);
Employee newEmployee = oldEmployee;
if (resource.getContent().getName() != null) {
newEmployee = newEmployee.withName(resource.getContent().getName());
}
if (resource.getContent().getName() != null) {
newEmployee = newEmployee.withName(resource.getContent().getName());
}
if (resource.getContent().getRole() != null) {
newEmployee = newEmployee.withRole(resource.getContent().getRole());
}
if (resource.getContent().getRole() != null) {
newEmployee = newEmployee.withRole(resource.getContent().getRole());
}
EMPLOYEES.put(id, newEmployee);
EMPLOYEES.put(id, newEmployee);
return findOne(id);
return findOne(id);
}).map(findOne -> {
return ResponseEntity.noContent() //
.location(URI.create(findOne //
.getLink(IanaLinkRelations.SELF) //
.map(link -> link.expand().getHref()) //
.orElse(""))) //
.build();
});
}) //
.map(findOne -> ResponseEntity.noContent() //
.location(findOne.getRequiredLink(IanaLinkRelations.SELF).toUri()).build());
}
}

View File

@@ -17,8 +17,6 @@ package org.springframework.hateoas.support;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -138,7 +136,7 @@ public class WebMvcEmployeeController {
Link link = linkTo(methodOn(getClass()).findOne(newEmployeeId)).withSelfRel().expand();
return ResponseEntity.created(URI.create(link.getHref())).build();
return ResponseEntity.created(link.toUri()).build();
}
@PutMapping("/employees/{id}")
@@ -149,7 +147,7 @@ public class WebMvcEmployeeController {
Link link = linkTo(methodOn(getClass()).findOne(id)).withSelfRel().expand();
return ResponseEntity.noContent() //
.location(URI.create(link.getHref())) //
.location(link.toUri()) //
.build();
}
@@ -170,18 +168,11 @@ public class WebMvcEmployeeController {
EMPLOYEES.put(id, newEmployee);
try {
return ResponseEntity //
.noContent() //
.location( //
new URI(findOne(id) //
.getLink(IanaLinkRelations.SELF) //
.map(link -> link.expand().getHref()) //
.orElse("") //
) //
).build();
} catch (URISyntaxException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity //
.noContent() //
.location(findOne(id) //
.getRequiredLink(IanaLinkRelations.SELF) //
.toUri())
.build();
}
}