001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
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.outerj.daisy.frontend;
017:
018: import org.apache.cocoon.environment.Request;
019: import org.outerj.daisy.repository.Repository;
020:
021: public class RequestUtil {
022: public static String getStringParameter(Request request,
023: String paramName) throws Exception {
024: return getStringParameter(request, paramName, null);
025: }
026:
027: public static String getStringParameter(Request request,
028: String paramName, String defaultValue) throws Exception {
029: String value = request.getParameter(paramName);
030:
031: if (value == null && defaultValue == null) {
032: throw new Exception("Invalid request: parameter \""
033: + paramName + "\" is missing.");
034: } else if (value == null) {
035: return defaultValue;
036: } else {
037: return value;
038: }
039: }
040:
041: public static int getIntParameter(Request request, String paramName)
042: throws Exception {
043: String value = request.getParameter(paramName);
044:
045: if (value == null) {
046: throw new Exception("Invalid request: parameter \""
047: + paramName + "\" is missing.");
048: }
049:
050: try {
051: return Integer.parseInt(value);
052: } catch (NumberFormatException e) {
053: throw new Exception("Invalid request: parameter \""
054: + paramName
055: + "\" does not contain a valid integer value: \""
056: + value + "\".");
057: }
058: }
059:
060: public static int getIntParameter(Request request,
061: String paramName, int defaultValue) throws Exception {
062: String value = request.getParameter(paramName);
063:
064: if (value == null) {
065: return defaultValue;
066: }
067:
068: try {
069: return Integer.parseInt(value);
070: } catch (NumberFormatException e) {
071: throw new Exception("Invalid request: parameter \""
072: + paramName
073: + "\" does not contain a valid integer value: \""
074: + value + "\".");
075: }
076: }
077:
078: public static long getLongParameter(Request request,
079: String paramName) throws Exception {
080: String value = request.getParameter(paramName);
081:
082: if (value == null) {
083: throw new Exception("Invalid request: parameter \""
084: + paramName + "\" is missing.");
085: }
086:
087: try {
088: return Long.parseLong(value);
089: } catch (NumberFormatException e) {
090: throw new Exception("Invalid request: parameter \""
091: + paramName
092: + "\" does not contain a valid long value: \""
093: + value + "\".");
094: }
095: }
096:
097: public static long getLongParameter(Request request,
098: String paramName, long defaultValue) throws Exception {
099: String value = request.getParameter(paramName);
100:
101: if (value == null) {
102: return defaultValue;
103: }
104:
105: try {
106: return Long.parseLong(value);
107: } catch (NumberFormatException e) {
108: throw new Exception("Invalid request: parameter \""
109: + paramName
110: + "\" does not contain a valid long value: \""
111: + value + "\".");
112: }
113: }
114:
115: public static boolean getBooleanParameter(Request request,
116: String paramName, boolean defaultValue) throws Exception {
117: String value = request.getParameter(paramName);
118:
119: if (value == null) {
120: return defaultValue;
121: } else {
122: return value.equalsIgnoreCase("true");
123: }
124: }
125:
126: public static String getServer(Request request) {
127: String server = request.getScheme() + "://"
128: + request.getServerName();
129: if (request.getServerPort() != 80)
130: server += ":" + request.getServerPort();
131: return server;
132: }
133:
134: public static long getBranchId(Request request,
135: long defaultBranchId, Repository repository)
136: throws Exception {
137: String branchParam = request.getParameter("branch");
138: return getBranchId(branchParam, defaultBranchId, repository);
139: }
140:
141: public static long getBranchId(String branch, long defaultBranchId,
142: Repository repository) throws Exception {
143: if (branch != null && branch.length() > 0) {
144: if (Character.isDigit(branch.charAt(0))) {
145: try {
146: return Long.parseLong(branch);
147: } catch (NumberFormatException e) {
148: throw new Exception(
149: "Invalid branch specification: \"" + branch
150: + "\".");
151: }
152: } else {
153: return repository.getVariantManager().getBranch(branch,
154: false).getId();
155: }
156: } else {
157: return defaultBranchId;
158: }
159: }
160:
161: public static long getLanguageId(Request request,
162: long defaultLanguageId, Repository repository)
163: throws Exception {
164: String languageParam = request.getParameter("language");
165: return getLanguageId(languageParam, defaultLanguageId,
166: repository);
167: }
168:
169: public static long getLanguageId(String language,
170: long defaultLanguageId, Repository repository)
171: throws Exception {
172: if (language != null && language.length() > 0) {
173: if (Character.isDigit(language.charAt(0))) {
174: try {
175: return Long.parseLong(language);
176: } catch (NumberFormatException e) {
177: throw new Exception(
178: "Invalid language specification: \""
179: + language + "\".");
180: }
181: } else {
182: return repository.getVariantManager().getLanguage(
183: language, false).getId();
184: }
185: } else {
186: return defaultLanguageId;
187: }
188: }
189:
190: /**
191: * Some browser (Internet Explorer is one) provide the complete file path in
192: * the upload file name, this method strips that.
193: */
194: public static String removePathFromUploadFileName(String fileName) {
195: int pos = fileName.lastIndexOf('\\');
196: if (pos == -1)
197: pos = fileName.lastIndexOf('/');
198: if (pos != -1)
199: fileName = fileName.substring(pos + 1);
200: return fileName;
201: }
202:
203: }
|