Skip to main content
summaryrefslogtreecommitdiffstats
blob: d33bf47d93632606899cf3ec188d26d94051382c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*******************************************************************************
 * Copyright (c) 2006, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Initial Contributors:
 * The following IBM employees contributed to the Remote System Explorer
 * component that contains this file: David McKnight, Kushal Munir, 
 * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, 
 * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley.
 * 
 * Contributors:
 * Xuan Chen (IBM) - [160775] [api] rename (at least within a zip) blocks UI thread
 * Noriaki Takatsu (IBM)  - [220126] [dstore][api][breaking] Single process server for multiple clients
 *******************************************************************************/

package org.eclipse.rse.internal.dstore.universal.miners.filesystem;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.eclipse.dstore.core.model.DE;
import org.eclipse.dstore.core.model.DataElement;
import org.eclipse.dstore.core.model.DataStore;
import org.eclipse.dstore.core.model.IByteConverter;
import org.eclipse.rse.dstore.universal.miners.ICancellableHandler;
import org.eclipse.rse.dstore.universal.miners.IUniversalDataStoreConstants;
import org.eclipse.rse.dstore.universal.miners.UniversalFileSystemMiner;
import org.eclipse.rse.dstore.universal.miners.UniversalServerUtilities;
import org.eclipse.rse.services.clientserver.IServiceConstants;
import org.eclipse.rse.services.clientserver.archiveutils.AbsoluteVirtualPath;
import org.eclipse.rse.services.clientserver.archiveutils.ISystemArchiveHandler;
import org.eclipse.rse.services.clientserver.archiveutils.VirtualChild;
import org.eclipse.dstore.core.server.SecuredThread;


public class UniversalDownloadHandler extends SecuredThread implements ICancellableHandler
{

	private boolean _isDone = false;
	private UniversalFileSystemMiner _miner;
	private DataElement _status;
	private DataElement _cmdElement;
	private boolean _isCancelled= false;
	
	public UniversalDownloadHandler(DataStore dataStore, UniversalFileSystemMiner miner, DataElement cmdElement, 
			DataElement status)
	{
		super(dataStore);
		_miner = miner;
		_status = status;
		_cmdElement = cmdElement;
	}

	public void run()
	{
		super.run();
		
		handleDownload(_cmdElement, _status);
		_isDone = true;
	}
	
	public boolean isDone()
	{
		return _isDone;
	}
	
	public boolean isCancelled()
	{
		return _isCancelled;
	}
	
	public void cancel()
	{
		_dataStore.trace("cancelling download"); //$NON-NLS-1$
		_isCancelled = true;
	}


