001: /*
002: * Copyright 2005 David M Johnson (For RSS and Atom In Action)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.roller.webservices.adminapi;
017:
018: import java.io.IOException;
019: import java.io.InputStreamReader;
020: import java.io.Writer;
021:
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServlet;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.jdom.Document;
029: import org.jdom.output.XMLOutputter;
030: import org.jdom.output.Format;
031: import org.apache.roller.webservices.adminapi.sdk.EntrySet;
032:
033: /**
034: * Atom Admin Servlet implements the Atom Admin endpoint.
035: * This servlet simply delegates work to a particular handler object.
036: *
037: * @web.servlet name="AtomAdminServlet"
038: * @web.servlet-mapping url-pattern="/roller-services/aapp/*"
039: *
040: * @author jtb
041: */
042: public class AtomAdminServlet extends HttpServlet {
043: private static Log logger = LogFactory.getFactory().getInstance(
044: AtomAdminServlet.class);
045:
046: /**
047: * Handles an Atom GET by calling handler and writing results to response.
048: */
049: protected void doGet(HttpServletRequest req, HttpServletResponse res)
050: throws ServletException, IOException {
051: try {
052: Handler handler = Handler.getHandler(req);
053: String userName = handler.getUserName();
054:
055: EntrySet c = handler.processGet();
056:
057: res.setStatus(HttpServletResponse.SC_OK);
058: res.setContentType("application/xml; charset=utf-8");
059: String s = c.toString();
060: Writer writer = res.getWriter();
061: writer.write(s);
062: writer.close();
063: } catch (HandlerException he) {
064: res.sendError(he.getStatus(), he.getMessage());
065: he.printStackTrace(res.getWriter());
066: logger.error(he);
067: }
068: }
069:
070: /**
071: * Handles an Atom POST by calling handler to identify URI, reading/parsing
072: * data, calling handler and writing results to response.
073: */
074: protected void doPost(HttpServletRequest req,
075: HttpServletResponse res) throws ServletException,
076: IOException {
077: try {
078: Handler handler = Handler.getHandler(req);
079: String userName = handler.getUserName();
080:
081: EntrySet c = handler.processPost(new InputStreamReader(req
082: .getInputStream()));
083:
084: res.setStatus(HttpServletResponse.SC_CREATED);
085: res.setContentType("application/xml; charset=utf-8");
086: String s = c.toString();
087: Writer writer = res.getWriter();
088: writer.write(s);
089: writer.close();
090: } catch (HandlerException he) {
091: res.sendError(he.getStatus(), he.getMessage());
092: he.printStackTrace(res.getWriter());
093: logger.error(he);
094: }
095: }
096:
097: /**
098: * Handles an Atom PUT by calling handler to identify URI, reading/parsing
099: * data, calling handler and writing results to response.
100: */
101: protected void doPut(HttpServletRequest req, HttpServletResponse res)
102: throws ServletException, IOException {
103: try {
104: Handler handler = Handler.getHandler(req);
105: String userName = handler.getUserName();
106:
107: EntrySet c = handler.processPut(new InputStreamReader(req
108: .getInputStream()));
109:
110: res.setStatus(HttpServletResponse.SC_OK);
111: res.setContentType("application/xml; charset=utf-8");
112: String s = c.toString();
113: Writer writer = res.getWriter();
114: writer.write(s);
115: writer.close();
116: } catch (HandlerException he) {
117: res.sendError(he.getStatus(), he.getMessage());
118: he.printStackTrace(res.getWriter());
119: logger.error(he);
120: }
121: }
122:
123: /**
124: * Handle Atom Admin DELETE by calling appropriate handler.
125: */
126: protected void doDelete(HttpServletRequest req,
127: HttpServletResponse res) throws ServletException,
128: IOException {
129: try {
130: Handler handler = Handler.getHandler(req);
131: String userName = handler.getUserName();
132:
133: EntrySet es = handler.processDelete();
134:
135: res.setStatus(HttpServletResponse.SC_OK);
136: res.setContentType("application/xml; charset=utf-8");
137: String s = es.toString();
138: Writer writer = res.getWriter();
139: writer.write(s);
140: writer.close();
141: } catch (HandlerException he) {
142: res.sendError(he.getStatus(), he.getMessage());
143: he.printStackTrace(res.getWriter());
144: logger.error(he);
145: }
146: }
147: }
|