001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * 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. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.util;
020:
021: import javax.servlet.http.HttpServletRequest;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.roller.RollerException;
025: import org.apache.roller.business.RollerFactory;
026: import org.apache.roller.business.WeblogManager;
027: import org.apache.roller.pojos.WeblogCategoryData;
028: import org.apache.roller.util.URLUtilities;
029:
030: /**
031: * Represents a request for a weblog preview.
032: */
033: public class WeblogSearchRequest extends WeblogRequest {
034:
035: private static Log log = LogFactory
036: .getLog(WeblogSearchRequest.class);
037:
038: private static final String SEARCH_SERVLET = "/roller-ui/rendering/search";
039:
040: // lightweight attributes
041: private String query = null;
042: private int pageNum = 0;
043: private String weblogCategoryName = null;
044:
045: // heavyweight attributes
046: private WeblogCategoryData weblogCategory = null;
047:
048: public WeblogSearchRequest() {
049: }
050:
051: public WeblogSearchRequest(HttpServletRequest request)
052: throws InvalidRequestException {
053:
054: // let our parent take care of their business first
055: // parent determines weblog handle and locale if specified
056: super (request);
057:
058: String servlet = request.getServletPath();
059:
060: // we only want the path info left over from after our parents parsing
061: String pathInfo = this .getPathInfo();
062:
063: // was this request bound for the search servlet?
064: if (servlet == null || !SEARCH_SERVLET.equals(servlet)) {
065: throw new InvalidRequestException(
066: "not a weblog search request, "
067: + request.getRequestURL());
068: }
069:
070: if (pathInfo != null) {
071: throw new InvalidRequestException("invalid path info, "
072: + request.getRequestURL());
073: }
074:
075: /*
076: * parse request parameters
077: *
078: * the only params we currently care about are:
079: * q - specifies the search query
080: * pageNum - specifies what pageNum # to display
081: * cat - limit results to a certain weblogCategoryName
082: */
083: if (request.getParameter("q") != null
084: && request.getParameter("q").trim().length() > 0) {
085: this .query = request.getParameter("q");
086: }
087:
088: if (request.getParameter("page") != null) {
089: String pageInt = request.getParameter("page");
090: try {
091: this .pageNum = Integer.parseInt(pageInt);
092: } catch (NumberFormatException e) {
093: // ignored, bad input
094: }
095: }
096:
097: if (request.getParameter("cat") != null
098: && request.getParameter("cat").trim().length() > 0) {
099: this .weblogCategoryName = URLUtilities.decode(request
100: .getParameter("cat"));
101:
102: // all categories must start with a /
103: if (!this .weblogCategoryName.startsWith("/")) {
104: this .weblogCategoryName = "/" + this .weblogCategoryName;
105: }
106: }
107: }
108:
109: public String getQuery() {
110: return query;
111: }
112:
113: public void setQuery(String query) {
114: this .query = query;
115: }
116:
117: public int getPageNum() {
118: return pageNum;
119: }
120:
121: public void setPageNum(int pageNum) {
122: this .pageNum = pageNum;
123: }
124:
125: public String getWeblogCategoryName() {
126: return weblogCategoryName;
127: }
128:
129: public void setWeblogCategoryName(String weblogCategory) {
130: this .weblogCategoryName = weblogCategory;
131: }
132:
133: public WeblogCategoryData getWeblogCategory() {
134:
135: if (weblogCategory == null && weblogCategoryName != null) {
136: try {
137: WeblogManager wmgr = RollerFactory.getRoller()
138: .getWeblogManager();
139: weblogCategory = wmgr.getWeblogCategoryByPath(
140: getWeblog(), weblogCategoryName);
141: } catch (RollerException ex) {
142: log.error("Error getting weblog category "
143: + weblogCategoryName, ex);
144: }
145: }
146:
147: return weblogCategory;
148: }
149:
150: public void setWeblogCategory(WeblogCategoryData weblogCategory) {
151: this.weblogCategory = weblogCategory;
152: }
153:
154: }
|