	protected DataElement handleDownload(DataElement theElement, DataElement status)
	{
		DataElement arg1 = _miner.getCommandArgument(theElement, 1);
		String elementType = arg1.getType();
		String remotePath = arg1.getName();

		int buffer_size = IUniversalDataStoreConstants.BUFFER_SIZE;
		DataElement bufferSizeElement = _dataStore.find(theElement, DE.A_TYPE, "buffer_size", 1); //$NON-NLS-1$
		if (bufferSizeElement != null)
		{
		    try
		    {
		        buffer_size = Integer.parseInt(bufferSizeElement.getName());
		    }
		    catch (Exception e)
		    {			        
		    }
		}
		
		String resultType = null;
		String resultMessage = null;						
		
		
		FileInputStream inputStream = null;
		BufferedInputStream bufInputStream = null;


		try
		{
			if (elementType.equals(IUniversalDataStoreConstants.UNIVERSAL_FILE_DESCRIPTOR) || elementType.equals(IUniversalDataStoreConstants.UNIVERSAL_ARCHIVE_FILE_DESCRIPTOR) || elementType.equals(IUniversalDataStoreConstants.UNIVERSAL_VIRTUAL_FILE_DESCRIPTOR))
			{

				_dataStore.trace("download:" + remotePath + "," + elementType); //$NON-NLS-1$ //$NON-NLS-2$

				File file = new File(remotePath);


				if (elementType.equals(IUniversalDataStoreConstants.UNIVERSAL_VIRTUAL_FILE_DESCRIPTOR))
				{
					AbsoluteVirtualPath vpath = _miner.getAbsoluteVirtualPath(remotePath);

					ISystemArchiveHandler handler = _miner.getArchiveHandlerFor(vpath.getContainingArchiveString());
					if (handler == null)
					{
						status.setAttribute(DE.A_SOURCE, IServiceConstants.FAILED);
						resultType = IUniversalDataStoreConstants.DOWNLOAD_RESULT_IO_EXCEPTION;
						resultMessage = "Corrupted archive."; //$NON-NLS-1$
						_isDone = true;
						_dataStore.createObject(arg1, resultType, resultMessage);
						_dataStore.refresh(arg1);
						return _miner.statusDone(status);
					}
					VirtualChild vChild = handler.getVirtualFile(vpath.getVirtualPart(), null);
					file = vChild.getExtractedFile();

				}

				DataElement arg2 = _miner.getCommandArgument(theElement, 2);
//				DataElement arg3 = _miner.getCommandArgument(theElement, 3);
				_miner.getCommandArgument(theElement, 3);

				int mode = (Integer.valueOf(arg1.getSource())).intValue();
				String localPath = arg2.getName();

				boolean isText = (mode == IUniversalDataStoreConstants.TEXT_MODE);

//				String clientEncoding = null;

				if (isText)
				{
//					clientEncoding = arg2.getSource();
					arg2.getSource();
				}

				// Read in the file
				inputStream = new FileInputStream(file);
				bufInputStream = new BufferedInputStream(inputStream, buffer_size);

				int totalBytes = (int)file.length();
				int totalWritten = 0;

				boolean first = true;
				byte[] buffer = new byte[buffer_size];
				byte[] convBytes;
				int numToRead = 0;

				IByteConverter byteConverter = _dataStore.getByteConverter();
				byteConverter.setContext(file);

				int available = bufInputStream.available();

				while (available > 0 && !_isCancelled)
				{
					numToRead = (available < buffer_size) ? available : buffer_size;

					int bytesRead = bufInputStream.read(buffer, 0, numToRead);

					if (bytesRead == -1)
						break;
					if (isText)
					{
						convBytes = byteConverter.convertHostBytesToClientBytes(buffer, 0, bytesRead);

						if (first)
						{ // send first set of bytes
							first = false;
							_dataStore.updateFile(localPath, convBytes, convBytes.length, true);
						}
						else
						{ // append subsequent segments
							_dataStore.updateAppendFile(localPath, convBytes, convBytes.length, true);
						}
						totalWritten += convBytes.length;
					}
					else
					{

						if (first)
						{ // send first set of bytes
							first = false;
							_dataStore.updateFile(localPath, buffer, bytesRead, true);
						}
						else
						{ // append subsequent segments
							_dataStore.updateAppendFile(localPath, buffer, bytesRead, true);
						}
						totalWritten +=bytesRead;
					}

					status.setAttribute(DE.A_SOURCE, "sent "+totalWritten + " of "+totalBytes); //$NON-NLS-1$ //$NON-NLS-2$
					_dataStore.refresh(status);
					available = bufInputStream.available();
				}

				resultType = IUniversalDataStoreConstants.DOWNLOAD_RESULT_SUCCESS_TYPE;
				resultMessage = IUniversalDataStoreConstants.DOWNLOAD_RESULT_SUCCESS_MESSAGE;

			}
		}
		catch (FileNotFoundException e)
		{
			UniversalServerUtilities.logError(UniversalFileSystemMiner.CLASSNAME, "handleDownload: error reading file " + remotePath, e, _dataStore); //$NON-NLS-1$
			resultType = IUniversalDataStoreConstants.DOWNLOAD_RESULT_FILE_NOT_FOUND_EXCEPTION;
			resultMessage = e.getLocalizedMessage();
		}
		catch (UnsupportedEncodingException e)
		{
			UniversalServerUtilities.logError(UniversalFileSystemMiner.CLASSNAME, "handleDownload: error reading file " + remotePath, e, _dataStore); //$NON-NLS-1$
			resultType = IUniversalDataStoreConstants.DOWNLOAD_RESULT_UNSUPPORTED_ENCODING_EXCEPTION;
			resultMessage = e.getLocalizedMessage();
		}
		catch (IOException e)
		{
			UniversalServerUtilities.logError(UniversalFileSystemMiner.CLASSNAME, "handleDownload: error reading file " + remotePath, e, _dataStore); //$NON-NLS-1$
			resultType = IUniversalDataStoreConstants.DOWNLOAD_RESULT_IO_EXCEPTION;
			resultMessage = e.getLocalizedMessage();
		}
		catch (Exception e)
		{
			UniversalServerUtilities.logError(UniversalFileSystemMiner.CLASSNAME, "handleDownload: error reading file " + remotePath, e, _dataStore); //$NON-NLS-1$
			resultType = IUniversalDataStoreConstants.DOWNLOAD_RESULT_EXCEPTION;
			resultMessage = e.getLocalizedMessage();
		}
		finally
		{

			try
			{

				if (bufInputStream != null)
					bufInputStream.close();

			}
			catch (IOException e)
			{
				UniversalServerUtilities.logError(UniversalFileSystemMiner.CLASSNAME, "handleDownload: error closing reader on " + remotePath, e, _dataStore); //$NON-NLS-1$
				resultType = IUniversalDataStoreConstants.DOWNLOAD_RESULT_IO_EXCEPTION;
				resultMessage = e.getMessage();
			}
		}
		_isDone = true;

		_dataStore.createObject(arg1, resultType, resultMessage);
		_dataStore.refresh(arg1);
		
		return _miner.statusDone(status);
	}


}

Back to the top