Skip to content

Commit e210d9b

Browse files
committed
#860 - Switch to URI.create() to avoid checked exceptions.
`URI.create()` wraps `new URI()` and converts a `UriSyntaxException` into an `IllegalArgumentException`.
1 parent cae49e7 commit e210d9b

File tree

7 files changed

+82
-127
lines changed

7 files changed

+82
-127
lines changed

src/docs/java/org/springframework/hateoas/EmployeeController.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
1919

2020
import java.net.URI;
21-
import java.net.URISyntaxException;
2221
import java.util.ArrayList;
2322
import java.util.List;
2423
import java.util.Map;
@@ -170,18 +169,14 @@ public ResponseEntity<?> partiallyUpdateEmployee( //
170169

171170
EMPLOYEES.put(id, newEmployee);
172171

173-
try {
174-
return ResponseEntity //
175-
.noContent() //
176-
.location( //
177-
new URI(findOne(id) //
178-
.getLink(IanaLinkRelations.SELF) //
179-
.map(link -> link.expand().getHref()) //
180-
.orElse("") //
181-
) //
182-
).build();
183-
} catch (URISyntaxException e) {
184-
return ResponseEntity.badRequest().body(e.getMessage());
185-
}
172+
return ResponseEntity //
173+
.noContent() //
174+
.location( //
175+
URI.create(findOne(id) //
176+
.getLink(IanaLinkRelations.SELF) //
177+
.map(link -> link.expand().getHref()) //
178+
.orElse("") //
179+
) //
180+
).build();
186181
}
187182
}

src/main/asciidoc/client.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The following example shows how to use it:
1515
Map<String, Object> parameters = new HashMap<>();
1616
parameters.put("user", 27);
1717
18-
Traverson traverson = new Traverson(new URI("http://localhost:8080/api/"), MediaTypes.HAL_JSON);
18+
Traverson traverson = new Traverson(URI.create("http://localhost:8080/api/"), MediaTypes.HAL_JSON);
1919
String name = traverson.follow("movies", "movie", "actor").
2020
withTemplateParameters(parameters).
2121
toObject("$.name");

src/test/java/org/springframework/hateoas/mediatype/collectionjson/CollectionJsonWebMvcIntegrationTest.java

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
2424

2525
import java.net.URI;
26-
import java.net.URISyntaxException;
2726
import java.util.ArrayList;
2827
import java.util.HashMap;
2928
import java.util.List;
@@ -37,11 +36,11 @@
3736
import org.springframework.context.annotation.Bean;
3837
import org.springframework.context.annotation.Configuration;
3938
import org.springframework.core.io.ClassPathResource;
39+
import org.springframework.hateoas.CollectionModel;
40+
import org.springframework.hateoas.EntityModel;
4041
import org.springframework.hateoas.IanaLinkRelations;
4142
import org.springframework.hateoas.Link;
4243
import org.springframework.hateoas.MediaTypes;
43-
import org.springframework.hateoas.EntityModel;
44-
import org.springframework.hateoas.CollectionModel;
4544
import org.springframework.hateoas.config.EnableHypermediaSupport;
4645
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
4746
import org.springframework.hateoas.support.Employee;
@@ -267,32 +266,24 @@ public ResponseEntity<?> newEmployee(@RequestBody EntityModel<Employee> employee
267266

268267
EMPLOYEES.put(newEmployeeId, employee.getContent());
269268

270-
try {
271-
return ResponseEntity.created(new URI(findOne(newEmployeeId) //
272-
.getLink(IanaLinkRelations.SELF.value()) //
273-
.map(link -> link.expand().getHref()) //
274-
.orElse(""))) //
275-
.build();
276-
} catch (URISyntaxException e) {
277-
return ResponseEntity.badRequest().body(e.getMessage());
278-
}
269+
return ResponseEntity.created(URI.create(findOne(newEmployeeId) //
270+
.getLink(IanaLinkRelations.SELF.value()) //
271+
.map(link -> link.expand().getHref()) //
272+
.orElse(""))) //
273+
.build();
279274
}
280275

