I am trying to download something an attachment behind a javascript button with HtmlUnit. Performing other tasks works great (eg. navigating, login).
I checked out the attachment unit test but it didnt help me.
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
final HtmlPage page1 = webClient.getPage( loginUrl );
final HtmlTextInput textField = page1.getElementByName(user);
final HtmlPasswordInput pwd = page1.getElementByName(pwd);
textField.setValueAttribute(User.getUsername());
pwd.setValueAttribute(User.getPassword());
final HtmlSubmitInput button = page1.getElementByName(login);
final HtmlPage page2 = button.click();
String buttonJavaScript = "window.location='"+folder+filename + ....... ";
ScriptResult result = page2.executeJavaScript(buttonJavaScript);
webClient.waitForBackgroundJavaScript(2000);
InputStream is = result.getNewPage().getWebResponse().getContentAsStream();
try {
File f = new File("filename.extension");
OutputStream os = new FileOutputStream(f);
byte[] bytes = new byte[1024];
while (read == is.read(bytes)) {
os.write(bytes, 0, read);
}
os.close();
is.close();
} catch (IOException ex) {
// Exception handling
}
However, it stops with:
runtimeError: message=[No node attached to this object] sourceName=[http://pagead2.googlesyndication.com/pagead/osd.js] line=[7] lineSource=[null] lineOffset=[0]
The file created is size 0.
There must be a way to get to the real file attached?!
Thank you in advance
Just in case anyone else is wondering: You need to use the AttachmentHandler.
ScriptResult result = page2.executeJavaScript(buttonJavaScript);
webClient.waitForBackgroundJavaScript(1000);
if( attachments.size() > 0 )
{
Attachment attachment = attachments.get(0);
Page attachedPage = attachment.getPage();
WebResponse attachmentResponse = attachedPage.getWebResponse();
String content = attachmentResponse.getContentAsString();
... write(content);
}
Related
my task is to encrypt a file that is uploaded to S3. The upload worked fine before the encryption but now after I encrypted the file I get this exception.
The XML you provided was not well-formed or did not validate against our published schema
I added this to the existing Code
final AwsCrypto crypto = new AwsCrypto();
try (
final FileInputStream in = new FileInputStream(encryptfile);
final FileOutputStream out = new FileOutputStream(file);
final CryptoOutputStream<?> encryptingStream = crypto.createEncryptingStream(crypt, out))
{
IOUtils.copy(in, encryptingStream);
}
My thoughts, Why does AmazonS3 expect a XML-File ? Why not a normal text document ?
Is there a Option to change this maybe with the Bucket Policy ?
EDIT
That is the upload code, maybe there is a Issue. I dont understand why it´s working without the encryption.
File uploaffile = encryptFile(file);
List<PartETag> partETags = new ArrayList<PartETag>();
String filename = String.valueOf(System.currentTimeMillis());
InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(awss3bucket, filename);
InitiateMultipartUploadResult initResponse = amazons3.initiateMultipartUpload(initRequest);
long partSize = 5 * 1024 * 1024;
long contentLength = uploaffile.length();
long filePosition = 0;
for (int i = 1; filePosition < contentLength; i++) {
partSize = Math.min(partSize, (contentLength - filePosition));
UploadPartRequest uploadRequest = new UploadPartRequest()
.withBucketName(awss3bucket)
.withKey(filename)
.withUploadId(initResponse.getUploadId())
.withPartNumber(i)
.withFileOffset(filePosition)
.withFile(uploaffile)
.withPartSize(partSize);
PartETag petag = new PartETag(amazons3.uploadPart(uploadRequest).getPartNumber(), amazons3.uploadPart(uploadRequest).getETag());
partETags.add(petag);
filePosition += partSize;
}
CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(awss3bucket, filename,
initResponse.getUploadId(), partETags);
amazons3.completeMultipartUpload(compRequest);
Maybe next time I should stop to copy some random Code from the Internet.
You use The FileoutputStream to write not the other way. So the File so was Empty which created the Exception.
CryptoInputStream<KmsMasterKey> encryptingStream = crypto.createEncryptingStream(crypt, in);
FileOutputStream out = null;
try {
out = new FileOutputStream(encryptfile);
IOUtils.copy(encryptingStream, out);
encryptingStream.close();
out.close();
} catch (IOException e)
I am posting json data to the server but on getting the response it should be like this
{"id":65,"check":1,"date":"08-Jan-19"}
instead, I am getting this
{"id":"65check=1","check":null,"date":"08-Jan-19"}
This is the code on button click I send json form data to the server but in response, the id value gets attached to check value, how to get the proper response.
Attendance_TimeCheck = "1";
users_identify = "65";
try {
URL urlForPostRequest = new URL("http://xenzet.com/ds/getrec.php");
System.out.println("Instantiated new URL: " + urlForPostRequest);
final long id = Long.valueOf(users_identify);
HttpURLConnection conection = (HttpURLConnection) urlForPostRequest.openConnection();
conection.setDoOutput(true);
conection.setRequestMethod("POST");
conection.setRequestProperty("User-Agent", "Mozilla/5.0");
conection.getOutputStream().write(("id="+id).getBytes(StandardCharsets.UTF_8));
conection.getOutputStream().write(("check="+Attendance_TimeCheck).getBytes(StandardCharsets.UTF_8));
conection.connect();
BufferedInputStream bis = new BufferedInputStream(conection.getInputStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int resultBuffer = bis.read();
while (resultBuffer != -1) {
bos.write((byte) resultBuffer);
resultBuffer = bis.read();
}
String result1 = bos.toString();
System.out.println(result1);
} catch (Exception ex) {
ex.printStackTrace();
}
I think the string is being read wrongly.
Check the payload you are passing to the POST request,
conection.getOutputStream().write(("id="+id).getBytes(StandardCharsets.UTF_8));
conection.getOutputStream().write(("check="+Attendance_TimeCheck).getBytes(StandardCharsets.UTF_8));
"id="+id followed by "check="+Attendance_TimeCheck will result to "id":"65check=1"
append the ampersand before the queryparam check to get the desired result,
conection.getOutputStream().write(("&check="+Attendance_TimeCheck).getBytes(StandardCharsets.UTF_8));
when I try to load the page in the server's contentpath "\manager" it should send the response of page manager.html complete with css and the various libraries used. The server send the page, the only problem does not execute the javascript present inside (with the corresponding images and css).
#Override
public void handle(HttpExchange he) throws IOException {
String root = "html/manager";
URI uri = he.getRequestURI();
File file = new File(root + uri.getPath()+ ".html").getCanonicalFile();
OutputStream os = null;
String response = "";
System.out.println(root + uri.getPath());
if (!file.isFile()) {
// Object does not exist or is not a file: reject with 404 error.
response = "404 (Not Found)\n";
he.sendResponseHeaders(404, response.length());
os = he.getResponseBody();
os.write(response.getBytes());
} else {
// Object exists and is a file: accept with response code 200.
he.sendResponseHeaders(200, 0);
os = he.getResponseBody();
FileInputStream fs = new FileInputStream(file);
final byte[] buffer = new byte[0x10000];
int count = 0;
while ((count = fs.read(buffer)) >= 0) os.write(buffer,0,count);
fs.close();
}
os.close();
}
Console output:
html/manager/manager
HTML server output
Opening local .html file with browser
Path:
/.../Applicazione/Illumify/html/manager/manager.html
doc folder used inside the manager.html
I have a pdf Document and that need to be digitally signed and the signature is provided by external service. I don't have the certificate chain prior to initiate the signature. I tried the following code but getting SigDict/Contents illegal data message.
source to create document hash and post that to external service to sign it
InputStream data = null;
DocumentSignStatus documentSignStatus = new DocumentSignStatus();
int contentEstimated = 8192;
PdfReader reader = new PdfReader(requestParams.getDocumentToBeSigned());
try {
reader.unethicalreading = true;
int pdfPagenumber = 1;
if((Integer)requestParams.getSignPageNo() == null || requestParams.getSignPageNo()==0 ){
//pdfPagenumber = 1; // Default signature on first page.
pdfPagenumber = reader.getNumberOfPages(); // Sign on last page
}else {
pdfPagenumber = requestParams.getSignPageNo();
}
PdfSignatureAppearance appearance = null;
ByteArrayOutputStream os = null;
os = new ByteArrayOutputStream ();
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', null, true);
Calendar cal = Calendar.getInstance();
appearance = stamper.getSignatureAppearance();
appearance.setSignDate(cal);
appearance.setAcro6Layers(false);
appearance.setReason("Test Signature");
appearance.setLocation("India");
appearance.setImage(null);
appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
Rectangle rect = requestParams.getRect();
appearance.setVisibleSignature(rect, pdfPagenumber, null);
HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>();
exc.put(PdfName.CONTENTS, new Integer(contentEstimated * 2 + 2));
PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
dic.setReason(appearance.getReason());
dic.setLocation(appearance.getLocation());
dic.setContact(appearance.getContact());
dic.setDate(new PdfDate(appearance.getSignDate()));
appearance.setCryptoDictionary(dic);
appearance.preClose(exc);
data = appearance.getRangeStream();
MessageDigest messageDigest;
String provider = null;
String hashAlgorithm = DigestAlgorithms.SHA256;
if (provider == null){
messageDigest = MessageDigest.getInstance(hashAlgorithm);
}else {
messageDigest = MessageDigest.getInstance(hashAlgorithm,provider);
}
int read = 0;
byte[] buff = new byte[contentEstimated];
while ((read = data.read(buff, 0, contentEstimated)) > 0)
{
messageDigest.update(buff,0,read);
}
byte[] hashDigest = messageDigest.digest();
byte[] documentHash = org.bouncycastle.util.encoders.Hex.encode(hashDigest);
//eSign Start
String hashdocument = new String(documentHash, "UTF-8");
System.out.println("Document Hash :"+hashdocument);
//Generate Sign Request XML for external source to sign the document hash
String eSignXmlStr = esignXML.generateEsignXML20(hashdocument,requestParams.getAadhaarNo());
String eSignSignedXML = myXMLSigner.signXML(eSignXmlStr, true);
System.out.print("sign request xml: " + eSignSignedXML);
// sign request xml generation complete
documentSignStatus.setSignedXML(eSignSignedXML);
session.put("hashdocument", documentHash);
session.put("appearance", appearance);
session.put("baos", os);
}catch(Exception e) {
e.printStackTrace();
}
return documentSignStatus;
Following is the source to attach the PKCS7 response to pdf to complete the signature procedure and get the signed pdf.
byte[] hashdocument = (byte[])session.get("hashdocument");
PdfSignatureAppearance appearance = (PdfSignatureAppearance)session.get("appearance");
ByteArrayOutputStream os = (ByteArrayOutputStream)session.get("baos");
//Get signed response xml
InputStream x = request.getInputStream();
String responseXML = IOUtils.toString(x, "UTF-8");
System.out.print("REsponse:" + responseXML);
//parse the xml and get pkcs7 data
String pkcs7asString = getPKCS7DataFromDigitalSignatureResponse(responseXML);
byte[] signedDocByte = org.bouncycastle.util.encoders.Base64.decode(pkcs7asString);
//////////////////// ADD SIGNED BYTES/HASH TO PDF DOCUMENT.
int contentEstimated = 8129;
byte[] paddedSig = new byte[contentEstimated];
System.arraycopy(signedDocByte, 0, paddedSig, 0, signedDocByte.length);
PdfDictionary dic2 = new PdfDictionary();
dic2.put(PdfName.CONTENTS, new PdfString(paddedSig).setHexWriting(true));
try {
appearance.close(dic2);
} catch (DocumentException e) {
e.printStackTrace();
}
try(OutputStream outputStream = new FileOutputStream("/SignTest2.0.pdf")) {
os.writeTo(outputStream);
}
os.close();
The method getPKCS7DataFromDigitalSignatureResponse(responseXML) is used to parse the external service response it returns a PKCS7 as String.
I tried to figure out the problem but couldn't find the root cause.
can anyone help to sort out this issue. Using itext version 5.4.5 copy od signed document is PDF with Signature Problem
I found the root cause of the issue, I made a small mistake, the content estimation variable in not correct in above source due to that paddedSig was not created properly.
Wrong value as shown in above source. int contentEstimated = 8129;
Correct Value is int contentEstimated = 8192;
Rest of the code is fine.
I was trying to download this image, directly from the full path. When I try it throw Firefox, I'm redirected to the initial page.
But into some Manga, i could see the url from image, and it's that one...
I'm using the code below... Someone knows why it's happening?
URL url = new URL("http://mangas.centraldemangas.com.br/death_note/death_note001-01.jpg");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
String nomeArquivo = "teste.jpg";
FileOutputStream fos = new FileOutputStream("C://ImagensAnime//"+nomeArquivo);
fos.write(response);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
Is there any way to download it using this URL directly?
Most likely they are checking the HTTP REFERRER (with mod_rewrite maybe) and if it doesn't match their domain it will redirect you to the homepage.
Try setting HTTP-REFERRER to http://centraldemangas.com.br/ and see if that works.