Cant download file from webservice using MTOM and Axis2 stub - file

I m trying to download a file from my Axis2 webservice server using MTOM and ADB.
I can download the file if I dont enable the MTOM both on server and the client sides. Any suggestions or code sample would be nice :)
Client side
ServerWSStub stub = new ServerWSStub();
stub._getServiceClient().getOptions().setProperty(Constants.Configuration.ENABLE_MTOM,Constants.VALUE_TRUE);
Server side axis2.xml
<parameter name="enableMTOM">optional</parameter>
This is my Server
public DataHandler download(String konum) throws Exception {
System.out.println("The filePath for download: " + konum);
FileDataSource dataSource = new FileDataSource(konum);
DataHandler datahandler = new DataHandler(dataSource);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace("http://benim.projem.org/dosya", "dosyam");
OMText textData = fac.createOMText(datahandler, true);
OMElement ele = fac.createOMElement("sonuc", ns);
ele.addChild(textData);
System.out.println(ele);
return datahandler;
This is my Client
ServerWSStub stub = new ServerWSStub();
//stub._getServiceClient().getOptions().setProperty(Constants.Configuration.ENABLE_MTOM,Constants.VALUE_TRUE);
//when uncommented i get java.lang.NoClassDefFoundError: org/apache/james/mime4j/MimeException
//while trying to invoke _operationClient.execute(true); in ServerWSStub
//I guess it is because of wrong unparsing
Download download = new Download();
download.setKonum(konum);
try {
DownloadResponse downloadResponse = stub.download(download);
DataHandler dh =(DataHandler) downloadResponse.get_return();
File file = new File("C:/dosya/"+fileNameType);
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
dh.writeTo(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (ServerWSExceptionException e) {
e.printStackTrace();
}
Any

I finally got the solution I guess. The Stream closes before the client gets the whole file thats why first I used the getOptions().setTimeOutInMilliSeconds(10000) method but it was also useless and then in the Stub file I commented
_messageContext.getTransportOut().getSender().cleanup(_messageContext);//look for the method's finally part
part so that during a large file transportation the stream had not been closed and i could download the whole file without any silly exceptions :)
--MIMEBoundary_e56e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <0.146e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea#apache.org>
<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:downloadResponse xmlns:ns="http://servis.ws.projem.tez.benim"><ns:return><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:1.046e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea#apache.org" /></ns:return></ns:downloadResponse></soapenv:Body></soapenv:Envelope>
--MIMEBoundary_e56e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-ID: <1.046e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea#apache.org>
binary code here

Simply add apache-mime4j-core-0.7.2.jar to WEB-INF/lib on the service (server) side. The jar can be found here.

Related

Issue with javax.mail and attached file

I want to send an email with javax.mail.
It works on ubuntu 14.04 and java 1.8.0_121 and tomcat7
With ubuntu 18.04 and java 1.8.0_181 and tomcat8 I am getting this error :
Caused by: java.io.IOException: "text/html" DataContentHandler
requires String object, was given object of type class
javax.mail.internet.MimeMultipart
Here is the code :
MimeMessage mex = new MimeMessage(session);
mex.setFrom(new InternetAddress(from));
mex.addRecipient(RecipientType.TO, new InternetAddress((String)((List)ccList).get(0)));
mex.setRecipients(RecipientType.BCC, from);
mex.setSubject(subject);
MimeMultipart var26 = new MimeMultipart();
MimeBodyPart attachBodyPart = new MimeBodyPart();
attachBodyPart.setText(messageBody);
var26.addBodyPart(attachBodyPart);
attachBodyPart = new MimeBodyPart();
byte[] data = baos.toByteArray();
new FileDataSource(fileName);
attachBodyPart.setDisposition("attachment");
attachBodyPart.setContent(data, "application/pdf");
attachBodyPart.setFileName(fileName);
var26.addBodyPart(attachBodyPart);
mex.setContent(var26, "text/html");
Transport transport = session.getTransport("smtp");
transport.connect(param.getSmtpHost(), from, pass);
transport.sendMessage(mex, mex.getAllRecipients());
transport.close();
Do you have any idea?
It's hard to believe this same code works anywhere.
Change
attachBodyPart.setText(messageBody);
to
attachBodyPart.setText(messageBody, "html");
Change
mex.setContent(var26, "text/html");
to
mex.setContent(var26);

Google App Engine: Is it possible to receive a file and email it without saving in Bolbstore

My application’s client end sends a file to servlet, I want to get
this file and send it as an email attachment without saving it.
Please help me how can I do so in Java.
Consider this method: Write the file to GCS (Google Cloud Storage) with a short Lifecycle, so it deletes itself after a short time: GCS Lifecycle
<?xml version="1.0" ?>
<LifecycleConfiguration>
<Rule>
<Action>
<Delete/>
</Action>
<Condition>
<Age>1</Age>
</Condition>
</Rule>
</LifecycleConfiguration>
Get your file, and do:
String fileName;
byte[] fileContent;
MailService service = MailServiceFactory.getMailService();
MailService.Message msg = new MailService.Message();
// msg.setSender(sender);
// msg.setReplyTo(replyTo);
// msg.setTo(recepients);
// msg.setSubject(subject);
// msg.setHtmlBody(message);
ArrayList<Attachment> attachments = new ArrayList<Attachment>();
Attachment attachment = new Attachment(fileName, fileContent);
attachments.add(attachment);
msg.setAttachments(attachments);
service.send(msg);
With Apache Commons FileUpload you can get a file from a web client upload without saving it. Send the file as an attachment with Multi-Part Messages from a byte[] array.

Tika 1.2 PDF parse error - org.apache.pdfbox.cos.COSString cannot be cast to org.apache.pdfbox.cos.COSDictionary

I am using Solr 4.0 and DIH (data import handler) with TikaProcessor for extracting text from PDF files stored in database. When I run indexing it gets failed to parse some PDF files and got the stack trace mentioned below.
Since Solr 4.0 uses Tika 1.2 I have written a unit test to parse the same PDF file using Tika 1.2 API, I got the same error.
The same problem with Tika 1.3 jars also. But when I tried using Tika 1.1 jars it works fine. Please let me if any of you have seen this error and how to fix this?
(I have posted the same in tika mailing list, but not much luck)
When I open the PDF file it is showing PDF/A mode. Not sure if this something related to the problem.
Here is the exception:
org.apache.tika.exception.TikaException: Unexpected RuntimeException from org.apache.tika.parser.pdf.PDFParser#1fbfd6<mailto:org.apache.tika.parser.pdf.PDFParser#1fbfd6>
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:244)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:242)
at org.apache.tika.parser.AutoDetectParser.parse(AutoDetectParser.java:120)
at com.pc.TikaWithIndexing.main(TikaWithIndexing.java:53)
Caused by: java.lang.ClassCastException: org.apache.pdfbox.cos.COSString cannot be cast to org.apache.pdfbox.cos.COSDictionary
at org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink.getAction(PDAnnotationLink.java:93)
at org.apache.tika.parser.pdf.PDF2XHTML.endPage(PDF2XHTML.java:148)
at org.apache.pdfbox.util.PDFTextStripper.processPage(PDFTextStripper.java:444)
at org.apache.pdfbox.util.PDFTextStripper.processPages(PDFTextStripper.java:366)
at org.apache.pdfbox.util.PDFTextStripper.writeText(PDFTextStripper.java:322)
at org.apache.tika.parser.pdf.PDF2XHTML.process(PDF2XHTML.java:66)
at org.apache.tika.parser.pdf.PDFParser.parse(PDFParser.java:153)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:242)
... 3 more
Here is the code snippet in JAVA:
String fileString = "C:/Bernard A J Am Coll Surg 2009.pdf";
File file = new File(fileString );
URL url = file.toURI().toURL();
ParseContext context = new ParseContext();;
Detector detector = new DefaultDetector();;
Parser parser = new AutoDetectParser(detector);;
Metadata metadata = new Metadata();
context.set(Parser.class, parser); //PPt,word,xlsx-- pdf,html
ByteArrayOutputStream outputstream = new ByteArrayOutputStream();
InputStream input = TikaInputStream.get(url, metadata);
ContentHandler handler = new BodyContentHandler(outputstream);
parser.parse(input, handler, metadata, context);
input.close();
outputstream.close();

