KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > screens > admin > XMLImportIssuesResults


1 package org.tigris.scarab.screens.admin;
2
3 /*
4  * ================================================================
5  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement: "This product includes
20  * software developed by Collab.Net <http://www.Collab.Net/>."
21  * Alternately, this acknowlegement may appear in the software itself, if
22  * and wherever such third-party acknowlegements normally appear.
23  *
24  * 4. The hosted project names must not be used to endorse or promote
25  * products derived from this software without prior written
26  * permission. For written permission, please contact info@collab.net.
27  *
28  * 5. Products derived from this software may not use the "Tigris" or
29  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
30  * prior written permission of Collab.Net.
31  *
32  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
36  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
38  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
40  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  *
44  * ====================================================================
45  *
46  * This software consists of voluntary contributions made by many
47  * individuals on behalf of Collab.Net.
48  */

49
50 // Core Java Stuff
51
import java.util.Collection JavaDoc;
52
53 // Turbine & Apache Commons Stuff
54
import org.apache.turbine.RunData;
55 import org.apache.turbine.TemplateContext;
56 import org.apache.commons.fileupload.FileItem;
57
58 // Scarab Stuff
59
import org.tigris.scarab.screens.Default;
60 import org.tigris.scarab.util.xmlissues.ScarabIssues;
61 import org.tigris.scarab.services.security.ScarabSecurity;
62 import org.tigris.scarab.om.ScarabUser;
63 import org.tigris.scarab.tools.ScarabRequestTool;
64 import org.tigris.scarab.om.Module;
65 import org.tigris.scarab.util.xmlissues.ImportIssues;
66 import org.tigris.scarab.tools.ScarabLocalizationTool;
67
68 /**
69  * Loads XML into Scarab via import, returning XML-formatted results
70  *
71  * @author <a HREF="mailto:mmurphy@collab.net">Mark L. Murphy</a>
72  * @version $Id: XMLImportIssuesResults.java 8469 2003-08-11 21:25:34Z dlr $
73  */

74 public class XMLImportIssuesResults extends Default
75 {
76     private static final int MIN_XML_SIZE = 1;
77     private static final int RESULT_OK = 0;
78     private static final int RESULT_ERROR_EXCEPTION = 100;
79     private static final int RESULT_ERROR_XML_MISSING = 101;
80     private static final int RESULT_ERROR_UNAUTHORIZED = 102;
81     private static final int RESULT_ERROR_INVALID_ISSUE_DATA = 103;
82
83     /**
84      * Builds up the context for display of variables on the page.
85      *
86      * Runs import of POSTed issue.
87      *
88      * @param data Turbine run data
89      * @param context Velocity template context
90      */

91     public void doBuildTemplate(RunData data, TemplateContext context)
92         throws Exception JavaDoc
93     {
94         String JavaDoc resultString = "";
95         int resultCode = RESULT_OK;
96         Collection JavaDoc importErrors = null;
97         ScarabIssues si = null;
98
99         super.doBuildTemplate(data, context);
100         ScarabLocalizationTool l10n = getLocalizationTool(context);
101         
102         if (isImportAuthorized(data))
103         {
104             FileItem issuesToImport = data.getParameters()
105                 .getFileItem("issues");
106             if (issuesToImport != null
107                 && issuesToImport.getSize() >= MIN_XML_SIZE)
108             {
109                 try
110                 {
111                     ImportIssues importIssues = new ImportIssues();
112                     ScarabRequestTool scarabR = getScarabRequestTool(context);
113                     importErrors = importIssues.runImport(issuesToImport,
114                         scarabR.getCurrentModule());
115                     si = importIssues.getScarabIssuesBeanReader();
116                     if (importErrors != null && !importErrors.isEmpty())
117                     {
118                         resultCode = RESULT_ERROR_INVALID_ISSUE_DATA;
119                         resultString = l10n.get("ProcessingErrors");
120                     }
121                 }
122                 catch (Exception JavaDoc e)
123                 {
124                     resultCode = RESULT_ERROR_EXCEPTION;
125                     // TODO: Improve localizability of this text.
126
resultString = l10n.get("ProcessingErrors") + ": "
127                         + e.getMessage();
128                 }
129             }
130             else
131             {
132                 resultCode = RESULT_ERROR_XML_MISSING;
133                 resultString = l10n.get("MissingXML");
134             }
135         }
136         else
137         {
138             resultCode = RESULT_ERROR_UNAUTHORIZED;
139             resultString = l10n.get("Unauthorized");
140         }
141
142         context.put("resultString", resultString);
143         context.put("resultCode", new Integer JavaDoc(resultCode));
144         context.put("importErrors", importErrors);
145         context.put("issues", si);
146         
147         String JavaDoc format = data.getParameters().getString("format");
148         if (format != null && format.equals("xml"))
149         {
150             String JavaDoc result = org.apache.turbine.modules.Module.handleRequest
151                 (context, "macros/XMLImportIssuesResultsMacro.vm");
152             data.getResponse().setContentType("text/plain");
153             data.getResponse().setContentLength(result.length());
154             data.getResponse().getOutputStream().print(result);
155     
156             // we already sent the response, there is no target to render
157
data.setTarget(null);
158         }
159     }
160
161     /**
162      * Indicates if this request is authorized.
163      *
164      * Overridden so we always return XML for this request, with that XML
165      * containing an error message if unauthorized. Otherwise, requesting this
166      * page might sometimes return HTML (error page) and sometimes return XML
167      * (authorized request), which will make parsing by automation clients
168      * difficult.
169      *
170      * @param data Turbine run data.
171      *
172      * @return Boolean indicating if authorized.
173      */

174     protected boolean isAuthorized(RunData data) throws Exception JavaDoc
175     {
176         return (true);
177     }
178
179     /**
180      * Indicates if this import request is authorized
181      *
182      * @param data Turbine run data.
183      *
184      * @return Boolean indicating whether or not authorized.
185      */

186     private boolean isImportAuthorized(RunData data)
187         throws Exception JavaDoc
188     {
189         String JavaDoc perm = ScarabSecurity.getScreenPermission
190             ("admin.XMLImportIssuesResults.vm");
191         TemplateContext context = getTemplateContext(data);
192         ScarabRequestTool scarabR = getScarabRequestTool(context);
193         Module currentModule = scarabR.getCurrentModule();
194         ScarabUser user = (ScarabUser) data.getUser();
195         boolean result = false;
196
197         if (perm != null) // Is there is a permission for this?
198
{
199             if (user.hasLoggedIn() &&
200                     user.hasPermission(perm, currentModule))
201             {
202                 result = true; // Confirmed user has permission
203
}
204         }
205         else
206         {
207             result = true; // No permission = unsecured
208
}
209
210         return (result);
211     }
212 }
213
214
Popular Tags