JSP file upload. Conversion from FTP to SFTP - java

I have a JSP page which contains a link to upload a file. Initially the file transfer was happening using FTP. The server seems to transfer the file from client using the FTP only. I require to change the code to support SFTP now. Like the server initiated an FTP session on the client machine, is it possible for SFTP as well? here is the client code:
FTPClient client = new FTPClient();
FTPClient clientUser = new FTPClient(); //From ftp location (user saves file here)
try { //Location where user chooses file from
eas_user_import_ip_address_val = resovleJNDIResource.resolveJNDIString(eas_user_import_ip_address_tx);
eas_user_import_id_val = resovleJNDIResource.resolveJNDIString(eas_user_import_id_tx);
eas_user_import_pwd_val = resovleJNDIResource.resolveJNDIString(eas_user_import_pwd_tx);
eas_user_file_prefix_val= resovleJNDIResource.resolveJNDIString(eas_user_file_prefix_tx);
eas_user_file_prefix_val= eas_user_file_prefix_val.trim();
clientUser.connect(eas_user_import_ip_address_val);
int replyUser = clientUser.getReplyCode();
if(!FTPReply.isPositiveCompletion(replyUser)) {
ENTADJRuntime.getInstance().logError("FTPService", "Error connecting to:" + eas_user_import_ip_address_val + " reply code:" + replyUser);
clientUser.disconnect();
return false;
}
boolean loginUser = clientUser.login(eas_user_import_id_val, eas_user_import_pwd_val);
if (!loginUser) {
ENTADJRuntime.getInstance().logError("FTPService", "Error logging in to:" + eas_user_import_id_val);
return false;
}
//Location where file gets copied to.
eas_import_ip_address_val = resovleJNDIResource.resolveJNDIString(eas_import_ip_address_tx);
eas_import_id_val = resovleJNDIResource.resolveJNDIString(eas_import_id_tx);
eas_import_pwd_val = resovleJNDIResource.resolveJNDIString(eas_import_pwd_tx);
eas_part_file_prefix_val = resovleJNDIResource.resolveJNDIString(eas_part_file_prefix_tx);
eas_p2p_file_prefix_val = resovleJNDIResource.resolveJNDIString(eas_p2p_file_prefix_tx);
client.connect(eas_import_ip_address_val);
int reply = client.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ENTADJRuntime.getInstance().logError("FTPService", "Error connecting to:" + eas_import_ip_address_val + " reply code:" + reply);
client.disconnect();
return false;
}
boolean login = client.login(eas_import_id_val, eas_import_pwd_val);
if (!login) {
ENTADJRuntime.getInstance().logError("FTPService", "Error loging in to:" + eas_import_id_val);
return false;
}
//Loged in to From and To locations. Now transfer file
clientUser.setFileType(FTP.ASCII_FILE_TYPE);
clientUser.enterLocalActiveMode(); //from example
String originalFileName = fileName;
fileName = eas_user_file_prefix_val+fileName;
InputStream ip = clientUser.retrieveFileStream(fileName);
String issueIdStr = StringHelper.prepad(issueId + "", 10, '0');
String headerRecord = "HEADER " + adjPlatformCd + issueIdStr + batchId + " Original file : " + fileName;
String trailerRecord = "TRAILER " + adjPlatformCd + issueIdStr + batchId + " Server file : " + eas_file_prefix_val + dt_tm_siz + ".csv";
client.setFileType(FTP.ASCII_FILE_TYPE);
//First store file as ".tmp". First header then data followed by trailer
boolean retValue = client.storeFile(eas_file_prefix_val + dt_tm_siz + ".tmp", new ByteArrayInputStream(headerRecord.getBytes()));
if (!retValue) {
ENTADJRuntime.getInstance().logError("FTPService", "Error creating:" + eas_file_prefix_val + dt_tm_siz + ".tmp");
return false;
}
retValue = client.appendFile(eas_file_prefix_val + dt_tm_siz + ".tmp", ip);
if (!retValue) {
ENTADJRuntime.getInstance().logError("FTPService", "Error append 1:" + eas_file_prefix_val + dt_tm_siz + ".tmp");
return false;
}
ip.close();
retValue = client.appendFile(eas_file_prefix_val + dt_tm_siz + ".tmp", new ByteArrayInputStream(trailerRecord.getBytes()));
if (!retValue) {
ENTADJRuntime.getInstance().logError("FTPService", "Error append 2:" + eas_file_prefix_val + dt_tm_siz + ".tmp");
return false;
}
boolean commandOK=clientUser.completePendingCommand(); // this command lets next few ftp commands proces successfully
// place user file in PROCESSED folder. Append issue id, batch #, date and time if file length is < 230
String renamedUserFileName = eas_user_file_prefix_val+ "PROCESSED\\" + originalFileName.substring(0, originalFileName.lastIndexOf(".csv")) + "_" + issueId + "_" + batchId.trim() + dt_tm_siz + ".csv";
String someSiteCommand = "RNFR " + fileName; //rename from
reply = clientUser.sendCommand(someSiteCommand);
someSiteCommand = "RNTO " + renamedUserFileName; //rename to
reply = clientUser.sendCommand(someSiteCommand);
if(!FTPReply.isPositiveCompletion(reply)) {
ENTADJRuntime.getInstance().logError("FTPService", "Error renaming:" + fileName + " reply code:" + reply);
return false;
}
someSiteCommand = "RNFR " + eas_file_prefix_val + dt_tm_siz + ".tmp"; //rename from
reply = client.sendCommand(someSiteCommand);
someSiteCommand = "RNTO " + eas_file_prefix_val + dt_tm_siz + ".csv"; //rename to
reply = client.sendCommand(someSiteCommand);
if(!FTPReply.isPositiveCompletion(reply)) {
ENTADJRuntime.getInstance().logError("FTPService", "Error renaming:" + eas_file_prefix_val + dt_tm_siz + ".tmp" + " reply code:" + reply);
return false;
}
client.logout();
clientUser.logout();