JavaMail PDF attachment shows up in garbled text or encoded text

I'm trying to send a PDF attachment through JavaMail, but when I receive the email, I get something like this, without any attachments in the mail:
===EMAIL CONTENT===
------=_Part_0_3786439.1313701770148
Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=ems_report.pdf
JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9MZW5ndGggMTE4Ni9GaWx0ZXIvRmxhdGVEZWNvZGU+
PnN0cmVhbQp4nLWaTW/jNhCG7/oVBIoF0kMYfgw/1FviZNsEa+/WVrCHogc3VgIXsb1VnS7670vK
lhynMjmxRQSOBA1JPXpfaTSSyAgn55wworlw/x8W2V/ZVZFJTZTRpJhlN0X2aybInd/q2zHStC4W
2cVHTvzaY3b2Y/Gnb7trUg/G6pXqKROMqFy4QRU5l25LmT2+HtFtUoZRadpRrR/VLd2f6352M5yQ
8ctyWq1eljMyWC2+Pc+ny4eSjMtvq2pNHlcVGd0WNx6DkacOlN9+d8tZfSjtsNttb1q+4sqBKG6p
5S0XeK72sM52TLfL+Xo+Xa+qv8nkZbGYVv8iWfYFozlwd/CvlpbJ3O/LszBLOGOdEnJmqLUEbE7l
zhxW41Ln7fa3D/1xXi0OYwry3UXutv21C4x/bti3NIuMb9ees4nr33Tw5JvfXhcwTRe/5rscKwUY
s5FCdEghBdWcgJYdUgBpfl6Kd5wzWDgeoROOzhuFoDPG5D3TQYROSUsBUHQSemZTETYNnAqBYhM9
o4kImnyHbLpnNhlhA+HYJIrNLTX5kOCCCGUuwTSFnECuKJfhzPW5mpUuwY7Lh3L+TzlrQPcuLTeS
lVT5gd52L1br6fPhowunL96mPIFOebzNeeLUnAcRFRVzSQ8l4i79b+R8q6KSLtnrwyp+mk//eC6P
lRFaGRVaRmhlVKfKqCIyaiejwp2MMR214PV5fUjH0Wp5/nySlqrVUqO1VK2W+lQtRURL2dspKVNe
===CODE===
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart part1 = new MimeBodyPart();
FileDataSource source = new FileDataSource("ems_report.pdf");
part1.setDataHandler(new DataHandler(source));
part1.setFileName(source.getName());
part1.setHeader("Content-Type", "application/pdf");
part1.setHeader("Content-Transfer-Encoding", "base64");
part1.setDisposition(Part.ATTACHMENT);
multipart.addBodyPart(part1);
MimeBodyPart part2 = new MimeBodyPart();
part2.setText("mimebodypart part2");
multipart.addBodyPart(part2);
message.setContent(multipart);
message.setHeader("Content-Type", "multipart/mixed");
What did I do wrong here? I've googled for the whole day and no one seems to have the solution. Any help is appreciated, thanks so much!
What you're seeing is a binary data of the multipart. You first have to decode the base64 string. After that you can process the PDF.
PS: If you open that mail in a client (Webmail, Outlook, Thunderbird) you should see the PDF correctly.

How can i get https responses on google app engine?

I get this exception when trying to fetch response for an https link
javax.net.ssl.SSLHandshakeException: Could not verify SSL certificate for: https://[...link...]
Below is the code I use. What am I doing wrong?
URL productsURL = new URL(link + authenticationExtension);
URLConnection connection = productsURL.openConnection();
connection.setRequestProperty("Authorization", "Basic " + userNamePasswordB64);
connection.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
See the FetchOptions doNotValidateCertificate method. There is a note about this in the URL Fetch docs.

Resources