281276
@PutMapping("/employees/{id}")
282277
public ResponseEntity<?> updateEmployee(@RequestBody EntityModel<Employee> employee, @PathVariable Integer id) {
283278

284279
EMPLOYEES.put(id, employee.getContent());
285280

286-
try {
287-
return ResponseEntity.noContent() //
288-
.location(new URI(findOne(id) //
289-
.getLink(IanaLinkRelations.SELF.value()) //
290-
.map(link -> link.expand().getHref()) //
291-
.orElse(""))) //
292-
.build();
293-
} catch (URISyntaxException e) {
294-
return ResponseEntity.badRequest().body(e.getMessage());
295-
}
281+
return ResponseEntity.noContent() //
282+
.location(URI.create(findOne(id) //
283+
.getLink(IanaLinkRelations.SELF.value()) //
284+
.map(link -> link.expand().getHref()) //
285+
.orElse(""))) //
286+
.build();
296287
}
297288

298289
@PatchMapping("/employees/{id}")
@@ -312,16 +303,12 @@ public ResponseEntity<?> partiallyUpdateEmployee(@RequestBody EntityModel<Employ
312303

313304
EMPLOYEES.put(id, newEmployee);
314305

315-
try {
316-
return ResponseEntity.noContent() //
317-
.location(new URI(findOne(id) //
318-
.getLink(IanaLinkRelations.SELF.value()) //
319-
.map(link -> link.expand().getHref()) //
320-
.orElse(""))) //
321-
.build();
322-
} catch (URISyntaxException e) {
323-
return ResponseEntity.badRequest().body(e.getMessage());
324-
}
306+
return ResponseEntity.noContent() //
307+
.location(URI.create(findOne(id) //
308+
.getLink(IanaLinkRelations.SELF.value()) //
309+
.map(link -> link.expand().getHref()) //
310+
.orElse(""))) //
311+
.build();
325312
}
326313
}
327314

src/test/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsValidationIntegrationTest.java

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
2424

2525
import java.net.URI;
26-
import java.net.URISyntaxException;
2726
import java.util.ArrayList;
2827
import java.util.List;
2928
import java.util.Map;
@@ -35,11 +34,11 @@
3534
import org.springframework.beans.factory.annotation.Autowired;
3635
import org.springframework.context.annotation.Bean;
3736
import org.springframework.context.annotation.Configuration;
37+
import org.springframework.hateoas.CollectionModel;
38+
import org.springframework.hateoas.EntityModel;
3839
import org.springframework.hateoas.IanaLinkRelations;
3940
import org.springframework.hateoas.Link;
4041
import org.springframework.hateoas.MediaTypes;
41-
import org.springframework.hateoas.EntityModel;
42-
import org.springframework.hateoas.CollectionModel;
4342
import org.springframework.hateoas.config.EnableHypermediaSupport;
4443
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
4544
import org.springframework.hateoas.support.Employee;
@@ -155,30 +154,26 @@ public ResponseEntity<?> newEmployee(@RequestBody Employee employee) {
155154

156155
EMPLOYEES.put(newEmployeeId, employee);
157156

158-
try {
159-
return ResponseEntity.noContent().location(new URI(findOne(newEmployeeId).getLink(IanaLinkRelations.SELF.value())
160-
.map(link -> link.expand().getHref()).orElse(""))).build();
161-
} catch (URISyntaxException e) {
162-
return ResponseEntity.badRequest().body(e.getMessage());
163-
}
157+
return ResponseEntity.noContent() //
158+
.location(URI.create( //
159+
findOne(newEmployeeId).getLink(IanaLinkRelations.SELF.value()) //
160+
.map(link -> link.expand().getHref()) //
161+
.orElse("")))
162+
.build();
164163
}
165164

166165
@PutMapping("/employees/{id}")
167166
public ResponseEntity<?> updateEmployee(@RequestBody Employee employee, @PathVariable Integer id) {
168167

169168
EMPLOYEES.put(id, employee);
170169

171-
try {
172-
return ResponseEntity //
173-
.noContent() //
174-
.location( //
175-
new URI(findOne(id).getLink(IanaLinkRelations.SELF.value()) //
176-
.map(link -> link.expand().getHref()) //
177-
.orElse("")) //
178-
).build();
179-
} catch (URISyntaxException e) {
180-
return ResponseEntity.badRequest().body(e.getMessage());
181-
}
170+
return ResponseEntity //
171+
.noContent() //
172+
.location( //
173+
URI.create(findOne(id).getLink(IanaLinkRelations.SELF.value()) //
174+
.map(link -> link.expand().getHref()) //
175+
.orElse("")) //
176+
).build();
182177
}
183178

