KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > replacementproxy > forms > ReplacementForm


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.replacementproxy.forms;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.Globals;
31 import org.apache.struts.action.ActionErrors;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionMessage;
34
35 import com.sslexplorer.core.forms.CoreForm;
36 import com.sslexplorer.replacementproxy.Replacement;
37
38 /**
39  *
40  */

41 public class ReplacementForm extends CoreForm {
42
43     final static List JavaDoc REPLACE_TYPES = new ArrayList JavaDoc();
44     Replacement replacement;
45
46     boolean editing = false;
47
48     static Log log = LogFactory.getLog(ReplacementForm.class);
49
50     /*
51      * (non-Javadoc)
52      *
53      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping,
54      * javax.servlet.http.HttpServletRequest)
55      */

56     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
57         if ("commit".equals(request.getParameter("action"))) {
58             ActionErrors msgs = new ActionErrors();
59             if ((getReplacement().getReplaceType() == Replacement.REPLACEMENT_TYPE_RECEIVED_CONTENT || getReplacement()
60                             .getReplaceType() == Replacement.REPLACEMENT_TYPE_SENT_CONTENT)
61                             && getReplacement().getMimeType().trim().equals("")) {
62                 msgs.add(Globals.MESSAGE_KEY, new ActionMessage("createReplacement.error.mimeTypeRequired"));
63             }
64             if (getReplacement().getMatchPattern().trim().equals("")) {
65                 msgs.add(Globals.MESSAGE_KEY, new ActionMessage("createReplacement.error.noMatchPattern"));
66             }
67             try {
68                 Pattern.compile(getReplacement().getMatchPattern().trim());
69             } catch (Exception JavaDoc e) {
70                 msgs.add(Globals.MESSAGE_KEY, new ActionMessage("createReplacement.error.invalidMatchPattern", e.getMessage()));
71             }
72             if (!replacement.getSitePattern().trim().equals("")) {
73                 try {
74                     Pattern.compile(getReplacement().getSitePattern().trim());
75                 } catch (Exception JavaDoc e) {
76                     msgs.add(Globals.MESSAGE_KEY, new ActionMessage("createReplacement.error.invalidSitePattern", e.getMessage()));
77                 }
78             }
79             return msgs;
80         } else {
81             return null;
82         }
83     }
84
85     public Replacement getReplacement() {
86         return replacement;
87     }
88
89     public void setReplacement(Replacement replacement) {
90         this.replacement = replacement;
91     }
92
93     public String JavaDoc getUpdateAction() {
94         return editing ? "/editReplacement.do" : "/createReplacement.do";
95     }
96
97     public void initialize(Replacement replacement, boolean editing) {
98         this.replacement = replacement;
99         this.editing = editing;
100     }
101
102     public void setEditing(boolean editing) {
103         this.editing = editing;
104     }
105
106     public boolean isEditing() {
107         return editing;
108     }
109
110     public void setReplaceType(String JavaDoc replaceType) {
111         try {
112             replacement.setReplaceType(Integer.parseInt(replaceType));
113         } catch (NumberFormatException JavaDoc nfe) {
114             log.error(nfe);
115         }
116     }
117
118     public String JavaDoc getReplaceType() {
119         return String.valueOf(replacement.getReplaceType());
120     }
121 }
Popular Tags