Solved

Java Socket Threaded Server http uploads

Posted on 2006-10-30
14
300 Views
Last Modified: 2013-11-24
Hi all

I have written a threaded Socket server that gets requests from a Flash client. Now what I want to do is create a class that listens on a port and accepts HTTP uploads, parses the data and writes the file correctly to disk.

I have the Listening code and the output for a gif is this

POST / HTTP/1.1
Accept: text/*
Content-Type: multipart/form-data; boundary=----------ae0ae0ei4GI3Ef1GI3cH2Ef1Ef1ei4
User-Agent: Shockwave Flash
Host: serv:2800
Content-Length: 0
Connection: Keep-Alive
Cache-Control: no-cache



for a text file

POST / HTTP/1.1
Accept: text/*
Content-Type: multipart/form-data; boundary=----------gL6Ef1gL6GI3cH2KM7Ef1Ij5cH2cH2
User-Agent: Shockwave Flash
Host: stefans:2800
Content-Length: 442
Connection: Keep-Alive
Cache-Control: no-cache

------------gL6Ef1gL6GI3cH2KM7Ef1Ij5cH2cH2
Content-Disposition: form-data; name="Filename"

foo.txt
------------gL6Ef1gL6GI3cH2KM7Ef1Ij5cH2cH2
Content-Disposition: form-data; name="Filedata"; filename="foo.txt"
Content-Type: application/octet-stream

This is a text file


last line
------------gL6Ef1gL6GI3cH2KM7Ef1Ij5cH2cH2
Content-Disposition: form-data; name="Upload"
Submit Query
------------gL6Ef1gL6GI3cH2KM7Ef1Ij5cH2cH2


However I want to know if there is a parser or class already available that will save me the work of having to read all the http rfc's.

I have looked at javax.servlet.http but have no idea how to get it working for me. (or working at all for that matter).

I will need code.

Using Eclipse 3.0.1

Thanks
0
Comment
Question by:stef4s
  • 5
  • 4
  • 4
  • +1
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17832433
0
 
LVL 5

Author Comment

by:stef4s
ID: 17832450
i have seen and downloaded this already.

Here are my problems (I followind your link)

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();

// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
    FileItemStream item = iter.next();
    String name = item.getFieldName();
    InputStream stream = item.openStream();
    if (item.isFormField()) {
        System.out.println("Form field " + name + " with value "
            + StreamUtil.asString(stream) + " detected.");
    } else {
        System.out.println("File field " + name + " with file name "
            + item.getName() + " detected.");
        // Process the input stream
        ...
    }
}


I don't have a request object (I haven't been able to get the servlet.http object working for me which i believe creates the request object).

I Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17832478
You'll have to go a little lower than that if you have your own sockets. Certain classes/objects will *not* be available, so you'll have to get into, and use, the source of that package
0
 
LVL 5

Author Comment

by:stef4s
ID: 17832534
If you could assist me with the servlet and how to create request response objects I believe the fileupload package would help.
0
 
LVL 92

Expert Comment

by:objects
ID: 17832604
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17832708
You just need to extract the stream parsing code from the source
0
 
LVL 5

Expert Comment

by:DizzyDiz
ID: 17836937
You don't create the request/response objects, the servlet container creates them for you. What you do is implement a servlet. (Write a class that extends servlet) and drop that into a servlet container like Tomcat or Jetty. I reccomend Jetty because it's simpler and quick to startup. "Drop that into a container" is a little more involved as you want to place the compiled servlet .class file into it's respective package hierarchy under the WEB-INF/classes folder of a deployed web app. "Deployed web app" is a little folder (or jar file) that contains web pages and JSPs and a WEB-INF/classes and WEB-INF/lib folder where all of the comp[iled code goes. You see your problem stems from trying to transition from a low level sockets program (which noone ever needs to write unless it's for an academic reason) to a Servlets based program. Most of what you've written is already written and handled by servlet containers like Jetty or Tomcat. These are socket servers that bind to port 80 or 8080 and handle web requests by creating the request/response objects internally and passing them to a servlet implementation. All you have to write is the servlet code. Follow this link to find out more about how servlets and Java web apps work: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 5

Author Comment

by:stef4s
ID: 17841448
Hi DizzyDiz

Thanks this is what I have so far (Downloaded Jetty 6.0.1)

in main


        Server HttpServer = new Server(pPort);
        //HttpServer.addHandler()
        HttpServer.addHandler(new HTTPHandler());
        try {
            HttpServer.start();
        } catch (Exception e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

in HTTPHandler.java

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;


public class HTTPHandler implements Handler{
   
    public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {
        System.out.println(request.toString());


        ServletOutputStream out = response.getOutputStream();
        out.print("<HTML><body>Hello world</body></HTML>");
        out.flush();
    }
   
    public void setServer(Server arg0) {
    }
   
    public Server getServer() {
        return null;
    }
   
    public void start() throws Exception {
    }
   
    public void stop() throws Exception {
    }
   
    public boolean isRunning() {
        return false;
    }
   
    public boolean isStarted() {
        return false;
    }
   
    public boolean isStarting() {
        return false;
    }
   
    public boolean isStopping() {
        return false;
    }
   
    public boolean isFailed() {
        return false;
    }
   
}




the  System.out.println(request.toString()); prints:

GET / HTTP/1.1
Accept: */*
Accept-Language: en-za
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; FDM)
Host: stefans:8080
Connection: keep-alive


