Fail to upload a image file into Google Doc via java api - java

below is my code
DocsService client = new DocsService("testappv1");
client.setUserCredentials(username, password);
client.setProtocolVersion(DocsService.Versions.V2);
File file = new File("C:/test.jpg");
DocumentEntry newDocument = new DocumentEntry();
newDocument.setTitle(new PlainTextConstruct("test"));
String mimeType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();
newDocument.setMediaSource(new MediaFileSource(file, mimeType));
newDocument = client.insert(destFolderUrl, newDocument);
the document was created successful, but it did not contain anything.

try the following
client.insert(new URL("https://docs.google.com/feeds/documents/private/full/?convert=false"), newDocument);
i think the ?convert=false bit is important, not sure how you do that without the url
client.insert(new URL(destFolderUrl+ "?convert=false"), newDocument);
would hopefully work in your case

Related

Selenium- How to read a PDF file with current user context

I am using selenium for SharePoint UI testing, and I have test scenario where I would need to read and verify the PDF content. but when I use the following code I get. Error ":java.io.IOException: Server returned HTTP response code: 401 for URL http://******.com/pdf"
So I would need help on reading the PDF file with current User context.
String GetURL = driver.getCurrentUrl();
URL TestURL = new URL(GetURL);
BufferedInputStream TestFile = new BufferedInputStream(TestURL.openStream());
PDFParser TestPDF = new PDFParser((RandomAccessRead) TestFile);
TestPDF.parse();
String TestText = new PDFTextStripper().getText(TestPDF.getPDDocument());
Assert.assertTrue(TestText.contains("Open the setting.xml, you can see it is like this"));

Itext generated PDF document to S3 server

I am trying to setup a website where you can "finish order" and then the order is generated in PDF-format. Then I need the pdf-file to be uploaded directly to my S3-bucket
I am fairly new to this so I don't know where to start.
Right now I have made some test code where I make a PDF file with "test" in it.
I have already made an image uploader with my S3-bucket, so I am familiar with how that works.
PdfWriter.getInstance(document, new FileOutputStream("PATH");
document.open();
document.add(new Paragraph("Test");
document.close();
What I want to know is: How do I take this document object and parse it to an S3 server? I have searched everywhere and can't find anything.
Your help is highly appreciated. Thanks!
I have implemented for me. content is byte[] of uploading file
final ObjectMetadata metadata = new ObjectMetadata();
metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
metadata.setContentType(contentType);
metadata.setContentLength(content.length);
final String md5Hex = DigestUtils.md5Hex(new BufferedInputStream(new ByteArrayInputStream(content)));
final PutObjectRequest putObjectRequest = new PutObjectRequest("bucketName", "folderName" + "/" + md5Hex,
new BufferedInputStream(new ByteArrayInputStream(content)), metadata);
final PutObjectResult s3Result = AmazonS3ClientBuilder.defaultClient().putObject(putObjectRequest);

Getting hashed Password from StandardProtectionPolicy before saving File - using Apache PDFBox

I'm working on a programm to bruteforce PDF-Files.
Now I can't read the hashed Owner-password before saving the PDDocument. The problem is that it is much slower to save the File again and again and again then just read out the hashed password from the StandardProtectionPolicy.
Here is my code which I'm working with.
//creating new Document
PDDocument doc1 = new PDDocument();
//creating StandardProtectionPolicy
StandardProtectionPolicy spp = new StandardProtectionPolicy("somepassword", "", new AccessPermission());
spp.setEncryptionKeyLength(128);
//setting the StandardProtectPolicy
doc1.protect(spp);
doc1.save("C:\\Users\\user\\Desktop\\filename.pdf");
//reading out the hash
String hash = new String(doc1.getEncryption().getOwnerKey());
//closing the PDDocument
doc1.close();
So my question is, if I can read out the hash without saving or even creating the PDDocument. If I just don't save the document it don't work.
Thank you for your help.
Luca
I've got the solution.
So for other people who want to know it:
PDDocument doc1 = new PDDocument();
StandardProtectionPolicy spp = new StandardProtectionPolicy(String.valueOf(i), "", new AccessPermission());
spp.setEncryptionKeyLength(128);
doc1.protect(spp);
don't need to save anymore
//doc1.save("C:\\Users\\user\\Desktop\\working.pdf");
StandardSecurityHandler sh = new StandardSecurityHandler(spp);
sh.prepareDocumentForEncryption(doc1);
hash = new String(doc1.getEncryption().getOwnerKey());

URL wont open file:\C filepath in Java

I need to open a filename using a URL(java.net.URL) as below:
file:/C:/RAdev/Basic/src/test/resources/xml Data/test
dir/app-config-seed-data.xml
I've the following java code to read
fileURL = new File(filePath).toURI().toURL();
is = fileURL.openStream();
Since windows can access file:\, even URL should be able to open the same.
Workaround used for now:
public static final String FILE_URL_PREFIX = "file:";
if (filePath.contains(FILE_URL_PREFIX)) {
filePath = filePath.replaceAll("file:/", "");
System.out.println("Modified filepath - " + filePath);
}
fileURL = new File(filePath).toURI().toURL();
is = fileURL.openStream();
Is the above workaround needed, please let me know if there is another way to reap the benefits of URL accessing. I'm new to URL/URI in java, help is really appreciated.
Thanks.
file:/C:/ is not a valid file url. Try starting your URLs with file://C:/.
Additionally, the File(String) constructor does not take a URL, it takes a local file path. If you have a URL as a string that you want to parse, use the URL(String) constructor:
URL fileURL = new URL("file://C:/RAdev/Basic/src/test/resources/xml Data/test dir/app-config-seed-data.xml");
is = fileURL.openStream();
Adding the below implementation on top of Darth Android suggestion worked:
URL url = new URL(filePath);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(),
url.getPort(), url.getPath(), url.getQuery(),
url.getRef());
URL fileURL = uri.toURL();
InputStream is = fileURL.openStream();

Programmatically load data into solr using solrj and java

How can I load data from an xml file into solr using the solrj API?
Thanks Pascal. I miss worded my question, I'm actually using groovy. But in any event your approach does work, but this was my solution:
CommonsHttpSolrServer server = SolrServerSingleton.getInstance().getServer();
def dataDir = System.getProperty("user.dir");
File xmlFile = new File(dataDir+"/book.xml");
def xml = xmlFile.getText();
DirectXmlRequest xmlreq = new DirectXmlRequest( "/update", xml);
server.request(xmlreq);
server.commit();
The first arg to DirectXmlRequest is a url path, it must be "/update" and that the variable xml is a string containing the XML. For example
<add>
<doc>
<field name="title">blah</field>
</doc>
</add>
With Java 6, you can use Xpath to fetch what you need from your xml file. Then, you populate a SolrInputDocument from what you extracted from the xml. When that document contains everything you need, you submit it to Solr using the add method of SolrServer.
SolrClient client = new HttpSolrClient("http://localhost:8983/solr/jiva/");
String dataDir = System.getProperty("user.dir");
File xmlFile = new File(dataDir + "/Alovera-Juice.xml");
if (xmlFile.exists()) {
InputStream is = new FileInputStream(xmlFile);
String str = IOUtils.toString(is);
DirectXmlRequest dxr = new DirectXmlRequest("/update", str);
client.request(dxr);
client.commit();
}

Categories