When exactly are the files transmitted to the server? - servlets

If I have a servlet that handles file uploads, when does the server actually receive the files?
Are they already available when I invoke HttpServletRequest::getParts or will the individual files only be fetched when I actually call the corresponding Part::write?
Asked differently, is it my servlet's responsibility to implement a parallel upstream of all files or do I just need to worry about writing them all to disk?

Are they already available when I invoke HttpServletRequest::getParts
Yes.
or will the individual files only be fetched when I actually call the corresponding Part::write?
Technically, files are not fetched. The server doesn't actually "download" files from the client. It's the client who sends the files as part of the request body to the server and the server has just to listen for these files and write them to a temporary storage before invoking the service method. The server will only invoke the service method when the request body is fully read. This is regardless of the request body content type. Hence the "Yes" on the previous question.
Asked differently, is it my servlet's responsibility to implement a parallel upstream of all files
Absolutely not. You don't at all need to synchronize anything on the HttpServletRequest. At most only on the HttpSession, but even this doesn't have a role here.
or do I just need to worry about writing them all to disk?
Yes, exactly that. Simply grab the Part, validate it and finally write it.
See also:
Recommended way to save uploaded files in a servlet application

Related

Interactive HTTP server

For manually testing an HTTP client in my application, I'd like to use a tool which starts an HTTP server my application can connect to and that lets me respond to request from my application manually. I'm basically looking for a tool with a GUI that lists all incoming requests and allows me to select a status code and type a response message. I've already tested the functionality with unit tests but I also want to verify it manually with no mocking etc.
Sounds simple but I didn't find such a tool. I've found some that can be scripted but no interactive one. Do you know one?
This can probably be written relatively easily by creating the Swing GUI dialog popup inside the servlet servicing methods. Have never seen Tomcat running this way but probably it would. Only, mind the server time out. It may be not long enough for you to make an input and require to be configured, also on the client side. Also, parallel calls will make multiple popups that may be difficult to respond but probably this is a single client app.
As a simplest solution, server GUI can be simply disposed after call and newly created as the next call arrives. This will make eveything indepenent on how servlet container is managing the servlets (does it stays, is it destroyed, maybe class is unloaded, who knows). Advanced solution could include the "server servlet" that would interact through its own JSP page but then it may be complex to update it when the new call arrives (unless maybe refresh periodically).

Non-locking sleep/waitfor/delay function for ASP.NET

I am writing an ASP.NET class that interfaces with an external application. The flow of the transaction between the web server and this application is as follows:
My object writes a file to a directory.
The outside application detects this file and processes it. This can take between 1-5 seconds.
The outside application writes a response file to the same directory.
My object detects the response file and parses the results.
The 1-5 seconds it can take for the external application to process my file is my problem. The most straightforward way to wait for the file seems to be something like this:
Do While Not File.Exists(f)
Thread.Sleep(500)
Loop
Of course, Thread.Sleep() completely locks up the rest of my website until the outside application processes the file. Clearly, this is not a workable solution.
How can I effectively "wait" for my file to be processed without locking up the rest of my website?
Use the FileSystemWatcher - when a file will be created it will fire an event that you can subscribe to.

Passing an Arraylist of Java objects to a servlet from Java program

I would like to pass an arrayList of objects to a servlet from a java program.
Can some one please tell me, how this can be done.
Look at this link they describe the process ind detail
http://www2.sys-con.com/ITSG/virtualcd/java/archives/0309/darby/index.html
Please note that if you are going to serialize objects back and forth that the compiled version must be in sync on both the client and the server or you will get errors. I would recommend converting your objects to either XML or JSON and then reading them from that on the server side. That way if you client and server code get out of sync it will still work.
For the client I would recommend Apache's HttpClient (or whatever they have renamed it to)
Have you considered using a web service framework for this instead of coding a naked servlet? The whole business might be about 10 lines of code using, for example, an Apache CXF JAX-RS service and client. If the objects are complex, you might want to use a full SOAP service.

Calling a method in an ASP.NET application from a Windows application

Other than using a web service, is there anyway to call a method in a web app from a windows application? Both run on the same machine.
I basically want to schedule a job to run a windows app which updates some file (for a bayesian spam filter), then I want to notify the web app to reload that file.
I know this can be done in other ways but I'm curious to know whether it's possible anyway.
You can make your windows app connect to the web app and do a GET in a page that responds by reloading your file, I don't think it is strictly necessary to use a web service. This way you can also make it happen from a web browser.
A Web Service is the "right" way if you want them to communicate directly. However, I've found it easier in some situations to coordinate via database records. For example, my web app has bulk email capability. To make it work, the web app just leaves a database record behind specifying the email to be sent. The WinApp scans periodically for these records and, when it finds one with an "unprocessed" status, it takes the appropriate action. This works like a charm for me in a very high volume environment.
You cannot quite do this in the other direction only because web apps don't generally sit around in a timing loop (there are ways around this but they aren't worth the effort). Thus, you'll require some type of initiating action to let the web app know when to reload the file. To do this, you could use the following code to do a GET on a page:
WebRequest wrContent = WebRequest.Create("http://www.yourUrl.com/yourpage.aspx");
Stream objStream = wrContent.GetResponse().GetResponseStream();
// I don't think you'll need the stream Reader but I include it for completeness
StreamReader objStreamReader = new StreamReader(objStream);
You'll then reload the file in the PageLoad method whenever this page is opened.
How is the web application loading the file? If you were using a dependency on the Cache object, then simply updating the file will invalidate the Cache entry, causing your code to reload that entry when it is found to be null (or based on the "invalidated" event).
Otherwise, I don't know how you would notify the application to update the file.
An ASP.NET application only exists as an instance to serve a request. This is why web services are an easy way to handle this - the application has been instantiated to serve the service request. If you could be sure the instance existed and got a handle to it, you could use remoting. But without having a concrete handle to an instance of the application, you can't invoke the method directly.
There's plenty of other ways to communicate. You could use a database or some other kind of list which both applications poll and update periodically. There are plenty of asynchronous MQ solutions out there.
So you'll create a page in your webapp specifically for this purpose. Use a Get request and pass in a url parameter. Then in the page_load event check for this paremter. if it exists then do your processing. By passing in the parameter you'll prevent accidental page loads that will cause the file to be uploaded and processed when you don't want it to be.
From the windows app make the url Get request by using the .Net HttpWebRequest. Example here: http://www.codeproject.com/KB/webservices/HttpWebRequest_Response.aspx

Disable WSDL Cache in ASP.NET

I am using SoapExtensionReflector to modify the WSDL sent to the client due to a complex proxy setup. This modification is based on a query string variable:
whatever.com/Service.asmx?WSDL&customaddress=proxy.whatever.com
This works well, and the modification is successful. However, the WSDL is cached by ASP.NET the first time, so a second call to, say...
whatever.com/Service.asmx?WSDL&customaddress=dohicky.whatever.com
will still use the variables from the original call. I would like to disable WSDL caching and force ASP.NET to reprocess the WSDL every time. Is there an easy way to do this programmatically?

Resources