001: package com.ice.jcvsweb.action;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.OutputStream;
006: import java.util.ArrayList;
007: import java.util.HashMap;
008: import javax.servlet.ServletContext;
009: import javax.servlet.http.HttpServlet;
010: import javax.servlet.http.HttpServletRequest;
011: import javax.servlet.http.HttpServletResponse;
012:
013: import org.apache.struts.action.ActionForm;
014: import org.apache.struts.action.ActionForward;
015: import org.apache.struts.action.ActionMapping;
016:
017: import com.ice.jcvsweb.bean.JCVSConfiguration;
018: import com.ice.jcvsweb.bean.JCVSEntry;
019: import com.ice.jcvsweb.bean.JCVSError;
020: import com.ice.jcvsweb.bean.JCVSProjectView;
021: import com.ice.jcvsweb.bean.JCVSUser;
022: import com.ice.jcvsweb.helper.ContextHelper;
023: import com.ice.jcvsweb.helper.ZipHelper;
024: import com.ice.jcvsweb.manager.JCVSConfigManager;
025: import com.ice.jcvsweb.manager.JCVSPermManager;
026:
027: public class SendZipArchive extends JCVSAction {
028: public ActionForward execute(ActionMapping mapping,
029: ActionForm form, HttpServletRequest request,
030: HttpServletResponse response) throws Exception {
031: String forwardAlias = "failure";
032: ArrayList pathBar = new ArrayList();
033: ServletContext ctx = request.getSession().getServletContext();
034: JCVSConfiguration config = this .getConfiguration(ctx);
035: JCVSUser user = this .establishUser(request);
036: JCVSPermManager permMgr = this .getPermissionManager(ctx);
037: JCVSProjectView view = this .establishView(request);
038: this .establishCommonArguments(request);
039:
040: ctx.log("[JCVSSendZIP] View '" + view + "'");
041:
042: if (user == null) {
043: JCVSError err = new JCVSError();
044: err.setException(new Throwable("NULL USER"));
045: err.setTitle("FATAL The user is not established.");
046: this .postAndLogError(request, err);
047: } else if (view == null) {
048: JCVSError err = new JCVSError();
049: err.setException(new Throwable("NULL VIEW"));
050: err.setTitle("FATAL View does not exist.");
051: this .postAndLogError(request, err);
052: } else if (!permMgr.getUserCanView(user, view.getProject())) {
053: forwardAlias = "notallowed";
054: this
055: .setReasonMessage(
056: request,
057: "This project has restricted access, and you do not "
058: + "have the needed permission to access the project.");
059: } else {
060: view.ensureViewIsOpen(ctx);
061: if (!view.isReady()) {
062: forwardAlias = "notready";
063: request.setAttribute("jcvsNotReady", view);
064: } else {
065: String viewDirPath = ContextHelper.getRealAbsolutePath(
066: ctx, view.getViewDirectory());
067:
068: String entryPath = this .getEntryPath(request);
069: if (entryPath.toLowerCase().endsWith(".zip")) {
070: int idx = entryPath.lastIndexOf("/");
071: if (idx == -1)
072: entryPath = "";
073: else
074: entryPath = entryPath.substring(0, idx);
075: }
076:
077: this .setEntryPath(request, entryPath);
078: pathBar = view.establishPathBar(entryPath);
079:
080: JCVSEntry dirEntry = view.getPackageEntry(entryPath);
081:
082: if (dirEntry == null) {
083: JCVSError err = new JCVSError();
084: err.setException(new Throwable("NULL DIR ENTRY"));
085: err.setTitle("FATAL The path '" + entryPath
086: + "' resulted in a NULL entry, "
087: + "which is a serious bug.");
088: this .postAndLogError(request, err);
089: } else {
090: File wdF = new File(viewDirPath + File.separator
091: + entryPath);
092:
093: if (!wdF.exists()) {
094: JCVSError err = new JCVSError();
095: err.setException(new Throwable(
096: "NONEXISTENT FILE"));
097: err.setTitle("FATAL The path '" + entryPath
098: + "' is non-existent at '"
099: + wdF.getPath() + "'.");
100: this .postAndLogError(request, err);
101: } else {
102: ZipHelper zippy = new ZipHelper();
103:
104: // REVIEW
105: // Globber avoid = new Globber();
106: // avoid.addMatchSpec( "CVS" );
107:
108: // UNDONE
109: String contentType = "application/zip";
110:
111: response.reset();
112: response.setContentType(contentType);
113: // response.setContentLength( (int) hashF.length() );
114:
115: String rootName = view.getProject().getDef()
116: .getKey()
117: + "-" + view.getKey();
118:
119: OutputStream os = response.getOutputStream();
120:
121: try {
122: zippy.writeZipArchive(os, rootName, wdF,
123: wdF, null, null);
124:
125: forwardAlias = null; // Success!
126: } catch (Exception ex) {
127: JCVSError err = new JCVSError();
128: err.setException(ex);
129: err
130: .setTitle("Failed downloading ZIP archive '"
131: + entryPath + "'.");
132: this.postAndLogError(request, err);
133: } finally {
134: os.close();
135: }
136: }
137: }
138: }
139: }
140:
141: if (forwardAlias != null) {
142: this.setPathBar(request, pathBar);
143: }
144:
145: return (forwardAlias == null) ? null : mapping
146: .findForward(forwardAlias);
147: }
148:
149: }
|