KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > sitecontext > SiteContextForm


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.sitecontext;
22
23 import java.io.Serializable JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.action.ActionErrors;
28 import org.apache.struts.action.ActionError;
29 import org.apache.struts.validator.DynaValidatorForm;
30 import org.apache.log4j.Logger;
31 import java.util.List JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import org.apache.commons.lang.StringUtils;
34 import java.util.Iterator JavaDoc;
35 import com.methodhead.aikp.AikpForm;
36
37 public class SiteContextForm
38 extends
39   AikpForm
40 implements
41   Serializable JavaDoc {
42
43   public void reset(
44     ActionMapping mapping,
45     HttpServletRequest JavaDoc request ) {
46
47     super.reset( mapping, request );
48
49     //
50
// convert the domains text to a list of domain names
51
//
52
String JavaDoc domainsText = StringUtils.trimToEmpty( ( String JavaDoc )request.getParameter( "domainsText" ) );
53     
54     String JavaDoc[] domains = domainsText.split( "\n" );
55
56     List JavaDoc list = new ArrayList JavaDoc();
57     for ( int i = 0; i < domains.length; i++ ) {
58       String JavaDoc s = StringUtils.strip( domains[ i ] );
59       if ( !StringUtils.isBlank( s ) )
60         list.add( s );
61     }
62
63     set( "domains", list );
64
65     if ( logger_.isDebugEnabled() ) {
66       logger_.debug( "Parsed " + list.size() + " domains from domainsText" );
67     }
68   }
69
70   public ActionErrors doValidate(
71     ActionMapping mapping,
72     HttpServletRequest JavaDoc request,
73     ActionErrors errors ) {
74
75     String JavaDoc path = ( String JavaDoc )get( "path" );
76
77     //
78
// validate domains
79
//
80
SiteContext siteContext = new SiteContext();
81     List JavaDoc list = ( List JavaDoc )get( "domains" );
82     for ( Iterator JavaDoc iter = list.iterator(); iter.hasNext(); ) {
83       String JavaDoc domain = ( String JavaDoc )iter.next();
84
85       //
86
// check length
87
//
88
if ( domain.length() > 67 ) {
89         errors.add(
90           "domainsText", new ActionError( "mhf.invaliddomain", domain ) );
91         break;
92       }
93
94       //
95
// make sure it's got valid characters
96
//
97
if ( !domain.matches( "[\\-A-Za-z0-9.]+" ) ) {
98         errors.add(
99           "domainsText", new ActionError( "mhf.invaliddomain", domain ) );
100         break;
101       }
102
103       //
104
// make sure a domain doesn't already exist
105
//
106
if ( siteContext.loadForDomainAndPath( domain, path ) &&
107            ( "saveNew".equals( get( "action" ) ) ||
108              !get( "id" ).equals( siteContext.get( "id" ).toString() ) ) ) {
109         errors.add(
110           "domainsText", new ActionError( "mhf.domainexists", domain, path ) );
111         break;
112       }
113     }
114
115     return errors;
116   }
117
118   private static Logger logger_ = Logger.getLogger( SiteContextForm.class );
119 }
120
Popular Tags