KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > blogs > TrackBackPingServlet


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution Sàrl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * ----- END LICENSE BLOCK -----
36  */

37
38 package org.jahia.blogs;
39
40 import org.jahia.blogs.actions.AddTrackBackPingAction;
41
42 import javax.servlet.ServletException JavaDoc;
43 import javax.servlet.http.HttpServlet JavaDoc;
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45 import javax.servlet.http.HttpServletResponse JavaDoc;
46
47 import org.apache.log4j.Logger;
48
49 import java.io.OutputStream JavaDoc;
50 import java.io.IOException JavaDoc;
51
52 /**
53  * Simple Servlet reacting to POST methods containing TrackBack ping requests
54  *
55  * @author Xavier Lawrence
56  */

57 public class TrackBackPingServlet extends HttpServlet JavaDoc {
58     
59     public static final String JavaDoc XML_HEADER =
60             "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
61     public static final String JavaDoc RESPONSE = "<response>";
62     public static final String JavaDoc END_RESPONSE = "</response>";
63     public static final String JavaDoc ERROR = "<error>";
64     public static final String JavaDoc END_ERROR = "</error>";
65     public static final String JavaDoc MESSAGE = "<message>";
66     public static final String JavaDoc END_MESSAGE = "</message>";
67     
68     // log4j logger
69
static Logger log = Logger.getLogger(TrackBackPingServlet.class);
70     
71     public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
72     throws ServletException JavaDoc, IOException JavaDoc {
73         StringBuffer JavaDoc resp = new StringBuffer JavaDoc();
74         
75         // Set ThreadLocal variables
76
ServletResources.setCurrentRequest(request);
77         ServletResources.setCurrentResponse(response);
78         ServletResources.setCurrentConfig(super.getServletConfig());
79         
80         try {
81             String JavaDoc postID = request.getParameter("entryID");
82             if (postID == null || postID.length() < 1) {
83                 throw new ServletException JavaDoc("Missing required parameter \"entryID\"");
84             }
85             
86             String JavaDoc url = request.getParameter("url");
87             if (url == null || url.length() < 1) {
88                 throw new ServletException JavaDoc("Missing required parameter \"url\"");
89             }
90             
91             String JavaDoc title = request.getParameter("title");
92             String JavaDoc blogName = request.getParameter("blog_name");
93             String JavaDoc excerpt = request.getParameter("excerpt");
94             
95             log.debug("\n\nAddTrackBackPingAction: "+postID+", "+url+", "+title+
96                     ", "+blogName+", "+excerpt);
97             
98             AddTrackBackPingAction action = new AddTrackBackPingAction(postID,
99                     title, excerpt, url, blogName);
100             action.execute();
101             
102             resp.append(XML_HEADER);
103             resp.append(RESPONSE);
104             resp.append(ERROR);
105             resp.append(0);
106             resp.append(END_ERROR);
107             resp.append(END_RESPONSE);
108             
109         } catch (Exception JavaDoc e) {
110             resp.append(XML_HEADER);
111             resp.append(RESPONSE);
112             resp.append(ERROR);
113             resp.append(1);
114             resp.append(END_ERROR);
115             resp.append(MESSAGE);
116             resp.append(e.getMessage());
117             resp.append(END_MESSAGE);
118             resp.append(END_RESPONSE);
119             
120             response.setStatus(500);
121             
122             e.printStackTrace();
123         }
124         
125         OutputStream JavaDoc output = response.getOutputStream();
126         output.write(resp.toString().getBytes("UTF-8"));
127         output.flush();
128         
129         // Cleanup ThreadLocal variables
130
ServletResources.setCurrentRequest(null);
131         ServletResources.setCurrentResponse(null);
132         ServletResources.setCurrentConfig(null);
133     }
134 }
135
Popular Tags