184179
@PatchMapping("/employees/{id}")
@@ -196,18 +191,14 @@ public ResponseEntity<?> partiallyUpdateEmployee(@RequestBody Employee employee,
196191

197192
EMPLOYEES.put(id, newEmployee);
198193

199-
try {
200-
return ResponseEntity //
201-
.noContent() //
202-
.location( //
203-
new URI(findOne(id) //
204-
.getLink(IanaLinkRelations.SELF.value()) //
205-
.map(link -> link.expand().getHref()) //
206-
.orElse(""))) //
207-
.build();
208-
} catch (URISyntaxException e) {
209-
return ResponseEntity.badRequest().body(e.getMessage());
210-
}
194+
return ResponseEntity //
195+
.noContent() //
196+
.location( //
197+
URI.create(findOne(id) //
198+
.getLink(IanaLinkRelations.SELF.value()) //
199+
.map(link -> link.expand().getHref()) //
200+
.orElse(""))) //
201+
.build();
211202
}
212203
}
213204

src/test/java/org/springframework/hateoas/server/mvc/MultiMediaTypeWebMvcIntegrationTest.java

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
2424

2525
import java.net.URI;
26-
import java.net.URISyntaxException;
2726
import java.util.ArrayList;
2827
import java.util.List;
2928
import java.util.Map;
@@ -37,11 +36,11 @@
3736
import org.springframework.context.annotation.Bean;
3837
import org.springframework.context.annotation.Configuration;
3938
import org.springframework.core.io.ClassPathResource;
39+
import org.springframework.hateoas.CollectionModel;
40+
import org.springframework.hateoas.EntityModel;
4041
import org.springframework.hateoas.IanaLinkRelations;
4142
import org.springframework.hateoas.Link;
4243
import org.springframework.hateoas.MediaTypes;
43-
import org.springframework.hateoas.EntityModel;
44-
import org.springframework.hateoas.CollectionModel;
4544
import org.springframework.hateoas.config.EnableHypermediaSupport;
4645
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
4746
import org.springframework.hateoas.mediatype.collectionjson.CollectionJsonLinkDiscoverer;
@@ -507,23 +506,15 @@ public ResponseEntity<?> newEmployee(@RequestBody EntityModel<Employee> employee
507506

508507
EMPLOYEES.put(newEmployeeId, employee.getContent());
509508

510-
try {
511-
return ResponseEntity.created(toUri(newEmployeeId)).build();
512-
} catch (URISyntaxException e) {
513-
return ResponseEntity.badRequest().body(e.getMessage());
514-
}
509+
return ResponseEntity.created(toUri(newEmployeeId)).build();
515510
}
516511

517512
@PutMapping("/employees/{id}")
518513
public ResponseEntity<?> updateEmployee(@RequestBody EntityModel<Employee> employee, @PathVariable Integer id) {
519514

520515
EMPLOYEES.put(id, employee.getContent());
521516

522-
try {
523-
return ResponseEntity.noContent().location(toUri(id)).build();
524-
} catch (URISyntaxException e) {
525-
return ResponseEntity.badRequest().body(e.getMessage());
526-
}
517+
return ResponseEntity.noContent().location(toUri(id)).build();
527518
}
528519

529520
@PatchMapping("/employees/{id}")
@@ -542,21 +533,17 @@ public ResponseEntity<?> partiallyUpdateEmployee(@RequestBody EntityModel<Employ
542533

543534
EMPLOYEES.put(id, newEmployee);
544535

545-
try {
546-
return ResponseEntity.noContent().location(toUri(id)).build();
547-
} catch (URISyntaxException e) {
548-
return ResponseEntity.badRequest().body(e.getMessage());
549-
}
536+
return ResponseEntity.noContent().location(toUri(id)).build();
550537
}
551538

552-
private URI toUri(Integer id) throws URISyntaxException {
539+
private URI toUri(Integer id) {
553540

554541
String uri = findOne(id) //
555542
.getLink(IanaLinkRelations.SELF.value()) //
556543
.map(link -> link.expand().getHref()) //
557544
.orElse("");
558545

559-
return new URI(uri);
546+
return URI.create(uri);
560547
}
561548
}
562549