GET /favicon.ico HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; FDM)
Host: stefans:8080
Connection: keep-alive

and the browser displays:

Hello world


Now how do I handle uploads?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17841509
Unless Jetty has support for multipart request parsing, i don't see how it will help you
0
 
LVL 5

Expert Comment

by:DizzyDiz
ID: 17841978
stef4s,

>Now how do I handle uploads?

You need the commons file upload component from here: http://jakarta.apache.org/commons/fileupload/
Writing web-apps in Java involves the assembly of components. The servlet container (which I've introduced you too) contains or hosts the web application. There are other components (like the commons file upload utility) that do other things likw parse upload requests, generate dynamic graphs and/or images or whatever. Installing a component in a Java web-app is a matter of dropping (copying) the component's jar file into the WEB-INF/lib folder of your web app. After reviewing your above code I see you are still working outside the standard. (I should have warned you about Jetty's alternate interfaces.) You want to implement a standard Java Servlet rather than Jetty's proprietary Handler interface. Implementing (actually extending) a servlet makes your code portable across servlet containers. The Handler interface you've implemented is specific to Jetty and gets you a little closer to the wire where you don't need to be. (There are specific uses for that interface but you don't need it for your project.) What you do is create a standard Java web app which follows the guidelines in the above link I gave you:
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html

Here's the one minute nickel tour of web-apps. A standard web app should model a certain structure. That is, your web app is contained in either a folder or a jar archive with a .war extension. The app typically contains HTML/JSP pages in the root (or any arbitrary path off of the root) and a special WEB-INF folder. The WEB-INF folder should contain your web app's descriptor file, which is an XML file that the servlet container (Jetty/Tomcat) uses to configure and deploy your web-app. Also contained in the WEB-INF folder are the classes and lib folder. The lib folder is where third party components go while the classes folder is where all of your compiled servlet code will go. So in all, you will have either a folder or a .war zip archive (understanding that jars and wars are really just zip files) that has your servlets, HTMLs/JSPs and third party components. This archive or folder is what you "deploy" to Jetty. Deploying to jetty I belive is a matter of dropping the web app into the webapps folder of the jetty distribution and starting Jetty. If you're confused I would start with a writing simple hello world JSP file in an empty web app and try deploying that and getting it to work. (The JSP can be a simple HTML page with a .jsp extension.) Once you master deploying a web app then you can move on to more complicated things like writing a servlet. Start with a simple servlet including code like what you posted above and move on from there. Post back here as you get stuck.
0
 
LVL 5

Author Comment

by:stef4s
ID: 17848233
DizziDiz

Have you implemented jetty 6 embedded?
0
 
LVL 5

Expert Comment

by:DizzyDiz
ID: 17849387
No, I haven't needed to. Jetty embedded is for people who wish to deploy a web server as an integral part of their application. (There are examples such as Snipsnap wiki and Maven-Proxy that use Jetty embedded and I have used both of these.) I always question why go through the complication of writing non-portable code to embed a web server in your app when you can write portable code that embeds your app in any arbitrary servlet container? In the few cases where it does make sense to embed a server like Jetty it comes down to a matter of convienience for the end-user. Both SnipSnap and Maven proxy offer distributions that embed Jetty and allow you to just launch and run the product without requiring a separate download and install of a servlet container. Both of these products also offer a deployable .war distribution as well. If you're thinking of embedding Jetty I' suggest you take the deployable .war approach first. Then, after you get your feet wet with web app development, try to embed the server.

Cliff
0
 
LVL 5

Author Comment

by:stef4s
ID: 17856003
I am on the right path.

Our software needs to be easily deployed. (one setup tool). The software must be usable in a client server architecture as well as a workstation only environment.

I used sockets for several reasons. Speed, and its inability to traverse proxy's. (We do not host online and only work on intra nets). I am using jetty simply for file uploads. Nothing else.
0
 
LVL 5

Accepted Solution

by:
DizzyDiz earned 500 total points
ID: 17857833
I see. Jetty does sound like a solid choice for your project. I still question wether or not you really need to extend their proprietary Handler interface or if you can simply implement a servlet. You can continue to embed Jetty using a mini embedded JEE compliant web app (consisting of only a servlet) to handle your uploads. If you're savvy enough you should be able to redeploy your solution in not only embedded mode but also as a self contained war that can be dropped into a product such as WebSphere or OrionAS or the like adding value for potential users who do not wish to install another web server for your app. By self contained war I'm referring to a JEE compliant web app that contains both a web based client installer and the server side component. What I'm really hinting at is that it might do justice if you refrain from roping your particular file upload solution to any proprietary interfaces. That solution can be modeled to plug into standard interfaces while the specifics of embedded deployment are addressed independantly.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 68
changeXy challenge 13 57
@SBGen Method 3 25
Java Timer (static) 9 15
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now