Like the server initiated an FTP session on the client machine, is it possible for SFTP as well? here is the client code:
SFTP is a completely different protocol than FTP and you probably cannot reuse any FTP specific code. It might be that you've meant FTPS instead which is FTP extended with TLS. If this is supported depends on the setup of the server, that is it is not enough to only change the client code. As long as the server supports it you can use it with Java, see for example Secure FTP with org.apache.commons.net.ftp.FTPClient

Related

Akamai Bypass (Java)

I am writing a bot to auto purchase items on a website (zalando).
Everything goes well from login to adding items to shopping cart but at the very end it doesn't work anyomore. It sends this error:
{ "edge_error": "halt", "ref_id": "18.57c51102.1663765843.299fc0e", "wait": 60, "feedback": { "email": true, "url": "", "recaptcha": { "enabled": false, "type": 0, "sitekey": "" } }}
I think it has something to do with their protection or just me missing a header or cookie or a delay... I honestly have no clue anymore
This is the code I use in the end (to checkout and generate a paypal link (post response)):
public void makePostJsonRequest(WebDriver driver, String eTag, String checkoutID)
{
retrieveCookiesMap(driver);
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost postRequest = new HttpPost("https://www.zalando.be/api/checkout/buy-now");
postRequest.setHeader("authority", "www.zalando.be");
postRequest.setHeader("accept", "application/json");
postRequest.setHeader("accept-language", "en-US,en;q=0.9");
postRequest.setHeader("content-type", "application/json");
postRequest.setHeader("cookie", "language-preference=nl;" +
" Zalando-Client-Id=" + cookiesMap.get("Zalando-Client-Id") + ";" +
" ncx=f;" +
" _gcl_au=" + cookiesMap.get("_gcl_au") + ";" +
" sqt_cap=" + cookiesMap.get("sqt_cap") + ";" +
" _ga=" + cookiesMap.get("_ga") + ";" +
" _gid=" + cookiesMap.get("_gid") + ";" +
" bm_sz=" + cookiesMap.get("bm_sz") + ";" +
" ak_bms=" + cookiesMap.get("ak_bms") + ";" +
" _gat_zalga=1;" +
" mpulseinject=false;" +
" frsx=" + cookiesMap.get("frsx") + ";" +
" zsa=" + cookiesMap.get("zsa") + ";" +
" zsr=" + cookiesMap.get("zsr") + ";" +
" zsi=" + cookiesMap.get("zsi") + ";" +
" bm_sv=" + cookiesMap.get("bm_sv") + ";" +
" _abck=" + cookiesMap.get("_abck") + ";");
postRequest.setHeader("origin", "https://www.zalando.be");
postRequest.setHeader("referer", "https://www.zalando.be/checkout/confirm");
postRequest.setHeader("sec-ch-ua", "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"104\"");
postRequest.setHeader("sec-ch-ua-mobile", "?0");
postRequest.setHeader("sec-ch-ua-platform", "\"Linux\"");
postRequest.setHeader("sec-fetch-dest", "empty");
postRequest.setHeader("sec-fetch-mode", "cors");
postRequest.setHeader("sec-fetch-site", "same-origin");
postRequest.setHeader("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36");
postRequest.setHeader("x-xsrf-token", cookiesMap.get("frsx"));
postRequest.setHeader("x-zalando-checkout-app", "web");
postRequest.setHeader("x-zalando-footer-mode", "desktop");
postRequest.setHeader("x-zalando-header-mode", "desktop");
eTag = StringUtils.chop(eTag);
eTag += "\\";
String jsonString = "{\"checkoutId\":\"" + checkoutID + "\"," +
"\"eTag\":" + "\"\\" + eTag + "\"" + "\"" + "}";
System.out.println(jsonString);
StringEntity entity = new StringEntity(jsonString);
postRequest.setEntity(entity);
long startTime = System.currentTimeMillis();
HttpResponse response = httpClient.execute(postRequest);
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("Time taken : "+elapsedTime+"ms");
InputStream is = response.getEntity().getContent();
Reader reader = new InputStreamReader(is);
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuilder builder = new StringBuilder();
while (true) {
try {
String line = bufferedReader.readLine();
if (line != null) {
builder.append(line);
} else {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(builder.toString());
System.out.println("****************");
} catch (Exception ex) {
ex.printStackTrace();
}
}
This means that Akamai (the provider that zalando uses for bot protection), has detected and stopped your request because it detected you as a bot. To avoid this "stop" you MUST send a valid _abck cookie, generated by passing sensor data to the zalando akamai endpoint( you can find using chrome devtools and analyzing the requests )

AWS S3 uploads stuck in WAITING state

I've looked at several questions and answers about this but I'm still unable to upload to an S3 bucket. I have alreadydeclared the service in the manifest within the application tags.
Every log statement returns the correct information, but the status is always WAITING and none of the transfer listeners are triggered. I do not receive any errors whatsoever and the S3 bucket continues to be empty.
What am I doing wrong?
public void uploadStoredItems() {
TransferUtility transferUtility = TransferUtility.builder()
.context(AWSProvider.getInstance().getContext())
.awsConfiguration(AWSProvider.getInstance().getConfiguration())
.s3Client(new AmazonS3Client(AWSProvider.getInstance().getIdentityManager().getCredentialsProvider()))
.build();
try {
for (File csvFile : csvFiles) {
String filename = csvFile.getName(); //.substring(csvFile.getName().lastIndexOf('/') + 1);
Log.d(TAG, "This is the filename for upload: " + filename);
// FileReader reads text files in the default encoding.
String line = null;
FileReader fileReader = new FileReader(csvFile);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
Log.d(TAG, "Here are the lines: " + line);
}
// Always close files.
bufferedReader.close();
TransferObserver uploadObserver = transferUtility.upload(filename, csvFile);
// Gets id of the transfer.
Log.d(TAG, "This is the bucket: " + uploadObserver.getBucket());
Log.d(TAG, "This is the state: " + uploadObserver.getState());
Log.d(TAG, "This is the id: " + uploadObserver.getId());
observers = transferUtility.getTransfersWithType(TransferType.UPLOAD);
TransferListener listener = new TransferListener() {
#Override
public void onStateChanged(int id, TransferState state) {
Log.d(TAG, "onStateChanged: " + state.toString());
if (TransferState.COMPLETED == state) {
Log.d(TAG, "COMPLETE: " + state.toString());
}
}
#Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
float percentDonef = ((float) bytesCurrent / (float) bytesTotal) * 100;
int percentDone = (int) percentDonef;
Log.d(TAG, "ID:" + id + " bytesCurrent: " + bytesCurrent
+ " bytesTotal: " + bytesTotal + " " + percentDone + "%");
}
#Override
public void onError(int id, Exception ex) {
// Handle errors
Log.d(TAG, "ERROR ID:" + id + " " + ex.getMessage());
}
};
for (TransferObserver observer : observers) {
if (TransferState.WAITING.equals(observer.getState())
|| TransferState.WAITING_FOR_NETWORK.equals(observer.getState())
|| TransferState.IN_PROGRESS.equals(observer.getState())) {
observer.setTransferListener(listener);
}
Log.d(TAG, "\n observers - id: " + observer.getId() + " state: " + observer.getState() + " key: " + observer.getKey() + " bytes total: " + observer.getBytesTotal());
}
Log.d(TAG, "Bytes Total: " + uploadObserver.getBytesTotal());
}
csvFiles.clear();
} catch(java.io.FileNotFoundException ex) {
System.out.println("Unable to open file");
} catch(java.io.IOException ex) {
System.out.println("Error reading file");
}
}
Turns out I have to declare the TransferUtility service to run in the same process that the service I am calling it from is running:
<application
...
<service
android:name=".MainService"
android:enabled="true"
android:process=":main_service"
android:stopWithTask="false" />
<service
android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
android:process=":main_service"
android:enabled="true" />
...
</application>

