Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 4630186

Browse files
author
Arjen Poutsma
committed
Revert "Merge pull request #28 from ramnivas/fix-RestTemple"
This reverts commit e3586d8, reversing changes made to fbfc885.
1 parent e3586d8 commit 4630186

File tree

1 file changed

+138
-1
lines changed

1 file changed

+138
-1
lines changed

src/main/scala/org/springframework/scala/web/client/RestTemplate.scala

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import scala.reflect.ClassTag
3030
* advantage of functions and Scala types.
3131
*
3232
* @author Arjen Poutsma
33-
* @author Ramnivas Laddad
3433
* @since 1.0
3534
* @constructor Creates a `RestTemplate` that wraps the given Java template, defaulting to the standard `RestTemplate`
3635
* @param javaTemplate the Java `RestTemplate` to wrap
@@ -76,6 +75,17 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
7675
Option(javaTemplate.getForObject(url, typeToClass[T], uriVariables.asJava))
7776
}
7877

78+
/**
79+
* Retrieve a representation by doing a GET on the URL.
80+
* The response (if any) is converted and returned.
81+
*
82+
* @param url the URL
83+
* @return the converted object
84+
*/
85+
def getForAny[T: ClassTag](url: URI): Option[T] = {
86+
Option(javaTemplate.getForObject(url, typeToClass[T]))
87+
}
88+
7989
/**
8090
* Retrieve an entity by doing a GET on the specified URL.
8191
* The response is converted and stored in an `ResponseEntity`.
@@ -102,6 +112,15 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
102112
javaTemplate.getForEntity(url, typeToClass[T], uriVariables.asJava)
103113
}
104114

115+
/**
116+
* Retrieve a representation by doing a GET on the URL .
117+
* The response is converted and stored in an [[org.springframework.http.ResponseEntity]]}.
118+
* @param url the URL
119+
* @return the converted object
120+
*/
121+
def getForEntity[T: ClassTag](url: URI): ResponseEntity[T] = {
122+
javaTemplate.getForEntity(url, typeToClass[T])
123+
}
105124

106125
// HEAD
107126
/**
@@ -130,6 +149,14 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
130149
javaTemplate.headForHeaders(url, uriVariables.asJava)
131150
}
132151

152+
/**
153+
* Retrieve all headers of the resource specified by the URL.
154+
* @param url the URL
155+
* @return all HTTP headers of that resource
156+
*/
157+
def headForHeaders(url: URI): HttpHeaders = {
158+
javaTemplate.headForHeaders(url)
159+
}
133160

134161
// POST
135162
/**
@@ -172,6 +199,22 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
172199
javaTemplate.postForLocation(url, request.orNull, uriVariables.asJava)
173200
}
174201

202+
/**
203+
* Create a new resource by POSTing the given object to the URL, and returns the value of the `Location` header.
204+
* This header typically indicates where the new resource is stored.
205+
*
206+
* The `request` parameter can be a [[org.springframework.http.HttpEntity]] in order to add additional HTTP headers
207+
* to the request.
208+
*
209+
* @param url the URL
210+
* @param request the Object to be POSTed
211+
* @return the value for the `Location` header
212+
* @see HttpEntity
213+
*/
214+
def postForLocation(url: URI, request: Option[Any]): URI = {
215+
javaTemplate.postForLocation(url, request.orNull)
216+
}
217+
175218
/**
176219
* Create a new resource by POSTing the given object to the URI template, and returns the representation found in
177220
* the response.
@@ -210,6 +253,22 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
210253
Option(javaTemplate.postForObject(url, request.orNull, typeToClass[T], uriVariables.asJava))
211254
}
212255

256+
/**
257+
* Create a new resource by POSTing the given object to the URL, and returns the representation found in the
258+
* response.
259+
*
260+
* The `request` parameter can be a [[org.springframework.http.HttpEntity]] in order to add additional HTTP headers
261+
* to the request.
262+
*
263+
* @param url the URL
264+
* @param request the Object to be POSTed
265+
* @return the converted object
266+
* @see HttpEntity
267+
*/
268+
def postForObject[T: ClassTag](url: URI, request: Option[Any]): Option[T] = {
269+
Option(javaTemplate.postForObject(url, request.orNull, typeToClass[T]))
270+
}
271+
213272
/**
214273
* Create a new resource by POSTing the given object to the URI template, and returns the response as
215274
* [[org.springframework.http.ResponseEntity]].
@@ -248,6 +307,23 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
248307
javaTemplate.postForEntity(url, request.orNull, typeToClass[T], uriVariables.asJava)
249308
}
250309

310+
/**
311+
* Create a new resource by POSTing the given object to the URL, and returns the response as
312+
* [[org.springframework.http.ResponseEntity]].
313+
*
314+
* The `request` parameter can be a [[org.springframework.http.HttpEntity]] in order to add additional HTTP headers
315+
* to the request.
316+
*
317+
* @param url the URL
318+
* @param request the Object to be POSTed, may be `null`
319+
* @return the converted object
320+
* @see HttpEntity
321+
* @since 3.0.2
322+
*/
323+
def postForEntity[T: ClassTag](url: URI, request: Option[Any]): ResponseEntity[T] = {
324+
javaTemplate.postForEntity(url, request.orNull, typeToClass[T])
325+
}
326+
251327
// PUT
252328
/**
253329
* Create or update a resource by PUTting the given object to the URI.
@@ -283,6 +359,19 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
283359
javaTemplate.put(url, request.orNull, uriVariables.asJava)
284360
}
285361

362+
/**
363+
* Creates a new resource by PUTting the given object to URL.
364+
*
365+
* The `request` parameter can be a [[org.springframework.http.HttpEntity]] in order to add additional HTTP headers
366+
* to the request.
367+
*
368+
* @param url the URL
369+
* @param request the Object to be PUT
370+
* @see HttpEntity
371+
*/
372+
def put(url: URI, request: Option[Any]) {
373+
javaTemplate.put(url, request.orNull)
374+
}
286375

