htiextension/com.nokia.s60tools.hticonnection/src/com/nokia/s60tools/hticonnection/services/ftpservice/UploadFileRequest.java
author dpodwall
Tue, 12 Jan 2010 13:17:53 -0600
changeset 0 61163b28edca
permissions -rw-r--r--
initial EPL conversion
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     1
/*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     3
* All rights reserved.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     4
* This component and the accompanying materials are made available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     6
* which accompanies this distribution, and is available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     8
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     9
* Initial Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    11
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    12
* Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    13
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    14
* Description:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    15
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    16
*/
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    17
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    18
package com.nokia.s60tools.hticonnection.services.ftpservice;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    19
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    20
import com.nokia.HTI.BaseService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    21
import com.nokia.HTI.HTIMessage;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    22
import com.nokia.HTI.IService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    23
import com.nokia.s60tools.hticonnection.core.RequestResult;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    24
import com.nokia.s60tools.hticonnection.exceptions.HTIException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    25
import com.nokia.s60tools.hticonnection.resources.Messages;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    26
import com.nokia.s60tools.hticonnection.services.IFTPListener;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    27
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    28
/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    29
 * Callable object that can be used to wait for request to complete.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    30
 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    31
public class UploadFileRequest extends AbstractFileTransferRequest{
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    32
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    33
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    34
	 * Name used for request.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    35
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    36
	private static final String REQUEST_NAME = "Upload file"; //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    37
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    38
	// Settings for upload file request
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    39
	private final byte[] fileData;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    40
	private final long timeout;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    41
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    42
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    43
	 * Uploads file to device
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    44
	 * @param fileData  Bytes read from local file
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    45
	 * @param remoteFile File in target where to write data
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    46
	 * @param listener Listener for this request. Or null if information is not needed.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    47
	 * @param timeout Timeout for request
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    48
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    49
	public UploadFileRequest(byte[] fileData, String remoteFile, IFTPListener listener, long timeout){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    50
		super(remoteFile, listener, REQUEST_NAME);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    51
		this.fileData = fileData;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    52
		this.timeout = timeout;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    53
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    54
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    55
	/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    56
	 * @see com.nokia.s60tools.hticonnection.core.AbstractRequest#createService()
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    57
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    58
	public BaseService createService(){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    59
		return new com.nokia.HTI.FTPService.FTPService();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    60
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    61
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    62
	/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    63
	 * @see com.nokia.s60tools.hticonnection.core.AbstractRequest#invokeService(com.nokia.HTI.IService)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    64
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    65
	public RequestResult invokeService(IService service) throws Exception{
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    66
		informStarted();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    67
		// Checking if request has been canceled.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    68
		if(requestCanceled) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    69
			throw new HTIException(REQUEST_NAME + Messages.getString("UploadFileRequest.RequestCanceled.Exception.Msg0"), true, true); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    70
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    71
		setStartedService((com.nokia.HTI.FTPService.FTPService)service);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    72
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    73
		// Sending upload file request.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    74
		HTIMessage result;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    75
		try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    76
			result = ((com.nokia.HTI.FTPService.FTPService)service)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    77
					.uploadFile(fileData, remoteFile, timeout);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    78
		} finally {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    79
			setStartedService(null);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    80
			// Checking if request was canceled while uploading file.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    81
			if(requestCanceled) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    82
				resultFutureTask.cancel(false);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    83
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    84
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    85
		return new RequestResult(result);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    86
    }
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    87
}