Can't Get Android to Append Data to .txt file

I'm pretty new here, and have been following all I could find on getting my program to append a series of numbers (determined from how often a button was pressed) to a .txt file whenever a timer runs out, but to no avail-I'm not sure what I'm missing...the app starts out by creating a .txt file in the downloads folder with a template in it:
String initialTemplate ="\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\",\"12\"";
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, filename+".txt");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(initialTemplate.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
And then when a countdowntimer object finishes and restarts/ends, it triggers the class writeresults() to append the values:
if (CountsRemaining < 1){
textViewTime.setText("00:00:00");
cancel();
writeResults();
finishcount();
} else {
//append data
writeResults();
//Clear all variables
clearCounts();
start();
}
And the important part, to actually write the data, which is what isn't working:
public void writeResults(){
String Message = Count1 + ", " + Count2 + ", " + Count3 + ", " + Count4 + ", " + Count5 + ", " +
Count6 + ", " + Count7 + ", " + Count8 + ", " + Count9 + ", " + Count10 + ", " + Count11 + Count12;
FileWriter writer;
try {
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
File file = new File (path + filename+".txt");
writer = new FileWriter(file, true);
writer.append(Message);
writer.append("this is a writing test message");
writer.close();
Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
Any idea what I might be missing? Parameters?
The problem is in this line:
File file = new File (path + filename+".txt");
It should be like this, if you look at your first code, when creating file:
File file = new File (path, filename+".txt");

How to add folder to zip

I have my GWT project and I want to upload files to server. I want the following algorithm to work
1) create a folder with the name from "userNumber"
2) write uploaded files to this folder
3) add folder to zip
4) delete folder (if exists)
As I write on the server side I think it is just java problem. Here is my code. I can perform only the first and the second steps, i.e. I can create a folder and write files to this folder.
#Override
public String executeAction(HttpServletRequest request,
List<FileItem> sessionFiles) throws UploadActionException {
String response = "";
userNumber = request.getParameter("userNumber");
File f = new File(ConfAppServer.getRealContextPath() + "/edoc/"
+ userNumber);
if (f.mkdir()) {
System.out.println("Directory Created");
} else {
System.out.println("Directory is not created");
}
for (FileItem item : sessionFiles) {
if (false == item.isFormField()) {
try {
String extension = item.getName().substring(
item.getName().length() - 3);
File file = null;
file = new File(ConfAppServer.getRealContextPath()
+ "/edoc/"
+ userNumber
+ System.getProperty("file.separator")
+ item.getName().substring(0,
item.getName().length() - 4) + "."
+ extension);
item.write(file);
receivedFiles.put(item.getFieldName(), file);
receivedContentTypes.put(item.getFieldName(),
item.getContentType());
response += "<file-" + cont + "-field>"
+ item.getFieldName() + "</file-" + cont
+ "-field>\n";
response += "<file-" + cont + "-name>" + item.getName()
+ "</file-" + cont + "-name>\n";
response += "<file-" + cont + "-size>" + item.getSize()
+ "</file-" + cont + "-size>\n";
response += "<file-" + cont + "-type>"
+ item.getContentType() + "</file-" + cont
+ "type>\n";
} catch (Exception e) {
throw new UploadActionException(e);
}
}
}
ZipUtils appZip = new ZipUtils(userNumber + ".zip",
ConfAppServer.getRealContextPath() + "/edoc/" + userNumber);
appZip.generateFileList(new File(ConfAppServer.getRealContextPath()
+ "/edoc/" + userNumber));
appZip.zipIt(userNumber + ".zip");
f.delete();
removeSessionFileItems(request);
return "<response>\n" + response + "</response>\n";
}
Here you can find my ZipUtils class.
When I try to delete my folder, nothing happens. The delete() method doesn't work. Help me please!!
My question is how to add folder to zip and then delete this folder.
Use java.nio.file. You won't have the hassle of manipulating the ZIP API yourself:
final Path pathToZip = Paths.get("path", "to", "your", "zip");
final URI uri = URI.create("jar:" + pathToZip.toUri());
final Map<String, ?> env = Collections.singletonMap("create", "true");
try (
final FileSystem fs = FileSystems.getFileSystem(uri, env);
) {
// write files into the zip here.
// See javadoc for java.nio.file.Files and FileSystem.getPath()
}
More details on how to use the API here.

