KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > util > WeblogSearchRequest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.util;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.roller.RollerException;
25 import org.apache.roller.model.RollerFactory;
26 import org.apache.roller.model.WeblogManager;
27 import org.apache.roller.pojos.WeblogCategoryData;
28 import org.apache.roller.util.URLUtilities;
29
30
31 /**
32  * Represents a request for a weblog preview.
33  */

34 public class WeblogSearchRequest extends WeblogRequest {
35     
36     private static Log log = LogFactory.getLog(WeblogSearchRequest.class);
37     
38     private static final String JavaDoc SEARCH_SERVLET = "/roller-ui/rendering/search";
39     
40     // lightweight attributes
41
private String JavaDoc query = null;
42     private int pageNum = 0;
43     private String JavaDoc weblogCategoryName = null;
44     
45     // heavyweight attributes
46
private WeblogCategoryData weblogCategory = null;
47     
48     
49     public WeblogSearchRequest() {}
50     
51     
52     public WeblogSearchRequest(HttpServletRequest JavaDoc request)
53             throws InvalidRequestException {
54         
55         // let our parent take care of their business first
56
// parent determines weblog handle and locale if specified
57
super(request);
58         
59         String JavaDoc servlet = request.getServletPath();
60         
61         // we only want the path info left over from after our parents parsing
62
String JavaDoc pathInfo = this.getPathInfo();
63         
64         // was this request bound for the search servlet?
65
if(servlet == null || !SEARCH_SERVLET.equals(servlet)) {
66             throw new InvalidRequestException("not a weblog search request, "+
67                     request.getRequestURL());
68         }
69         
70         if(pathInfo != null) {
71             throw new InvalidRequestException("invalid path info, "+
72                     request.getRequestURL());
73         }
74         
75         
76         /*
77          * parse request parameters
78          *
79          * the only params we currently care about are:
80          * q - specifies the search query
81          * pageNum - specifies what pageNum # to display
82          * cat - limit results to a certain weblogCategoryName
83          */

84         if(request.getParameter("q") != null &&
85                 request.getParameter("q").trim().length() > 0) {
86             this.query = request.getParameter("q");
87         }
88         
89         if(request.getParameter("page") != null) {
90             String JavaDoc pageInt = request.getParameter("page");
91             try {
92                 this.pageNum = Integer.parseInt(pageInt);
93             } catch(NumberFormatException JavaDoc e) {
94                 // ignored, bad input
95
}
96         }
97         
98         if(request.getParameter("cat") != null &&
99                 request.getParameter("cat").trim().length() > 0) {
100             this.weblogCategoryName =
101                     URLUtilities.decode(request.getParameter("cat"));
102             
103             // all categories must start with a /
104
if(!this.weblogCategoryName.startsWith("/")) {
105                 this.weblogCategoryName = "/"+this.weblogCategoryName;
106             }
107         }
108     }
109
110     public String JavaDoc getQuery() {
111         return query;
112     }
113
114     public void setQuery(String JavaDoc query) {
115         this.query = query;
116     }
117
118     public int getPageNum() {
119         return pageNum;
120     }
121
122     public void setPageNum(int pageNum) {
123         this.pageNum = pageNum;
124     }
125
126     public String JavaDoc getWeblogCategoryName() {
127         return weblogCategoryName;
128     }
129
130     public void setWeblogCategoryName(String JavaDoc weblogCategory) {
131         this.weblogCategoryName = weblogCategory;
132     }
133
134     public WeblogCategoryData getWeblogCategory() {
135         
136         if(weblogCategory == null && weblogCategoryName != null) {
137             try {
138                 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
139                 weblogCategory = wmgr.getWeblogCategoryByPath(getWeblog(), weblogCategoryName);
140             } catch (RollerException ex) {
141                 log.error("Error getting weblog category "+weblogCategoryName, ex);
142             }
143         }
144         
145         return weblogCategory;
146     }
147
148     public void setWeblogCategory(WeblogCategoryData weblogCategory) {
149         this.weblogCategory = weblogCategory;
150     }
151     
152 }
153
Popular Tags