View Javadoc

1   package net.sourceforge.blogentis.modules.screens;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: TrackBack.java,v 1.2 2004/10/28 10:41:40 tassos Exp $
23  //
24  
25  import java.util.Date;
26  
27  import net.sourceforge.blogentis.om.Comment;
28  import net.sourceforge.blogentis.trackback.TrackbackPostEditExtension;
29  import net.sourceforge.blogentis.turbine.BlogParameterParser;
30  import net.sourceforge.blogentis.utils.AbsoluteLinkURL;
31  import net.sourceforge.blogentis.utils.LinkFactoryService;
32  import net.sourceforge.blogentis.utils.MappedConfiguration;
33  import net.sourceforge.blogentis.utils.StringUtils;
34  
35  import org.apache.turbine.modules.screens.RawScreen;
36  import org.apache.turbine.util.RunData;
37  
38  /***
39   * @author abas
40   */
41  public class TrackBack
42          extends RawScreen {
43      public static final String CONTENT_TYPE_ATTRIBUTE = "net.sourceforge.blogentis.modules.screens.TrackBack.ContentType";
44  
45      public static final String RESULT_ATTRIBUTE = "net.sourceforge.blogentis.modules.screens.TrackBack.Result";
46  
47      protected String getContentType(RunData data) {
48          checkTrackBack(data);
49          return (String)data.getRequest().getAttribute(CONTENT_TYPE_ATTRIBUTE);
50      }
51  
52      protected void doOutput(RunData data)
53              throws Exception {
54          checkTrackBack(data);
55          byte result[] = data.getRequest().getAttribute(RESULT_ATTRIBUTE)
56              .toString().getBytes("utf-8");
57          data.getResponse().getOutputStream().write(result);
58      }
59  
60      private void checkTrackBack(RunData data) {
61          if (data.getRequest().getAttribute(RESULT_ATTRIBUTE) != null)
62              return;
63          BlogParameterParser bpp = (BlogParameterParser)data.getParameters();
64  
65          boolean isPostIt = false;
66          String failure = null;
67          if (bpp.getBlog() == null || bpp.getPost() == null
68              || bpp.getBlog().getBlogId() != bpp.getPost().getBlogId())
69              failure = "No such post found";
70  
71          MappedConfiguration conf = bpp.getBlog().getConfiguration();
72  
73          if (!conf.getBoolean(TrackbackPostEditExtension.T_BLOG_RECEIVE, false))
74              failure = "Disallowed by configuration";
75  
76          String title = bpp.getString("title", null);
77          String url = bpp.getString("url", bpp.getString("link", null));
78          String email = bpp.getString("author", bpp.getString("email", null));
79          String text = bpp.getString("excerpt", bpp.getString("comment", null));
80          String name = bpp.getString("blog_name", bpp.getString("name", null));
81  
82          if (url != null) {
83              if (conf
84                  .getBoolean(
85                              TrackbackPostEditExtension.T_BLOG_IGNORE_FROM_SERVER,
86                              false)
87                  && url.startsWith(LinkFactoryService.getBaseURI()))
88                  failure = "Disallowed by configuration";
89              if (conf
90                  .getBoolean(TrackbackPostEditExtension.T_BLOG_IGNORE_FROM_SELF,
91                              true)
92                  && url.startsWith(LinkFactoryService.getLink()
93                      .permaLink(bpp.getBlog()).toString()))
94                  failure = "Disallowed by configuration";
95          }
96  
97          if (bpp.getString("agent", null) != null)
98              isPostIt = true;
99  
100         if (failure == null) {
101             Comment c = new Comment();
102             c.setEmail(email);
103             String s = StringUtils.removeTags(text);
104             if (s.length() > 255)
105                 s = s.substring(0, 252) + "...";
106             c.setText(s);
107             c.setTitle(title);
108             c.setUrl(url);
109             c.setName(name);
110             c.setPostedTime(new Date());
111             if (email != null || text != null || url != null || name != null) {
112                 try {
113                     c.setPost(bpp.getPost());
114                     c.save();
115                 } catch (Exception e) {
116                     failure = "Database Exception";
117                 }
118             } else {
119                 failure = "Nothing specified in the trackback";
120             }
121         }
122         if (isPostIt) {
123             data.setRedirectURI(new AbsoluteLinkURL().setBlog(bpp.getBlog())
124                 .toString());
125             data.setStatusCode(302);
126             data.getRequest().setAttribute(RESULT_ATTRIBUTE, "");
127             data.getRequest().setAttribute(CONTENT_TYPE_ATTRIBUTE, "");
128         } else {
129             data.getRequest().setAttribute(CONTENT_TYPE_ATTRIBUTE, "text/xml");
130             String msg;
131             if (failure != null)
132                 msg = "<?xml version='1.0' encoding='utf-8'?>"
133                       + "<response><error>1</error>" + "<message>" + failure
134                       + "</message>" + "</response>";
135             else
136                 msg = "<?xml version='1.0' encoding='utf-8'?>"
137                       + "<response><error>0</error>" + "</response>";
138             data.getRequest().setAttribute(RESULT_ATTRIBUTE, msg);
139         }
140     }
141 }