KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > webservices > adminapi > AtomAdminServlet


1 /*
2  * Copyright 2005 David M Johnson (For RSS and Atom In Action)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.roller.webservices.adminapi;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStreamReader JavaDoc;
20 import java.io.Writer JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServlet JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.jdom.Document;
29 import org.jdom.output.XMLOutputter;
30 import org.jdom.output.Format;
31 import org.apache.roller.webservices.adminapi.sdk.EntrySet;
32
33 /**
34  * Atom Admin Servlet implements the Atom Admin endpoint.
35  * This servlet simply delegates work to a particular handler object.
36  *
37  * @web.servlet name="AtomAdminServlet"
38  * @web.servlet-mapping url-pattern="/roller-services/aapp/*"
39  *
40  * @author jtb
41  */

42 public class AtomAdminServlet extends HttpServlet JavaDoc {
43     private static Log logger = LogFactory.getFactory().getInstance(AtomAdminServlet.class);
44     
45     /**
46      * Handles an Atom GET by calling handler and writing results to response.
47      */

48     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc {
49         try {
50             Handler handler = Handler.getHandler(req);
51             String JavaDoc userName = handler.getUserName();
52             
53             EntrySet c = handler.processGet();
54             
55             res.setStatus(HttpServletResponse.SC_OK);
56             res.setContentType("application/xml; charset=utf-8");
57             String JavaDoc s = c.toString();
58             Writer JavaDoc writer = res.getWriter();
59             writer.write(s);
60             writer.close();
61         } catch (HandlerException he) {
62             res.sendError(he.getStatus(), he.getMessage());
63             he.printStackTrace(res.getWriter());
64             logger.error(he);
65         }
66     }
67     
68     /**
69      * Handles an Atom POST by calling handler to identify URI, reading/parsing
70      * data, calling handler and writing results to response.
71      */

72     protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc {
73         try {
74             Handler handler = Handler.getHandler(req);
75             String JavaDoc userName = handler.getUserName();
76             
77             EntrySet c = handler.processPost(new InputStreamReader JavaDoc(req.getInputStream()));
78             
79             res.setStatus(HttpServletResponse.SC_CREATED);
80             res.setContentType("application/xml; charset=utf-8");
81             String JavaDoc s = c.toString();
82             Writer JavaDoc writer = res.getWriter();
83             writer.write(s);
84             writer.close();
85         } catch (HandlerException he) {
86             res.sendError(he.getStatus(), he.getMessage());
87             he.printStackTrace(res.getWriter());
88             logger.error(he);
89         }
90     }
91     
92     /**
93      * Handles an Atom PUT by calling handler to identify URI, reading/parsing
94      * data, calling handler and writing results to response.
95      */

96     protected void doPut(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc {
97         try {
98             Handler handler = Handler.getHandler(req);
99             String JavaDoc userName = handler.getUserName();
100             
101             EntrySet c = handler.processPut(new InputStreamReader JavaDoc(req.getInputStream()));
102             
103             res.setStatus(HttpServletResponse.SC_OK);
104             res.setContentType("application/xml; charset=utf-8");
105             String JavaDoc s = c.toString();
106             Writer JavaDoc writer = res.getWriter();
107             writer.write(s);
108             writer.close();
109         } catch (HandlerException he) {
110             res.sendError(he.getStatus(), he.getMessage());
111             he.printStackTrace(res.getWriter());
112             logger.error(he);
113         }
114     }
115     
116     /**
117      * Handle Atom Admin DELETE by calling appropriate handler.
118      */

119     protected void doDelete(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc {
120         try {
121             Handler handler = Handler.getHandler(req);
122             String JavaDoc userName = handler.getUserName();
123             
124             EntrySet es = handler.processDelete();
125             
126             res.setStatus(HttpServletResponse.SC_OK);
127             res.setContentType("application/xml; charset=utf-8");
128             String JavaDoc s = es.toString();
129             Writer JavaDoc writer = res.getWriter();
130             writer.write(s);
131             writer.close();
132         } catch (HandlerException he) {
133             res.sendError(he.getStatus(), he.getMessage());
134             he.printStackTrace(res.getWriter());
135             logger.error(he);
136         }
137     }
138 }
139
Popular Tags