I have used Freemarker for creating a template which I will be using to send as email.
Here is the snippet of the parameters that i wish to include in the template.
Iam using java..
//use freemarker
Configuration config = new Configuration();
config.setTemplateLoader(new ClassTemplateLoader(getClass(), "/"));
Template template = config.getTemplate("helloworld.ftl");
// Build the data-model
Map<String, Object> data = new HashMap<String, Object>();
data.put("message", "Hello!! You have got a new approval mail!");
//List parsing
List<String> mailDetails = new ArrayList<String>();
mailDetails.add(fromAddress);
mailDetails.add(fromName);
mailDetails.add(toAddress);
mailDetails.add(toName);
mailDetails.add(subject);
mailDetails.add(body);
data.put("mailDetails", mailDetails);
// Console output
Writer out = new OutputStreamWriter(System.out);
template.process(data, out);
out.flush();
This is tested and it successfully created a template in the specified folder.
All I want to know is how do i pass the template that is generated as a parameter while sending mail?
I am sending email as follows in Liferay
How should I pass the template as a parameter while sending mail?
You're writing to System.out
// Console output
Writer out = new OutputStreamWriter(System.out);
template.process(data, out);
out.flush();
You can write to a String:
StringWriter out = new StringWriter();
template.process(data, out);
String finishedMessage = out.toString();
or pass any other writer to the process() method.
Related
I am trying to read the contents of a PDF file using Java-Selenium. Below is my code. getWebDriver is a custom method in the framework. It returns the webdriver.
URL urlOfPdf = new URL(this.getWebDriver().getCurrentUrl());
BufferedInputStream fileToParse = new BufferedInputStream(urlOfPdf.openStream());
PDFParser parser = new PDFParser((RandomAccessRead) fileToParse);
parser.parse();
String output = new PDFTextStripper().getText(parser.getPDDocument());
The second line of the code gives compile time error if I don't parse it to RandomAccessRead type.
And when I parse it, I get this run time error:
java.lang.ClassCastException: java.io.BufferedInputStream cannot be cast to org.apache.pdfbox.io.RandomAccessRead
I need help with getting rid of these errors.
First of, unless you want to interfere in the PDF loading process, there is no need to explicitly use the PdfParser class. You can instead use a static PDDocument.load method:
URL urlOfPdf = new URL(this.getWebDriver().getCurrentUrl());
BufferedInputStream fileToParse = new BufferedInputStream(urlOfPdf.openStream());
PDDocument document = PDDocument.load(fileToParse);
String output = new PDFTextStripper().getText(document);
Otherwise, if you do want to interfere in the loading process, you have to create a RandomAccessRead instance for your BufferedInputStream, you cannot simply cast it because the classes are not related.
You can do that like this
URL urlOfPdf = new URL(this.getWebDriver().getCurrentUrl());
BufferedInputStream fileToParse = new BufferedInputStream(urlOfPdf.openStream());
MemoryUsageSetting memUsageSetting = MemoryUsageSetting.setupMainMemoryOnly();
ScratchFile scratchFile = new ScratchFile(memUsageSetting);
PDFParser parser;
try
{
RandomAccessRead source = scratchFile.createBuffer(fileToParse);
parser = new PDFParser(source);
parser.parse();
}
catch (IOException ioe)
{
IOUtils.closeQuietly(scratchFile);
throw ioe;
}
String output = new PDFTextStripper().getText(parser.getPDDocument());
(This essentially is copied and pasted from the source of PDDocument.load.)
How to set the app properties of a file using Google Drive v3 in java?
The reference says: "files.update with {'appProperties':{'key':'value'}}", but I don't understand how to apply that to my java code.
I've tried things like
file = service.files().create(body).setFields("id").execute();
Map<String, String> properties = new HashMap<>();
properties.put(DEVICE_ID_KEY, deviceId);
file.setAppProperties(properties);
service.files().update(file.getId(), file).setFields("appProperties").execute();
but then I get an error that "The resource body includes fields which are not directly writable."
And to get the data:
File fileProperty = service.files().get(sFileId).setFields("appProperties").execute();
So how to set and get the properties for the file?
Thanks! :)
Edit
I tried
file = service.files().create(body).setFields("id").execute();
Map<String, String> properties = new HashMap<>();
properties.put(DEVICE_ID_KEY, deviceId);
file.setAppProperties(properties);
service.files().update(file.getId(), file).execute();
but I still get the same error message.
To avoid this error on v3
"The resource body includes fields which are not directly writable."
when calling update, you need to create an empty File with the new changes and pass that to the update function.
I wrote this and other notes as a v3 Migration Guide here.
The Drive API client for Java v3 indicates that the File.setAppProperties will require a Hashmap<String,String> parameter. Try to remove the setFields("appProperties") call since you are trying to overwrite appProperties itself (you're still calling Update at this time).
When retrieving appProperties, you'll just need to call getAppProperties.
Hope this helps!
File fileMetadata = new File();
java.io.File filePath = new java.io.File(YOUR_LOCAL_FILE_PATH);
Map<String, String> map = new HashMap<String, String>();
map.put(YOUR_KEY, YOUR_VALUE); //can be filled with custom String
fileMetadata.setAppProperties(map);
FileContent mediaContent = new FileContent(YOUR_IMPORT_FORMAT, filePath);
File file = service.files().create(fileMetadata, mediaContent)
.setFields("id, appProperties").
.execute();
YOUR_IMPORT_FORMAT, fill this with the value in this link, https://developers.google.com/drive/api/v3/manage-uploads, there is explanation below the example code
setFields("id, appProperties"), fill this with the value in this link: https://developers.google.com/drive/api/v3/migration, this the most important part I think, if you don't set the value in the setFields method your additional input will not be written
With version v3, to add or update appProperties for an existing file and without this error:
"The resource body includes fields which are not directly writable."
You should do:
String fileId = "Your file id key here";
Map<String, String> appPropertiesMap = new HashMap<String, String>();
appPropertiesMap.put("MyKey", "MyValue");
appPropertiesMap.put("MySecondKey", "any value");
//set only the metadata you want to change
//do not set "id" !!! You will have "The resource body includes fields which are not directly writable." error
File fileMetadata = new File();
fileMetadata.setAppProperties(appPropertiesMap);
File updatedFileMetadata = driveService.files().update(fileId, fileMetadata).setFields("id, appProperties").execute();
System.out.printf("Hey, I see my appProperties :-) %s \n", updatedFileMetadata.toPrettyString());
I am using jackson for converting POJO to JSON
User user = new User();
user.setAge(25);
user.setName("Shahid");
ObjectMapper mapper= new ObjectMapper();
mapper.writeValue("D:/test.json", user);
instead of writing it to file , I want to write it to on String variable (jsonString) . So that I get the result as follow.
String jsonString= "{"name" : "Shahid","age" : 25}";
You can try,
mapper.writeValueAsString(user).
Please refer documentation for more details.
You can try this:
OutputStream os = new ByteArrayOutputStream();
mapper.writeValue(os, user);
String json = os.toString();
Am working with GWT application and integrated with Simple framework to parse objects into XML, I have POJO classes on client side and use the parser on server side. I need to write the serialized object to String variable instead of file cause files not allowed in GWT App engine https://groups.google.com/forum/?fromgroups=#!topic/google-web-toolkit/M7Zo3U7CKD8.
Current code I have in the server side on GWT RPC ServiceImpl
File result = new File("c:/myXMLFile.xml");
Serializer serializer = new Persister();
MyBeanToSerialize beanToSerialize = new MyBeanToSerialize("firstName","LastName");
serializer.write(beanToSerialize, result);
I found the solution for returning String from the XML parser by using the writer object instead of File the code is as the following:-
String parser(){
StringWriter writer = new StringWriter();
Serializer serializer = new Persister();
MyBeanToSerialize beanToSerialize = new MyBeanToSerialize("firstName","LastName");
serializer.write(beanToSerialize, writer);
return writer.getBuffer().toString();
)
I'm trying to replace a document on alfresco with Apache chemistry.
I create an inputstream from a file stored on disk, i create a contenstream with the constructor ContentStreamImpl and i try to replace the document with the .setContentStream method.
The result of this operation is
org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException: Internal Server Error
This is the part of my code:
InputStream newDoc = new FileInputStream(global.getPathTemp() + filename);
ContentStream content = new ContentStreamImpl("Prova", BigInteger.valueOf(newDoc.available()), mimetype, newDoc);
alfDoc.setContentStream(content, true);
Can someone help me??
I solved it with
alfDoc.setContentStream(content, true, true);