287376
// DELETE
288377
/**
@@ -308,6 +397,14 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
308397
javaTemplate.delete(url, uriVariables.asJava)
309398
}
310399

400+
/**
401+
* Delete the resources at the specified URL.
402+
*
403+
* @param url the URL
404+
*/
405+
def delete(url: URI) {
406+
javaTemplate.delete(url)
407+
}
311408

312409
// OPTIONS
313410
/**
@@ -336,6 +433,16 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
336433
javaTemplate.optionsForAllow(url, uriVariables.asJava).asScala
337434
}
338435

436+
/**
437+
* Return the value of the Allow header for the given URL.
438+
*
439+
* @param url the URL
440+
* @return the value of the allow header
441+
*/
442+
def optionsForAllow(url: URI): Set[HttpMethod] = {
443+
javaTemplate.optionsForAllow(url).asScala
444+
}
445+
339446
// exchange
340447
/**
341448
* Execute the HTTP method to the given URI template, writing the given request entity to the request, and
@@ -370,6 +477,18 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
370477
javaTemplate.exchange(url, method, requestEntity.orNull, typeToClass[T], uriVariables.asJava)
371478
}
372479

480+
/**
481+
* Execute the HTTP method to the given URI template, writing the given request entity to the request, and
482+
* returns the response as `ResponseEntity`.
483+
*
484+
* @param url the URL
485+
* @param method the HTTP method (GET, POST, etc)
486+
* @param requestEntity the entity (headers and/or body) to write to the request
487+
* @return the response as entity
488+
*/
489+
def exchange[T: ClassTag](url: URI, method: HttpMethod, requestEntity: Option[HttpEntity[_]]): ResponseEntity[T] = {
490+
javaTemplate.exchange(url, method, requestEntity.orNull, typeToClass[T])
491+
}
373492

374493
// general execution
375494
/**
@@ -414,6 +533,24 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
414533
uriVariables.asJava))
415534
}
416535

536+
/**
537+
* Execute the HTTP method to the given URL, preparing the request with the given function, and reading the response
538+
* with a function.
539+
*
540+
* @param url the URL
541+
* @param method the HTTP method (GET, POST, etc)
542+
* @param requestFunction function that prepares the request
543+
* @param responseFunction function that extracts the return value from the response
544+
* @return an arbitrary object, as returned by the response object
545+
*/
546+
def execute[T](url: URI, method: HttpMethod)
547+
(requestFunction: ClientHttpRequest => Unit)
548+
(responseFunction: ClientHttpResponse => T): Option[T] = {
549+
Option(javaTemplate.execute(url, method, functionToRequestCallback(requestFunction),
550+
functionToResponseExtractor(responseFunction)))
551+
552+
}
553+
417554
private def asInstanceOfAnyRef(seq: Seq[Any]) = {
418555
seq.map(_.asInstanceOf[AnyRef])
419556
}

0 commit comments

Comments
 (0)