@@ -11,7 +11,7 @@ import org.restlet.{Client,Component,Restlet};
11
11
import org .restlet .data ._
12
12
import java .text .SimpleDateFormat
13
13
14
- import scala .xml .XML
14
+ import scala .xml .{ XML , PrettyPrinter }
15
15
import scala .xml .Elem
16
16
17
17
/*
@@ -25,20 +25,28 @@ object RFC3339 extends SimpleDateFormat("yyyy-MM-dd'T'h:m:ss")
25
25
* This restlet mixes in the traits corresponding to the HTTP methods it supports and the dispatch logic is handled by each of the trait.
26
26
*/
27
27
class AdminRestlet (val subscriber : Subscriber ) extends Default with Logging with POST with GET with DELETE {
28
+ val xmlPrinter = new PrettyPrinter (100 ,2 )
29
+
28
30
def doPOST (request : Request , response : Response ){
29
31
val id = request.getAttributes.get(" ID" )
30
32
if (id != null ){
31
33
response.setStatus(Status .CLIENT_ERROR_METHOD_NOT_ALLOWED )
32
34
return
33
35
}
34
- if (request.getEntity.getMediaType != MediaType .TEXT_XML ){
35
- response.setStatus(Status .CLIENT_ERROR_NOT_ACCEPTABLE ," Please provide some XML." )
36
+
37
+ if (request.getEntity.getMediaType != MediaType .APPLICATION_WWW_FORM ){
38
+ log.error(" Invalid MIME type: " + request.getEntity.getMediaType)
39
+ response.setStatus(Status .CLIENT_ERROR_METHOD_NOT_ALLOWED )
36
40
return
37
41
}
38
-
39
- val xml = XML .loadString(request.getEntity.getText)
40
- subscriber.subUnsub(xml \ " @feedURL" text , true )
41
- }
42
+ val form = request.getEntityAsForm
43
+ val feedURL = form.getFirstValue(" feedURL" )
44
+ if (feedURL != null ){
45
+ subscriber.subUnsub(feedURL , true )
46
+ response.setEntity(" Creating subscription for feed: " + feedURL,MediaType .TEXT_PLAIN )
47
+ response.setStatus(Status .SUCCESS_ACCEPTED )
48
+ }
49
+ }
42
50
43
51
def doDELETE (request : Request , response : Response ){
44
52
val id = request.getAttributes.get(" ID" )
@@ -48,7 +56,8 @@ class AdminRestlet (val subscriber : Subscriber ) extends Default with Logging w
48
56
found match {
49
57
case Some (subscription) =>
50
58
subscriber.subUnsub(subscription.feedURL,subscription.topicURL,subscription.hubURL,false )
51
- response.setStatus(Status .SUCCESS_OK ," Created a new subscription for feed: " + subscription.feedURL)
59
+ response.setEntity(" Removing subscription for feed: " + subscription.feedURL, MediaType .TEXT_PLAIN )
60
+ response.setStatus(Status .SUCCESS_ACCEPTED )
52
61
case None => response.setStatus(Status .CLIENT_ERROR_NOT_FOUND )
53
62
}
54
63
}
@@ -58,7 +67,7 @@ class AdminRestlet (val subscriber : Subscriber ) extends Default with Logging w
58
67
if (id != null ){
59
68
val found = subscriber.activeSubscriptions.find( _.id.toString == id)
60
69
found match {
61
- case Some (subscription) => response.setEntity(subscription.toXML.toString , MediaType .TEXT_XML )
70
+ case Some (subscription) => response.setEntity(xmlPrinter.format( subscription.toXML) , MediaType .TEXT_XML )
62
71
case None => response.setStatus(Status .CLIENT_ERROR_NOT_FOUND )
63
72
}
64
73
}
0 commit comments