What is the Java type that can recieve MULTIPART_FORM_DATA? - jax-rs

I'm making a REST service which should recieve a file (from Postman) as Multipart type, I know i can recieve the file as "File" or "InputStream" but they come with extra characters and stuff like:
-------------48574845
content-type: idk
other-thing: idkeither
THE ACTUAL FILE GOES HERE
--------------345345345
I want to save that incoming file to the server
I've been googleing for days, found many answers here in StackOverflow BUT:
-I should not use Spring or any framework.
-I tried with IAttachment and MultiPartBody, #RequestFormParams and other things, nothin works.
-Im not familiar with Maven, i tried but when I use it my rest doens't even work
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
public String getXml(#QueryParam("age") String age, InputStream file) throws IOException {
}
This is what i got so far

Finally did it after weeks.
all you need is this beautiful thing called "import javax.mail.Multipart;"
MimeBodyPart parsedFile= new MimeBodyPart(file);
saveToFile(parsedFile.getInputStream(),parsedFile.getFileName());
return "hey i FINALLY DID IT";

Related

Google Apps Script ScriptDb permissions issue

I am having an issue trying to query the ScriptDb of a resource file in Google Apps Script. I create a script file (file1), add it as a resource to another script file (file2). I call file1 from file2 to return a handle to its ScriptDb. This works fine. I then try to query the ScriptDb but have a permissions error returned.
Both files owned by same user and in same google environment
See code below:
file 1:
function getMyDb() {
return ScriptDb.getMyDb;
}
file 2 (references file1):
function getDataFromFile1() {
var db = file1.getMyDb(); // This works
var result = db.query({..............}); // This results in a permissions error!
}
I am at a loss to understand why I can access file1 and get back a handle on the ScriptDb, but then am not able to query it, due to an permissions issue.
I have tried to force file1 to require re-authorization, but have not yet been successful. I tried adding a new function and running it, so any suggestions there would be gratefully received.
Thanks in advance
Chris
There appears to be an error in file1/line2. It says "return ScriptDb.getMyDb;" but it should say "return ScriptDb.getMyDb();"
If you leave out the ()s then when you call file1 as a library, file1.getMyDb() will return a function which you store in var db. Then the line var result = db.query({..............}) results in an error because there is no method "query" in the function.
Is that what's causing your error?
I have figured out what the problem was, a misunderstanding on my part regarding authorisation. I was thinking of it in terms of file permissions, when in fact that problem was that my code was not authorised to run the DbScript service. As my code calls a different file and receives back a pointer to a ScriptDb database it is not using the ScriptDb service, so then when it calls the db.query() it invokes the ScriptDb service, for which it is not authorised.
To resolve this I just had to create a dummy function and make a ScriptDb.getMyDb() call, which triggered authorisation for the service. The code then worked fine.
Thanks for the input though.
Chris

Why does GetBasicPropertiesAsync() sometimes throw an Exception?

In Windows8, I'm trying to use GetBasicPropertiesAsync() to get the size of a newly created file. Sometimes, but not always (~25% of the time), this call gives an exception of:
"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))".
The file is created using DotNetZip. I'm adding thousands of files to the archive which takes a few minutes to run:
using (ZipFile zip = new ZipFile())
{
zip.AddFile(...); // for thousands of files
zip.Save(cr.ArchiveName);
}
var storageFile = await subFolder.GetFileAsync(cr.ArchiveName);
// storageFile is valid at this point
var basicProperties = await storageFile.GetBasicPropertiesAsync(); // BOOM!
A few apparently random things seem to decrease the likelihood of the exception:
Deleting an existing copy of cr.ArchiveName before the start of the loop.
Not viewing the directory using File Explorer
Weird, huh? It smells like it might be a bug related to File System Tunneling or maybe it's some internal caching that DotNetZip is performing and holding onto resources (maybe renaming the TEMP file) even after the ZipFile is disposed?
Trying to (unsuccessfully) answer my own question.
At first, I though this was a known issue with DotNetZip holding onto file handles until the next garbage collection. I am using the SL/WP7 port of DotNetZip from http://slsharpziplib.codeplex.com/ which presumably doesn't include the bug fixed by this workitem:
http://dotnetzip.codeplex.com/workitem/12727
But, according to that theory, doing:
GC.Collect();
GC.WaitForPendingFinalizers();
should have provided a work around, which it didn't.
Next I tried using handle, which didn't show any other activity on the failing StorageFile.
So for now, I'm still stumped.

