KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > RequestUtil


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16 package org.outerj.daisy.frontend;
17
18 import org.apache.cocoon.environment.Request;
19 import org.outerj.daisy.repository.Repository;
20
21 public class RequestUtil {
22     public static String JavaDoc getStringParameter(Request request, String JavaDoc paramName) throws Exception JavaDoc {
23         return getStringParameter(request, paramName, null);
24     }
25
26     public static String JavaDoc getStringParameter(Request request, String JavaDoc paramName, String JavaDoc defaultValue) throws Exception JavaDoc {
27         String JavaDoc value = request.getParameter(paramName);
28
29         if (value == null && defaultValue == null) {
30             throw new Exception JavaDoc("Invalid request: parameter \"" + paramName + "\" is missing.");
31         } else if (value == null) {
32             return defaultValue;
33         } else {
34             return value;
35         }
36     }
37
38     public static int getIntParameter(Request request, String JavaDoc paramName, int defaultValue) throws Exception JavaDoc {
39         String JavaDoc value = request.getParameter(paramName);
40
41         if (value == null) {
42             return defaultValue;
43         }
44
45         try {
46             return Integer.parseInt(value);
47         } catch (NumberFormatException JavaDoc e) {
48             throw new Exception JavaDoc("Invalid request: parameter \"" + paramName + "\" does not contain a valid integer value: \"" + value + "\".");
49         }
50     }
51
52     public static long getLongParameter(Request request, String JavaDoc paramName) throws Exception JavaDoc {
53         String JavaDoc value = request.getParameter(paramName);
54
55         if (value == null) {
56             throw new Exception JavaDoc("Invalid request: parameter \"" + paramName + "\" is missing.");
57         }
58
59         try {
60             return Long.parseLong(value);
61         } catch (NumberFormatException JavaDoc e) {
62             throw new Exception JavaDoc("Invalid request: parameter \"" + paramName + "\" does not contain a valid long value: \"" + value + "\".");
63         }
64     }
65
66     public static boolean getBooleanParameter(Request request, String JavaDoc paramName, boolean defaultValue) throws Exception JavaDoc {
67         String JavaDoc value = request.getParameter(paramName);
68
69         if (value == null) {
70             return defaultValue;
71         } else {
72             return value.equalsIgnoreCase("true");
73         }
74     }
75
76     public static String JavaDoc getServer(Request request) {
77         String JavaDoc server = request.getScheme() + "://" + request.getServerName();
78         if (request.getServerPort() != 80)
79             server += ":" + request.getServerPort();
80         return server;
81     }
82
83     public static long getBranchId(Request request, long defaultBranchId, Repository repository) throws Exception JavaDoc {
84         String JavaDoc branchParam = request.getParameter("branch");
85         return getBranchId(branchParam, defaultBranchId, repository);
86     }
87
88     public static long getBranchId(String JavaDoc branch, long defaultBranchId, Repository repository) throws Exception JavaDoc {
89         if (branch != null && branch.length() > 0) {
90             if (Character.isDigit(branch.charAt(0))) {
91                 try {
92                     return Long.parseLong(branch);
93                 } catch (NumberFormatException JavaDoc e) {
94                     throw new Exception JavaDoc("Invalid branch specification: \"" + branch + "\".");
95                 }
96             } else {
97                 return repository.getVariantManager().getBranch(branch, false).getId();
98             }
99         } else {
100             return defaultBranchId;
101         }
102     }
103
104     public static long getLanguageId(Request request, long defaultLanguageId, Repository repository) throws Exception JavaDoc {
105         String JavaDoc languageParam = request.getParameter("language");
106         return getLanguageId(languageParam, defaultLanguageId, repository);
107     }
108
109     public static long getLanguageId(String JavaDoc language, long defaultLanguageId, Repository repository) throws Exception JavaDoc {
110         if (language != null && language.length() > 0) {
111             if (Character.isDigit(language.charAt(0))) {
112                 try {
113                     return Long.parseLong(language);
114                 } catch (NumberFormatException JavaDoc e) {
115                     throw new Exception JavaDoc("Invalid language specification: \"" + language + "\".");
116                 }
117             } else {
118                 return repository.getVariantManager().getLanguage(language, false).getId();
119             }
120         } else {
121             return defaultLanguageId;
122         }
123     }
124
125     /**
126      * Some browser (Internet Explorer is one) provide the complete file path in
127      * the upload file name, this method strips that.
128      */

129     public static String JavaDoc removePathFromUploadFileName(String JavaDoc fileName) {
130         int pos = fileName.lastIndexOf('\\');
131         if (pos == -1)
132             pos = fileName.lastIndexOf('/');
133         if (pos != -1)
134             fileName = fileName.substring(pos + 1);
135         return fileName;
136     }
137
138 }
139
Popular Tags