src/test/java/org/springframework/hateoas/server/reactive/WebFluxLinkBuilderTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public class WebFluxLinkBuilderTest {
5555
* @see #728
5656
*/
5757
@Test
58-
public void linkAtSameLevelAsExplicitServerExchangeShouldWork() throws URISyntaxException {
58+
public void linkAtSameLevelAsExplicitServerExchangeShouldWork() {
5959

6060
when(this.exchange.getRequest()).thenReturn(this.request);
61-
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/api"));
61+
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/api"));
6262
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
6363

6464
linkTo(methodOn(TestController.class).root(), this.exchange).withSelfRel().toMono() //
@@ -76,10 +76,10 @@ public void linkAtSameLevelAsExplicitServerExchangeShouldWork() throws URISyntax
7676
* @see #728
7777
*/
7878
@Test
79-
public void linkAtSameLevelAsContextProvidedServerExchangeShouldWork() throws URISyntaxException {
79+
public void linkAtSameLevelAsContextProvidedServerExchangeShouldWork() {
8080

8181
when(this.exchange.getRequest()).thenReturn(this.request);
82-
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/api"));
82+
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/api"));
8383
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
8484

8585
linkTo(methodOn(TestController.class).root()).withSelfRel().toMono() //
@@ -97,11 +97,11 @@ public void linkAtSameLevelAsContextProvidedServerExchangeShouldWork() throws UR
9797
* @see #728
9898
*/
9999
@Test
100-
public void shallowLinkFromDeepExplicitServerExchangeShouldWork() throws URISyntaxException {
100+
public void shallowLinkFromDeepExplicitServerExchangeShouldWork() {
101101

102102
when(this.exchange.getRequest()).thenReturn(this.request);
103103

104-
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/api/employees"));
104+
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/api/employees"));
105105
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
106106

107107
linkTo(methodOn(TestController.class).root(), this.exchange).withSelfRel().toMono() //
@@ -120,10 +120,10 @@ public void shallowLinkFromDeepExplicitServerExchangeShouldWork() throws URISynt
120120
* @see #728
121121
*/
122122
@Test
123-
public void shallowLinkFromDeepContextProvidedServerExchangeShouldWork() throws URISyntaxException {
123+
public void shallowLinkFromDeepContextProvidedServerExchangeShouldWork() {
124124

125125
when(this.exchange.getRequest()).thenReturn(this.request);
126-
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/api/employees"));
126+
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/api/employees"));
127127
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
128128

129129
linkTo(methodOn(TestController.class).root()).withSelfRel().toMono() //
@@ -142,10 +142,10 @@ public void shallowLinkFromDeepContextProvidedServerExchangeShouldWork() throws
142142
* @see #728
143143
*/
144144
@Test
145-
public void deepLinkFromShallowExplicitServerExchangeShouldWork() throws URISyntaxException {
145+
public void deepLinkFromShallowExplicitServerExchangeShouldWork() {
146146

147147
when(this.exchange.getRequest()).thenReturn(this.request);
148-
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/api"));
148+
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/api"));
149149
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
150150

151151
linkTo(methodOn(TestController.class).deep(), this.exchange).withSelfRel().toMono() //
@@ -163,10 +163,10 @@ public void deepLinkFromShallowExplicitServerExchangeShouldWork() throws URISynt
163163
* @see #728
164164
*/
165165
@Test
166-
public void deepLinkFromShallowContextProvidedServerExchangeShouldWork() throws URISyntaxException {
166+
public void deepLinkFromShallowContextProvidedServerExchangeShouldWork() {
167167

168168
when(this.exchange.getRequest()).thenReturn(this.request);
169-
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/api"));
169+
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/api"));
170170
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
171171

172172
linkTo(methodOn(TestController.class).deep()).withSelfRel().toMono() //
@@ -185,10 +185,10 @@ public void deepLinkFromShallowContextProvidedServerExchangeShouldWork() throws
185185
* @see #728
186186
*/
187187
@Test
188-
public void linkToRouteWithNoMappingShouldWork() throws URISyntaxException {
188+
public void linkToRouteWithNoMappingShouldWork() {
189189

190190
when(this.exchange.getRequest()).thenReturn(this.request);
191-
when(this.request.getURI()).thenReturn(new URI("http://localhost:8080/"));
191+
when(this.request.getURI()).thenReturn(URI.create("http://localhost:8080/"));
192192
when(this.request.getHeaders()).thenReturn(new HttpHeaders());
193193

194194
linkTo(methodOn(TestController2.class).root()).withSelfRel().toMono() //

0 commit comments

Comments
 (0)