|
| 1 | +/** |
| 2 | + * Copyright 2013 Sean Kavanagh - [email protected] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.keybox.manage.action; |
| 17 | + |
| 18 | +import com.keybox.common.util.AuthUtil; |
| 19 | +import com.keybox.manage.db.SystemStatusDB; |
| 20 | +import com.keybox.manage.model.HostSystem; |
| 21 | +import com.keybox.manage.model.SchSession; |
| 22 | +import com.keybox.manage.util.DBUtils; |
| 23 | +import com.keybox.manage.util.SSHUtil; |
| 24 | +import com.opensymphony.xwork2.ActionSupport; |
| 25 | +import org.apache.struts2.convention.annotation.Action; |
| 26 | +import org.apache.struts2.convention.annotation.Result; |
| 27 | +import org.apache.struts2.interceptor.ServletRequestAware; |
| 28 | +import org.apache.struts2.interceptor.ServletResponseAware; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +import javax.servlet.http.HttpServletRequest; |
| 33 | +import javax.servlet.http.HttpServletResponse; |
| 34 | +import java.io.File; |
| 35 | +import java.io.FileInputStream; |
| 36 | +import java.util.*; |
| 37 | + |
| 38 | +public class DownloadAction extends ActionSupport implements ServletRequestAware, ServletResponseAware { |
| 39 | + |
| 40 | + private static Logger log = LoggerFactory.getLogger(DownloadAction.class); |
| 41 | + |
| 42 | + String downloadContentType; |
| 43 | + String downloadFileName; |
| 44 | + List<Long> idList = new ArrayList<Long>(); |
| 45 | + String pullFile = "~/full_path_to_file"; |
| 46 | + List<HostSystem> hostSystemList; |
| 47 | + HostSystem pendingSystemStatus; |
| 48 | + HostSystem currentSystemStatus; |
| 49 | + HttpServletRequest servletRequest; |
| 50 | + HttpServletResponse servletResponse; |
| 51 | + FileInputStream fileInputStream = null; |
| 52 | + |
| 53 | + public static final String DOWNLOAD_PATH = DBUtils.class.getClassLoader().getResource(".").getPath() + "../download"; |
| 54 | + |
| 55 | + |
| 56 | + @Action(value = "/admin/setDownload", |
| 57 | + results = { |
| 58 | + @Result(name = "success", location = "/admin/download.jsp") |
| 59 | + } |
| 60 | + ) |
| 61 | + public String setDownload() throws Exception { |
| 62 | + Long userId= AuthUtil.getUserId(servletRequest.getSession()); |
| 63 | + |
| 64 | + SystemStatusDB.setInitialSystemStatus(idList, userId, AuthUtil.getUserType(servletRequest.getSession())); |
| 65 | + return SUCCESS; |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + @Action(value = "/admin/pull", |
| 71 | + results = { |
| 72 | + @Result(name = "input", location = "/admin/download.jsp"), |
| 73 | + @Result(name = "success", location = "/admin/download_result.jsp") |
| 74 | + } |
| 75 | + ) |
| 76 | + public String pull() { |
| 77 | + |
| 78 | + Long userId=AuthUtil.getUserId(servletRequest.getSession()); |
| 79 | + Long sessionId=AuthUtil.getSessionId(servletRequest.getSession()); |
| 80 | + try { |
| 81 | + |
| 82 | + //get next pending system |
| 83 | + pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId); |
| 84 | + if (pendingSystemStatus != null) { |
| 85 | + //get session for system |
| 86 | + SchSession session = null; |
| 87 | + for(Integer instanceId : SecureShellAction.getUserSchSessionMap().get(sessionId).getSchSessionMap().keySet()) { |
| 88 | + |
| 89 | + //if host system id matches pending system then download |
| 90 | + if(pendingSystemStatus.getId().equals(SecureShellAction.getUserSchSessionMap().get(sessionId).getSchSessionMap().get(instanceId).getHostSystem().getId())){ |
| 91 | + session = SecureShellAction.getUserSchSessionMap().get(sessionId).getSchSessionMap().get(instanceId); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if(session!=null) { |
| 96 | + |
| 97 | + // clean filename paths out |
| 98 | + downloadFileName = cleanFilenameForDownloadFile(pullFile); |
| 99 | + |
| 100 | + //pull download from system, saving with systemId and userId to make it unique for that system/user |
| 101 | + currentSystemStatus = SSHUtil.pullDownload(pendingSystemStatus, session.getSession(), pullFile, |
| 102 | + buildFilePath(cleanDownloadFilename(pullFile, pendingSystemStatus.getId()), userId)); |
| 103 | + |
| 104 | + //update system status |
| 105 | + SystemStatusDB.updateSystemStatus(currentSystemStatus, userId); |
| 106 | + |
| 107 | + pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId); |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + hostSystemList = SystemStatusDB.getAllSystemStatus(userId); |
| 112 | + |
| 113 | + |
| 114 | + } catch (Exception e) { |
| 115 | + log.error(e.toString(), e); |
| 116 | + addActionError("Exception occurred during pull to keybox: " + e.getMessage()); |
| 117 | + return ERROR; |
| 118 | + } |
| 119 | + |
| 120 | + return SUCCESS; |
| 121 | + } |
| 122 | + |
| 123 | + @Action(value = "/admin/download", |
| 124 | + results = { |
| 125 | + @Result(name = "success", |
| 126 | + type = "stream", |
| 127 | + params = { "contentType", "application/octet-stream", |
| 128 | + "inputName", "fileInputStream", |
| 129 | + "contentDisposition", "attachment;filename=${downloadFileName}" } |
| 130 | + ), |
| 131 | + @Result(name = "error", location = "/admin/error.jsp") |
| 132 | + } |
| 133 | + ) |
| 134 | + public String download() { |
| 135 | + |
| 136 | + // https://www.mkyong.com/struts/struts-download-file-from-website-example/ |
| 137 | + // http://stackoverflow.com/questions/13973264/download-file-in-struts-2-using-annotation |
| 138 | + Long userId = AuthUtil.getUserId(servletRequest.getSession()); |
| 139 | + try { |
| 140 | + |
| 141 | + // build the file path based on the user downloading |
| 142 | + String filePath = buildFilePath(downloadFileName, userId); |
| 143 | + |
| 144 | + File fileToDownload = new File(filePath); |
| 145 | + if (!fileToDownload.isFile()) { |
| 146 | + // file requested doesn't exist |
| 147 | + addActionError("File (" + downloadFileName + ") does not exist"); |
| 148 | + return ERROR; |
| 149 | + } |
| 150 | + |
| 151 | + fileInputStream = new FileInputStream(fileToDownload); |
| 152 | + } catch (Exception e) { |
| 153 | + log.error(e.toString(), e); |
| 154 | + addActionError("Exception occurred during download: " + e.getMessage()); |
| 155 | + return ERROR; |
| 156 | + } |
| 157 | + |
| 158 | + return SUCCESS; |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Validates all fields for downloading a file |
| 164 | + */ |
| 165 | + public void validateDownload() { |
| 166 | + |
| 167 | + if (pullFile == null || pullFile.trim().equals("")) { |
| 168 | + addFieldError("pullFile", "Required"); |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + public String getDownloadContentType() { |
| 175 | + return downloadContentType; |
| 176 | + } |
| 177 | + |
| 178 | + public void setDownloadContentType(String downloadContentType) { |
| 179 | + this.downloadContentType = downloadContentType; |
| 180 | + } |
| 181 | + |
| 182 | + public String getDownloadFileName() { |
| 183 | + return downloadFileName; |
| 184 | + } |
| 185 | + |
| 186 | + public void setDownloadFileName(String downloadFileName) { |
| 187 | + this.downloadFileName = downloadFileName; |
| 188 | + } |
| 189 | + |
| 190 | + public String getPullFile() { |
| 191 | + return pullFile; |
| 192 | + } |
| 193 | + |
| 194 | + public void setPullFile(String pullDir) { |
| 195 | + this.pullFile = pullDir; |
| 196 | + } |
| 197 | + |
| 198 | + public List<Long> getIdList() { |
| 199 | + return idList; |
| 200 | + } |
| 201 | + |
| 202 | + public void setIdList(List<Long> idList) { |
| 203 | + this.idList = idList; |
| 204 | + } |
| 205 | + |
| 206 | + public List<HostSystem> getHostSystemList() { |
| 207 | + return hostSystemList; |
| 208 | + } |
| 209 | + |
| 210 | + public void setHostSystemList(List<HostSystem> hostSystemList) { |
| 211 | + this.hostSystemList = hostSystemList; |
| 212 | + } |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + public HttpServletRequest getServletRequest() { |
| 217 | + return servletRequest; |
| 218 | + } |
| 219 | + |
| 220 | + public void setServletRequest(HttpServletRequest servletRequest) { |
| 221 | + this.servletRequest = servletRequest; |
| 222 | + } |
| 223 | + |
| 224 | + public HttpServletResponse getServletResponse() { |
| 225 | + return servletResponse; |
| 226 | + } |
| 227 | + |
| 228 | + public void setServletResponse(HttpServletResponse servletResponse) { |
| 229 | + this.servletResponse = servletResponse; |
| 230 | + } |
| 231 | + |
| 232 | + public HostSystem getPendingSystemStatus() { |
| 233 | + return pendingSystemStatus; |
| 234 | + } |
| 235 | + |
| 236 | + public void setPendingSystemStatus(HostSystem pendingSystemStatus) { |
| 237 | + this.pendingSystemStatus = pendingSystemStatus; |
| 238 | + } |
| 239 | + |
| 240 | + public HostSystem getCurrentSystemStatus() { |
| 241 | + return currentSystemStatus; |
| 242 | + } |
| 243 | + |
| 244 | + public void setCurrentSystemStatus(HostSystem currentSystemStatus) { |
| 245 | + this.currentSystemStatus = currentSystemStatus; |
| 246 | + } |
| 247 | + |
| 248 | + private static String cleanFilenameForDownloadFile(String filePath) { |
| 249 | + return filePath.replaceAll("[^A-Za-z0-9]", ""); |
| 250 | + } |
| 251 | + |
| 252 | + // builds up the full download path, for the given user |
| 253 | + private static String buildFilePath(String filenameWithSystem, long userId) { |
| 254 | + return DOWNLOAD_PATH + "/" + filenameWithSystem + "_" + userId; |
| 255 | + } |
| 256 | + |
| 257 | + private static String cleanDownloadFilename(String pullPath, long systemId) { |
| 258 | + return cleanFilenameForDownloadFile(pullPath) + "_" + systemId; |
| 259 | + } |
| 260 | + |
| 261 | + public FileInputStream getFileInputStream() { |
| 262 | + return fileInputStream; |
| 263 | + } |
| 264 | + |
| 265 | + public void setFileInputStream(FileInputStream fileInputStream) { |
| 266 | + this.fileInputStream = fileInputStream; |
| 267 | + } |
| 268 | +} |
0 commit comments