IzPack: How to show UTF-8 license with LicensePanel?

As title. My license file contains UTF-8 characters and by default IzPack's LicensePanel seems to expect ASCII text files.
Is there a solution to this?
UPDATE:
I tried using "encoding" attributes with my resource line:
<res id="LicencePanel.licence" src="Licence.txt" encoding="utf-8"/>
It didn't work.
I have had a similar problem with my LicencePanel.licence resource. I have an InfoPanel.Info resource in my installation as well. Both my info file (readme.txt) and licence (licence.txt) are in plain text format. My compiler accepts the readme file, but not the licence file when I run the installation.
Perhaps it isn't an encoding problem, since both files were in the same format, but the info file was accepted and the license was not.
Looks like this isn't going to work. I looked at the source for 4.3.5 and it looks like this may be a bug. Maybe it is fixed in a future version. I had a look at the source and this is the issue. Inside LicencePanel.java:
String resNamePrifix = "LicencePanel.licence";
licence = ResourceManager.getInstance().getTextResource(resNamePrifix);
ResourceManager has two methods:
public String getTextResource(String resource, String encoding) throws ResourceNotFoundException, IOException
public String getTextResource(String resource) throws ResourceNotFoundException, IOException
The first second one is being used while the first one should be used.
Edit: Just checked 5.0.0-rc1 and I think the issue occurs there too. (Didn't test just glanced at the code).

WebGet UriTemplate fails on dot ('.'/%2E)?

I've been trying for a while now to figure out why this UriTemplate does not work as part of a WCF service and can't seem to get anywhere:
[WebGet(UriTemplate="api/1.0/version")]
string GetVersion();
A quick test shows that UrlTemplateTable matches it fine (output is 'api/1.0/version'):
static void Main(string[] args)
{
Uri prefix = new Uri("http://localhost/");
System.UriTemplateTable table = new System.UriTemplateTable(prefix);
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(new UriTemplate("api/1.0/version"), "a"));
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(new UriTemplate("api/2.0/version"), "b"));
table.KeyValuePairs.Add(new KeyValuePair<UriTemplate, Object>(new UriTemplate("api/version"), "c"));
Uri uri = new Uri("http://localhost/api/1.0/version");
UriTemplateMatch match = table.MatchSingle(uri);
Console.WriteLine("{0}", match.Template.ToString());
}
The dot is not an illegal character in URLs, RequestPathInvalidCharacters does not exclude it, there are no rewrite rules in place that could interfere. Couldn't find anything in the documentation on it either.
While there is an obvious workaround, not use the dot in the template, I'm curious as to why it fails with 'HTTP 404/The resource cannot be found'.
I ran into the same issue before. I realized it was a limitation in IIS, and nothing to do with WCF. When IIS initially intercepts the request assumes the value following the . represents an extension, so it tries to find a managed handler for that extension. Since it does not find any, it just throws a 404.
Regards
Pablo.

Apache Error is incompatible with throws class

I've got a quick question. In my grails project I am making some web service calls. One of the calls (for a search function) tends to timeout if not enough characters are given to search for. I can't increase the number of required characters so I instead am trying to catch the exception and display an error page asking the user to add more detailed parameters.
The method looks like this:
import org.apache.http.client.HttpResponseException
class RestSearchService implements SearchService {
List<Person> getPersonSearch( String fName, String lName) throws HttpResponseException {
...
// Make the call
...
}
}
I then catch the thrown exception in the controller to redirect to the error page. I've tested it and this code appears to work fine. The problem is that the method above is underlined (I'm using SpringSource Tool Suite for an IDE) and says
Exception HttpResponseException is not compatible with
throws clause in SearchService.getPersonSearch(String, String)
Does anyone know what might be causing this? Also, might it mean that there is an actual problem or circumstance where this will cause the app to break? Like I said, from what I can tell the throw/redirect is working like a champ, but that error makes me nervous about moving the app to production.
Thanks in advance,
-Mike
I would say that your interface SearchService is not right! what is the signature of the method 'getPersonSearch' in the interface?
its like this:
List<Person> getPersonSearch( String fName, String lName);
or like this:
List<Person> getPersonSearch( String fName, String lName) throws HttpResponseException;
the second one is correct, if you have the first one, thats should be the problem!