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.sitecontext;
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 org.apache.log4j.Logger;
031: import java.util.List;
032: import java.util.ArrayList;
033: import org.apache.commons.lang.StringUtils;
034: import java.util.Iterator;
035: import com.methodhead.aikp.AikpForm;
036:
037: public class SiteContextForm extends AikpForm implements Serializable {
038:
039: public void reset(ActionMapping mapping, HttpServletRequest request) {
040:
041: super .reset(mapping, request);
042:
043: //
044: // convert the domains text to a list of domain names
045: //
046: String domainsText = StringUtils.trimToEmpty((String) request
047: .getParameter("domainsText"));
048:
049: String[] domains = domainsText.split("\n");
050:
051: List list = new ArrayList();
052: for (int i = 0; i < domains.length; i++) {
053: String s = StringUtils.strip(domains[i]);
054: if (!StringUtils.isBlank(s))
055: list.add(s);
056: }
057:
058: set("domains", list);
059:
060: if (logger_.isDebugEnabled()) {
061: logger_.debug("Parsed " + list.size()
062: + " domains from domainsText");
063: }
064: }
065:
066: public ActionErrors doValidate(ActionMapping mapping,
067: HttpServletRequest request, ActionErrors errors) {
068:
069: String path = (String) get("path");
070:
071: //
072: // validate domains
073: //
074: SiteContext siteContext = new SiteContext();
075: List list = (List) get("domains");
076: for (Iterator iter = list.iterator(); iter.hasNext();) {
077: String domain = (String) iter.next();
078:
079: //
080: // check length
081: //
082: if (domain.length() > 67) {
083: errors.add("domainsText", new ActionError(
084: "mhf.invaliddomain", domain));
085: break;
086: }
087:
088: //
089: // make sure it's got valid characters
090: //
091: if (!domain.matches("[\\-A-Za-z0-9.]+")) {
092: errors.add("domainsText", new ActionError(
093: "mhf.invaliddomain", domain));
094: break;
095: }
096:
097: //
098: // make sure a domain doesn't already exist
099: //
100: if (siteContext.loadForDomainAndPath(domain, path)
101: && ("saveNew".equals(get("action")) || !get("id")
102: .equals(siteContext.get("id").toString()))) {
103: errors.add("domainsText", new ActionError(
104: "mhf.domainexists", domain, path));
105: break;
106: }
107: }
108:
109: return errors;
110: }
111:
112: private static Logger logger_ = Logger
113: .getLogger(SiteContextForm.class);
114: }
|