Java renameTo() leaving cifs* files on samba mount

I have a linux java program that is reading and writing to a CIFS mounted Windows file share. See code snippet at bottom.
After execution, there are files named "cifs*" left in the directory. If the target directory is on the local file system, the code works like a champ. However, if the target directory is a CIFS mounted Windows file share, I get the left-over files. I'm not sure what configuration changes I need to make to fix. Any help is greatly appreciated.
FileRenameMutex myFileRenameMutex = new FileRenameMutex("/WindowsMount/MoveLock");
class FileRenameMutex
{
// global variables
String lockFileBasename;
String unusedExtension = "_Unused";
String unusedFilename;
String inUseFilename;
String localMachineAddress;
String formattedTime;
// log4j logger for this class
public static Logger logMutex = Logger.getLogger(FileRenameMutex.class.getName());
public FileRenameMutex(String _lockFileBase) {
this.lockFileBasename = _lockFileBase;
logMutex.debug("FileRenameMutex: Constructor, with file " + lockFileBasename + " user = " + System.getProperty("user.name"));
try {
localMachineAddress = InetAddress.getLocalHost().getHostAddress();
//localMachineAddress = InetAddress.getLocalHost().getHostName();
} catch ( Exception e ) {
localMachineAddress = "UNKNOWN_HOST";
logMutex.error(" Could not determine host address. " + e.getMessage());
}
// set the file names
unusedFilename = lockFileBasename + unusedExtension;
inUseFilename = lockFileBasename + "_" + localMachineAddress;
logMutex.debug(" Local machine = " + localMachineAddress);
logMutex.debug(" Unused file name = " + this.unusedFilename);
logMutex.debug(" In Use file name = " + this.inUseFilename);
}
public boolean tryAcquire() throws InterruptedException {
boolean returnVal = false;
File unusedFile = new File(unusedFilename);
File inUseFile = new File(inUseFilename);
formattedTime = new Date().toString();
String inUseText = "This file created by the Alfresco cron job to MoveFileAndCreateDir "
+ "(move files from Unfiled to Filed folders).\n"
+ "Running on " + localMachineAddress + "\n Started at " + formattedTime + "\n";
try {
FileWriter fstream = new FileWriter(inUseFile);
BufferedWriter out = new BufferedWriter(fstream);
// attempt to rename file
logMutex.debug(" Attempting to rename mutex file " + unusedFilename + " to " + inUseFilename);
if ( unusedFile.renameTo(inUseFile) ) {
logMutex.debug(" Rename to inUse successful");
out.write(inUseText);
out.flush();
out.close();
returnVal = true; // lock was acquired
} else {
// System.out.println("Rename to inUse failed");
logMutex.error(" Rename of " + unusedFilename + " to " + inUseFilename + " failed in tryAcquire().");
}
} catch ( Exception e ) {
throw new InterruptedException("Error acquiring lock in tryAcquire(): " + e.getMessage());
}
return returnVal;
}
public void release() {
File unusedFile = new File(unusedFilename);
File inUseFile = new File(inUseFilename);
String unusedText = "This file was last by the Alfresco cron job to MoveFileAndCreateDir "
+ "Ran on " + localMachineAddress + "\n Started at " + formattedTime + "\n";
try {
FileWriter fstream = new FileWriter(inUseFile);
BufferedWriter out = new BufferedWriter(fstream);
out.write(unusedText);
out.flush();
out.close();
// attempt to rename file
logMutex.debug(" Attempting to rename active mutex file " + inUseFilename + " back to " + unusedFilename);
if ( inUseFile.renameTo(unusedFile) ) {
logMutex.debug(" Rename back to unused successful");
} else {
logMutex.error(" Rename of " + inUseFilename + " to " + unusedFilename + " failed in release().");
}
} catch ( Exception e ) {
logMutex.error("Error resetting lock file in release(): " + e.getMessage());
}
} // release()
} // end of class FileRenameMutex

Categories