001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import java.io.Serializable;
024: import javax.servlet.http.HttpServletRequest;
025:
026: import org.apache.struts.action.ActionMapping;
027: import org.apache.struts.action.ActionErrors;
028: import org.apache.struts.action.ActionError;
029: import org.apache.struts.validator.DynaValidatorForm;
030: import java.util.List;
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import org.apache.struts.util.LabelValueBean;
034: import com.methodhead.sitecontext.SiteContext;
035: import com.methodhead.auth.AuthUtil;
036: import com.methodhead.tree.Tree;
037: import org.apache.commons.lang.StringUtils;
038: import java.io.File;
039: import org.apache.commons.validator.GenericValidator;
040:
041: public final class ModuleForm extends DynaValidatorForm implements
042: Serializable {
043:
044: public void reset(ActionMapping mapping, HttpServletRequest request) {
045:
046: set("fragments", ShimUtils.getHtmlFragmentOptions(request));
047: }
048:
049: protected void validateConfigureNavModule(ActionMapping mapping,
050: HttpServletRequest request, ActionErrors errors) {
051:
052: if (errors.size() == 0) {
053:
054: //
055: // jsp
056: //
057: if ("jsp".equals(get("type"))) {
058: String jsp = (String) get("jsp");
059:
060: if (StringUtils.isBlank(jsp)) {
061: errors.add("jsp", new ActionError(
062: "shim.moduleForm.missingJsp"));
063: } else {
064:
065: //
066: // make sure it ends in jsp
067: //
068: if (!jsp.endsWith(".jsp"))
069: errors.add("jsp", new ActionError(
070: "shim.moduleForm.invalidJsp"));
071:
072: //
073: // verify jsp exists
074: //
075: File file = new File(request.getSession()
076: .getServletContext().getRealPath(
077: "/WEB-INF/resources/"
078: + SiteContext.getContext(
079: request).getInt(
080: "id") + "/"
081: + get("jsp")));
082:
083: if (!file.exists() || !file.isFile())
084: errors.add("jsp", new ActionError(
085: "shim.moduleForm.jspDoesNotExist"));
086: }
087: }
088:
089: //
090: // folding
091: //
092: else if ("folding".equals(get("type"))) {
093:
094: //
095: // integer settings
096: //
097: if (!GenericValidator.isInt((String) get("startlev")))
098: errors.add("startlev", new ActionError(
099: "shim.moduleForm.invalidStartLev"));
100:
101: if (!GenericValidator.isInt((String) get("contextlev")))
102: errors.add("contextlev", new ActionError(
103: "shim.moduleForm.invalidStartLev"));
104:
105: if (!GenericValidator.isInt((String) get("depthlev")))
106: errors.add("depthlev", new ActionError(
107: "shim.moduleForm.invalidStartLev"));
108:
109: if (!GenericValidator.isInt((String) get("toplev")))
110: errors.add("toplev", new ActionError(
111: "shim.moduleForm.invalidTopLev"));
112:
113: //
114: // html fragments
115: //
116: String s = null;
117:
118: s = (String) get("header");
119: if (s.length() > 512)
120: errors.add("header", new ActionError(
121: "shim.moduleForm.headerTooLong"));
122:
123: s = (String) get("link");
124: if (s.length() > 512)
125: errors.add("link", new ActionError(
126: "shim.moduleForm.linkTooLong"));
127:
128: s = (String) get("curlink");
129: if (s.length() > 512)
130: errors.add("curlink", new ActionError(
131: "shim.moduleForm.curlinkTooLong"));
132:
133: s = (String) get("footer");
134: if (s.length() > 512)
135: errors.add("footer", new ActionError(
136: "shim.moduleForm.footerTooLong"));
137: }
138: }
139: }
140:
141: protected void validateConfigureIncludeModule(
142: ActionMapping mapping, HttpServletRequest request,
143: ActionErrors errors) {
144:
145: if (errors.size() == 0) {
146:
147: String jsp = (String) get("jsp");
148:
149: if (StringUtils.isBlank(jsp)) {
150: errors.add("jsp", new ActionError(
151: "shim.moduleForm.missingJsp"));
152: return;
153: }
154:
155: //
156: // make sure it ends in jsp
157: //
158: if (!jsp.endsWith(".jsp"))
159: errors.add("jsp", new ActionError(
160: "shim.moduleForm.invalidJsp"));
161:
162: //
163: // verify jsp exists
164: //
165: else {
166: File file = new File(request.getSession()
167: .getServletContext().getRealPath(
168: "/WEB-INF/resources/"
169: + SiteContext.getContext(
170: request).getInt("id")
171: + "/" + get("jsp")));
172:
173: if (!file.exists() || !file.isFile())
174: errors.add("jsp", new ActionError(
175: "shim.moduleForm.jspDoesNotExist"));
176: }
177: }
178: }
179:
180: public ActionErrors validate(ActionMapping mapping,
181: HttpServletRequest request) {
182:
183: //
184: // don't do anything if we're cancelling
185: //
186: if (!StringUtils.isBlank((String) get("cancel")))
187: return new ActionErrors();
188:
189: ActionErrors errors = super .validate(mapping, request);
190:
191: if ("/configureNavModule".equals(mapping.getPath()))
192: validateConfigureNavModule(mapping, request, errors);
193: else if ("/configureIncludeModule".equals(mapping.getPath()))
194: validateConfigureIncludeModule(mapping, request, errors);
195: else if ("/configureTextModule".equals(mapping.getPath())) {
196: // nothing here
197: } else
198: throw new ShimException("Unexpected mapping \""
199: + mapping.getPath() + "\".");
200:
201: return errors;
202: }
203: }
|