Apache Abdera Multipart Request throwing nullpointer Exception(IBM connection API) - multipart

I am using Apache abdera to post multipart request to IBM connection 4.0 API. I am getting nullpointer exception from Abdera API. Please let me know what's the root cause.
private void createEntryWithAttachment(){
try {
String activityId = "urn:lsid:ibm.com:oa:662d0dc7-0308-48ee-8291-d730c733d2d1";
String activityIdLocal = activityId.substring(activityId.lastIndexOf(":")+1, activityId.length());
String createEntryLocal = createEntry+activityIdLocal;
Abdera abdera = new Abdera();
AbderaClient client = new AbderaClient(abdera);
AbderaClient.registerTrustManager();
System.out.println("pd --->"+pd);
client.addCookie("poktam2cl.iespc.ibm.com", "PD-S-SESSION-ID", pd, "/", null, true);
RequestOptions requestOptions = client.getDefaultRequestOptions();
requestOptions.setUseChunked(true);
requestOptions.setHeader("Connection", "close");
requestOptions.setHeader("Content-Type", "multipart/related;type=\"application/atom+xml\"");
requestOptions.setContentType("multipart/related;type=\"application/atom+xml\"");
requestOptions.setSlug("Sample.txt");
Credentials credentials = new UsernamePasswordCredentials(username, password);
client.addCredentials(createEntryLocal, AuthScope.ANY_REALM,AuthScope.ANY_SCHEME, credentials);
Entry entry = abdera.getFactory().newEntry();
entry.setTitle("create entry with attachment title ");
entry.setContent("create entry with attachment content");
javax.xml.namespace.QName field = new QName("http://www.ibm.com/xmlns/prod/sn", "field", "snx");
org.apache.abdera.model.Element fieldElement = entry.addExtension(field);
fieldElement.setAttributeValue("type", "file");
fieldElement.setAttributeValue("name", "sampletextfile1");
fieldElement.setAttributeValue("position", "3000");
FileInputStream fis = new FileInputStream(filepath);
requestOptions.setHeader("Content-Length", "35");
entry.addCategory("http://www.ibm.com/xmlns/prod/sn/type","entry", "Entry");
ClientResponse response = client.post(createEntryLocal, entry, fis, "multipart/related;type=\"application/atom+xml\"", requestOptions );
System.out.println("Entry Created with attachment's resp: " + response.getStatus());
if(response.getStatus() == 201){
System.out.println("Entry Created with attachment successfully .....");
printIBMConnectionErrorMessage(response);
}else{
System.out.println("Entry with attachment creation failed");
printIBMConnectionErrorMessage(response);
//System.exit(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Output
java.lang.NullPointerException
at org.apache.abdera.protocol.client.util.MultipartRelatedRequestEntity.writeInput(MultipartRelatedRequestEntity.java:74)
at org.apache.abdera.protocol.client.util.MultipartRelatedRequestEntity.writeRequest(MultipartRelatedRequestEntity.java:59)
at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:499)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2114)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at org.apache.abdera.protocol.client.AbderaClient.execute(AbderaClient.java:688)
at org.apache.abdera.protocol.client.AbderaClient.post(AbderaClient.java:306)
at JavaAgentEntryWithAttachment.createEntryWithAttachment(JavaAgentEntryWithAttachment.java:157)
at JavaAgentEntryWithAttachment.main(JavaAgentEntryWithAttachment.java:66)
This exception is coming from abdera API, class called MultipartRelatedRequestEntity.java, Line no 74. I have placed line no 74 source code below. So its clear that contentSrc is null & Abdera API not allowing me to set this value. Please let me know what I am missing here.
String contentId = entry.getContentSrc().toString();

I did in two steps:
Send the file
Call to update the data
Each with the good mime type. You can not send the file with XML mime type. And put the length of the file.

It is possible to avoid the nullpointer and do it in one request. I had the same issue and created another issue and managed to find a solution. You can find it here.
It comes down to the following code example where you create a HttpClient Part which can contain a StringPart and a FilePart
final Entry entry = // ... Create your Entry
final RequestOptions options = this.client.getDefaultRequestOptions();
options.setHeader("Content-Type", "multipart/related;type=\"application/atom+xml\"");
StringPart entryPart = new StringPart("entry", entry.toString());
entryPart.setContentType("application/atom+xml");
FilePart filePart = new FilePart("file", new File(resource.getFile()));
RequestEntity request = new MultipartRequestEntity(new Part[] { entryPart, filePart}, this.client.getHttpClientParams());
ClientResponse response = client.post(this.url + this.activityId, request, options);
Hope this will help people in the future if they are using Abdera.

Related

Flutter : InternalError , Filter error, bad data when upload image on server

I do simple upload image to server using http packages. But the problem is , when i upload it i got error Unhandled Exception: InternalError: 'Filter error, bad data'.
Upload
Future testUpload(File selfieImage, String pembertang) async {
final result = await reusableRequestServer.requestServer(() async {
//create multipart request for POST or PATCH method
var request = http.MultipartRequest('POST', Uri.parse('$baseApiUtang/addUtang'));
//add text fields
request.fields['pembertang'] = '$pembertang';
//create multipart using filepath, string or bytes
var pic = await http.MultipartFile.fromPath('selfie', selfieImage.path);
//add multipart to request
request.files.add(pic);
var response = await request.send();
//Get the response from the server
var responseData = await response.stream.toBytes();
var responseString = String.fromCharCodes(responseData);
print('responseData : $responseData');
print('responseString : $responseString');
return responseString;
});
return result;
}
With this strange problem , i try it with postman but it's work like i want.
My opinion, it's not problem in my backend code. I missed something ?
Update
Similiar problem with me on this issue
Update 2
When i look inside the code and print variable 1 by 1, i got status code 403
Hi can you pls try following code
var stream = new http.ByteStream(Stream.castFrom(imageFile.openRead()));
// get file length
var length = await imageFile.length();
// create multipart request
var request = new http.MultipartRequest("POST", uri);
var name = firstname +"."+imageFile.path.split(".").last;
// multipart that takes file
var multipartFile = new http.MultipartFile('profile', stream, length,
filename: basename(name));
In my case my can not take so much long name so i customise image name and then tried and its working .
so you can also try this.

Sending multiple files to webdav server in one request

I want to send to webdav server a 100 files in one request. I'm getting list of messages, then I creating binary body parts of them. This is my code which gets me 301 http error response code. When I send one file it's working but expected behaviour is to send parts of files. And I want it to be created one file by one on the server, is it possible?
CloseableHttpClient client = HttpClients.createDefault();
HttpPut httpPost = new HttpPut("http://localhost:8888/webdav"); // I also tried with / at the end
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
messages.forEach(m -> {
builder.addBinaryBody("file", new File(Paths.get(m.getPath()).toAbsolutePath().toString()),
ContentType.APPLICATION_OCTET_STREAM, m.getFileName() + ".encryptedByAes");
builder.addBinaryBody("file", new File(Paths.get(m.getAesPath()).toAbsolutePath().toString()),
ContentType.APPLICATION_OCTET_STREAM, m.getFileName() + ".aes");
});
HttpEntity multipart = builder.build();
httpPost.setEntity(multipart);
UsernamePasswordCredentials creds
= new UsernamePasswordCredentials("test", "test");
httpPost.addHeader(new BasicScheme().authenticate(creds, httpPost, null));
CloseableHttpResponse response = client.execute(httpPost);
if (response.getStatusLine().getStatusCode() != HTTP_OK) {
throw new WebDavException("Error while executing request to webdav server with messages");
}
client.close();

Spring - Download excel with POI

I have this controller which create an empty sheet and I want to return the excel file to the navigator. The problem is, the excel file is corrupted.
If I create the file on my computer the file isn't corrupted, so my HSSFWorkbook is valid. Seems a problem of encodage/encapsulation added by the spring context ?
#Controller
public class ExportController {
#RequestMapping(value = "/export/test/excel", method = RequestMethod.POST)
public void downloadExcelTestFile(
HttpServletRequest request,
HttpServletResponse response) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
wb.createSheet("Sheet1");
//response.reset();
//response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=test.xls");
OutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();
wb.close();
}
The download start well, I receive the file test.xls, but I can't open it. Is there a Spring way to achiev a proper download inside a #Controller ?
I use Spring 4.2.4
UPDATE 1
I tried a Spring way but it's not working better
HSSFWorkbook wb = new HSSFWorkbook();
wb.createSheet("Sheet1");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
wb.write(bos);
} finally {
bos.close();
}
byte[] bytes = bos.toByteArray();
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/vnd.ms-excel;");
headers.set("content-length",Integer.toString(bytes.length));
headers.set("Content-Disposition", "attachment; filename=test.xls");
return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.CREATED);
UPDATE 3
I found a reason but I don't understand why.
If I build my war file and deploy it manually in the very same tomcat 7.0.70 it works. My Excel is not corrupted.
If I download from the dev environnement in eclipse, it doesn't work. Seems a tomcat + eclipse issue.
Ok that wasn't a Spring issue, not even a tomcat issue.
The problem was from my grunt-connect-proxy, when I run my front throught localhost:9000 : files that I downloaded were corrupted. If I build the project in a war file or run the front from localhost:8080 ( same port than the server ) without "grunt serve" and so without the proxy it works.
I have not fix the problem with grunt ... I just ignore it, but this answer can save your time.
Sample Spring Backed Code to create an excel and return it using Spring REST. The input parameters may change as per your requirement
#RequestMapping(value = "/convertFlatFileToExcel.do", method = RequestMethod.POST)
public HttpEntity<byte[]> convertFlatFileToExcel(#RequestParam(value="file") MultipartFile file,#RequestParam(value="jobid") String jobid) {
ByteArrayOutputStream archivo = new ByteArrayOutputStream();
XSSFWorkbook workbook = new XSSFWorkbook();
workbook.write(archivo);
if(null!=workbook && null!=archivo) {
workbook.close();
archivo.close();
}
byte[] documentContent = archivo.toByteArray();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
headers.set(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"myexcelfile.xls\"");
headers.setContentLength(documentContent.length);
response = new ResponseEntity<byte[]>(documentContent, headers, HttpStatus.OK);
}
**Sample UI Code:
Below is the sample code to call to Rest Service using Angular JS. Import the FileSaver js file using https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2014-11-29/FileSaver.min.js
This will have the method saveAs() to save the given excel blob data with a given name.
**
$http.post(urlBase+'/convertFlatFileToExcel.do', formData,{
transformRequest : angular.identity,
responseType: 'arraybuffer',
headers : {
'Content-Type' : undefined,
'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
}})
.then(
function (response) {
$window.sessionStorage.showProgress = "";
var file = new Blob([response.data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
saveAs(file, jobid.toUpperCase()+'.xlsx');
},
function (errResponse) {
$window.sessionStorage.showProgress = "";
$mdDialog.show($mdDialog.alert({title: 'Invalid Job ID!',textContent: 'Please enter a valid Job ID. For any issues, please contact the admin!',ok: 'GOT IT!'}));
deferred.reject(errResponse);
});

Passing an image from a Wep Api service to another Web Api service

For security reasons, I am building two Web Api services. The first Web Api app will have access to an image generating service, and will act as a security proxy. The second Web Api app will call the first app from the internet and retrieve the image.
However, I can't seem to get to negotiate passage of the image correctly. My thought was to have the security proxy Web API to get the image, and then pass it as a byte array my other service which would allow a user to download the image. However, when my browser attempts to open the image, it is always corrupted.
Here is the security proxy getting the image, which I know is successful:
public byte[] Get(string invoice, string Customer)
{
object image;
try
{
image = _repo.GetImage(invoice, Customer);
}
catch (ApplicationException exc)
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No Image with Invoice Number = {0}", invoice.ToString())),
ReasonPhrase = "Image Not Found"
};
throw new HttpResponseException(resp);
}
catch (Exception exc)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
}
return (byte[])image;
}
This returns an array with a length of 40133.
The calling Web API service looks like this:
public HttpResponseMessage Get(string invoice, string Customer)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
byte[] img = client.GetByteArrayAsync("http://localhost:1363/api/Image/" + invoice + "/" + Customer).Result;
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new ByteArrayContent(img);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/tiff");
var disposition = new ContentDispositionHeaderValue("attachment");
disposition.FileName = "ImageDocument.tif";
response.Content.Headers.ContentDisposition = disposition;
return response;
}
However, the length of the img byte array is 53514.
When the browser tries to open the image, it tells me it is corrupt. If I open the TIFF in notepad, I get :
"SUkqAAgAAAASAP4ABAABAAAAAAAAAAABBAABAAAAsAYAAAEBBAABAAAAvgQAAAIBAwABAAAAAQAAAAMBAwABAAAABAAAAAY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BrI+bBoNYQrfNIiMREWdl4zVWRERkQzVECBpNcRyMCz6PhQgZwQGQLjpCIWwgxERGLLYAx//zLWLx4IeDnBnxSGFMRgIeZ4zcaR+KuPM4KeZ6MBTqKcj8YjAQ4IejDoQ4eE07WGnra3p9w07Xhw1s7NHu+0/v+/SQf6/9+cjwp0Z0Z8KeCm4p4IGQwhoz4cwCFBZN8u8s5duXeXTLva7pN6J56l45sf8u8u
SNIP*
Anyone know what I am doing wrong?
Thanks!
Chris
Solved
If anyone is interested in the calling code that leverages the solution identified, here it is:
public HttpResponseMessage Get(string invoice, string Customer)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/tiff"));
byte[] img = client.GetByteArrayAsync("http://localhost:1363/api/Image/" + invoice + "/" + Customer).Result;
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new ByteArrayContent(img);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/tiff");
var disposition = new ContentDispositionHeaderValue("attachment");
disposition.FileName = "ImageDocument.tif";
response.Content.Headers.ContentDisposition = disposition;
return response;
}
With your above current return type (byte[]) of action, formatters of web api are probably handling them and hence you are seeing unexpected response.
can you try sending the image as a ByteArrayContent instead?(you need to have HttpResponseMessage as a return type here)
Example:
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(..your byte array here...);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg");
return response;

