001: package org.tigris.scarab.screens.admin;
002:
003: /*
004: * ================================================================
005: * Copyright (c) 2000-2002 CollabNet. All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions are
009: * met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in the
016: * documentation and/or other materials provided with the distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement: "This product includes
020: * software developed by Collab.Net <http://www.Collab.Net/>."
021: * Alternately, this acknowlegement may appear in the software itself, if
022: * and wherever such third-party acknowlegements normally appear.
023: *
024: * 4. The hosted project names must not be used to endorse or promote
025: * products derived from this software without prior written
026: * permission. For written permission, please contact info@collab.net.
027: *
028: * 5. Products derived from this software may not use the "Tigris" or
029: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
030: * prior written permission of Collab.Net.
031: *
032: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
033: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
034: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
035: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
036: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
037: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
038: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
039: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
040: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
041: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
042: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
043: *
044: * ====================================================================
045: *
046: * This software consists of voluntary contributions made by many
047: * individuals on behalf of Collab.Net.
048: */
049:
050: // Core Java Stuff
051: import java.util.Collection;
052:
053: // Turbine & Apache Commons Stuff
054: import org.apache.turbine.RunData;
055: import org.apache.turbine.TemplateContext;
056: import org.apache.commons.fileupload.FileItem;
057:
058: // Scarab Stuff
059: import org.tigris.scarab.screens.Default;
060: import org.tigris.scarab.util.xmlissues.ScarabIssues;
061: import org.tigris.scarab.services.security.ScarabSecurity;
062: import org.tigris.scarab.om.ScarabUser;
063: import org.tigris.scarab.tools.ScarabRequestTool;
064: import org.tigris.scarab.om.Module;
065: import org.tigris.scarab.util.xmlissues.ImportIssues;
066: import org.tigris.scarab.tools.ScarabLocalizationTool;
067:
068: /**
069: * Loads XML into Scarab via import, returning XML-formatted results
070: *
071: * @author <a href="mailto:mmurphy@collab.net">Mark L. Murphy</a>
072: * @version $Id: XMLImportIssuesResults.java 10004 2006-02-03 21:29:55Z hair $
073: */
074: public class XMLImportIssuesResults extends Default {
075: private static final int MIN_XML_SIZE = 1;
076: private static final int RESULT_OK = 0;
077: private static final int RESULT_ERROR_EXCEPTION = 100;
078: private static final int RESULT_ERROR_XML_MISSING = 101;
079: private static final int RESULT_ERROR_UNAUTHORIZED = 102;
080: private static final int RESULT_ERROR_INVALID_ISSUE_DATA = 103;
081:
082: /**
083: * Builds up the context for display of variables on the page.
084: *
085: * Runs import of POSTed issue.
086: *
087: * @param data Turbine run data
088: * @param context Velocity template context
089: */
090: public void doBuildTemplate(final RunData data,
091: final TemplateContext context) throws Exception {
092:
093: String resultString = "";
094: int resultCode = RESULT_OK;
095: Collection importErrors = null;
096: ScarabIssues si = null;
097:
098: super .doBuildTemplate(data, context);
099: final ScarabLocalizationTool l10n = getLocalizationTool(context);
100:
101: if (isImportAuthorized(data)) {
102: final FileItem issuesToImport = data.getParameters()
103: .getFileItem("issues");
104:
105: if (issuesToImport != null
106: && issuesToImport.getSize() >= MIN_XML_SIZE) {
107: try {
108: final ImportIssues importIssues = new ImportIssues();
109: final ScarabRequestTool scarabR = getScarabRequestTool(context);
110: final String type = data.getParameters().getString(
111: "xmlFormat");
112:
113: importErrors = importIssues.runImport(
114: issuesToImport, scarabR.getCurrentModule(),
115: ImportIssues.ImportType.valueOf(type));
116:
117: si = importIssues.getScarabIssuesBeanReader();
118: if (importErrors != null && !importErrors.isEmpty()) {
119: resultCode = RESULT_ERROR_INVALID_ISSUE_DATA;
120: resultString = l10n.get("ProcessingErrors");
121: }
122: } catch (Exception e) {
123: resultCode = RESULT_ERROR_EXCEPTION;
124: // TODO: Improve localizability of this text.
125: resultString = l10n.get("ProcessingErrors") + ": "
126: + e.getMessage();
127: }
128: } else {
129: resultCode = RESULT_ERROR_XML_MISSING;
130: resultString = l10n.get("MissingXML");
131: }
132: } else {
133: resultCode = RESULT_ERROR_UNAUTHORIZED;
134: resultString = l10n.get("Unauthorized");
135: }
136:
137: context.put("resultString", resultString);
138: context.put("resultCode", new Integer(resultCode));
139: context.put("importErrors", importErrors);
140: context.put("issues", si);
141:
142: final String format = data.getParameters().getString("format");
143: if (format != null && format.equals("xml")) {
144: final String result = org.apache.turbine.modules.Module
145: .handleRequest(context,
146: "macros/XMLImportIssuesResultsMacro.vm");
147: data.getResponse().setContentType("text/plain");
148: data.getResponse().setContentLength(result.length());
149: data.getResponse().getOutputStream().print(result);
150:
151: // we already sent the response, there is no target to render
152: data.setTarget(null);
153: }
154: }
155:
156: /**
157: * Indicates if this request is authorized.
158: *
159: * Overridden so we always return XML for this request, with that XML
160: * containing an error message if unauthorized. Otherwise, requesting this
161: * page might sometimes return HTML (error page) and sometimes return XML
162: * (authorized request), which will make parsing by automation clients
163: * difficult.
164: *
165: * @param data Turbine run data.
166: *
167: * @return Boolean indicating if authorized.
168: */
169: protected boolean isAuthorized(RunData data) throws Exception {
170: return (true);
171: }
172:
173: /**
174: * Indicates if this import request is authorized
175: *
176: * @param data Turbine run data.
177: *
178: * @return Boolean indicating whether or not authorized.
179: */
180: private boolean isImportAuthorized(RunData data) throws Exception {
181: String perm = ScarabSecurity
182: .getScreenPermission("admin.XMLImportIssuesResults.vm");
183: TemplateContext context = getTemplateContext(data);
184: ScarabRequestTool scarabR = getScarabRequestTool(context);
185: Module currentModule = scarabR.getCurrentModule();
186: ScarabUser user = (ScarabUser) data.getUser();
187: boolean result = false;
188:
189: if (perm != null) // Is there is a permission for this?
190: {
191: if (user.hasLoggedIn()
192: && user.hasPermission(perm, currentModule)) {
193: result = true; // Confirmed user has permission
194: }
195: } else {
196: result = true; // No permission = unsecured
197: }
198:
199: return (result);
200: }
201: }
|