oauth get request token for google shows "The page you requested is invalid. "

I am trying to send a request to oauth google to get a request token.i passed all the parameters required.here is the code.please help.
The out put of the below code is a token provided by google to access its users private data.but i am getting the result that the page i requested is invalid.where did i go wrong?
should i pass the scope also in the url?
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
try {
String oauthConsumerKey = "my key";
String oauthSignatureMethod = "HMAC-SHA1";
String oauthSignature = "my signature";
// Send the request
URL url = new URL("https://www.google.com/accounts/OAuthGetRequestToken"+oauthConsumerKey+
oauthSignatureMethod+oauthSignature);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
//write parameters
writer.flush();
// Get the response
StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println("\n" +line);
}
writer.close();
reader.close();
//Output the response
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
As specified in the documentation of the OAuthGetRequestToken call, you are missing some required parameters:
oauth_consumer_key (required)
oauth_nonce (required)
oauth_signature_method (required)
oauth_signature (required)
oauth_timestamp (required)
scope (required)
oauth_callback (required)
Your query URL also has to be in a format like:
https://www.google.com/accounts/OAuthGetRequestToken?param1=value1&param2=value2&...
Please note that the Google OAuth 1.0 API is already deprecated and should not be used for new projects:
Important: OAuth 1.0 has been officially deprecated as of April 20, 2012. It will continue to work as per our deprecation policy, but we encourage you to migrate to OAuth 2.0 as soon as possible.
Further the OAuth 2 flows are way more simple, check out the documentation at Google.

Resources