You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Eugene Labunsky <el...@trendmedium.com> on 2007/04/13 01:07:25 UTC

(Unknown)

Dear Mark,

I have problem with OUTPUT data, but TreeMap used for parsing of HTTP 
request header.
No problems with the short sample reply, but try my code with added 1024 
http lines, run this HTTP server and then try 2-3 times refresh 
http://server/

Review my sample (just run()):

       public void run() {

            String url;
            Map headers = new TreeMap();
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(this.in));
            PrintWriter out = new PrintWriter(
                    new BufferedWriter(new OutputStreamWriter(this.out)));

            try {

                // Get request URL.
                url = in.readLine().split(" ")[1];

                // Read header
                String line;
                while ((line = in.readLine()) != null && !line.equals("")) {
                    String[] tokens = line.split(": ");
                    headers.put(tokens[0], tokens[1]);
                }

                // Write header
                out.println("HTTP/1.0 200 OK");
                out.println("Content-Type: text/html");
                out.println("Server: MINA Example");
                out.println();

                out.println("<html><head></head><body>");

                Iterator it = headers.entrySet().iterator();
                while (it.hasNext()) {
                    Entry e = (Entry) it.next();
                    out.println("<p>" + e.getKey() + "-" + e.getValue() + 
"</p>");
                }


                for (int i = 0; i < 1024; i++) {
                    out.println("<p>this is line: " + i + "</p>");
                }

                out.println("</body></html>");

            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                out.flush();
                out.close();
                try {
                    in.close();
                }
                catch (IOException e) {
                }
            }
        }

You can see no any Hash etc... Just for(;;) and println to PrintWriter.
You will see after 2nd refresh MINA HTTP server will be missed about top 
part of HTTP reply.

But if you will delete header parsing part and keep just 1024 lines output 
MINA will reply correct. This is strange...
This code works correctly:

        public void run() {

            String url;
            Map headers = new TreeMap();
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(this.in));
            PrintWriter out = new PrintWriter(
                    new BufferedWriter(new OutputStreamWriter(this.out)));

            try {

                // Write header
                out.println("HTTP/1.0 200 OK");
                out.println("Content-Type: text/html");
                out.println("Server: MINA Example");
                out.println();

                out.println("<html><head></head><body>");

                for (int i = 0; i < 1024; i++) {
                    out.println("<p>this is line: " + i + "</p>");
                }

                out.println("</body></html>");

            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                out.flush();
                out.close();
                try {
                    in.close();
                }
                catch (IOException e) {
                }
            }
        }

>Sorry.  Its not a HashMap, but a TreeMap.

>http://mina.apache.org/report/1.1/xref/org/apache/mina/example/httpserver/stream/HttpProtocolHandler.html#69

>TreeMaps use natural ordering, so I am still not sure that ordering can be
>guaranteed.  The reason I mention ordering is that when you pull the
>information out of the TreeMap, it might not come out in the same order in
>which it was put in.

>I hope this clears up my previous statements.

>
> Dear Mark,
>
> I've added output to the STREAM example (just Main & HttpProtocolHandler
> classes).
> In this sample I can't see any HashMap, just write to PrintWriter and
> finally flush & close it (I keep all sample just added those 2 lines of
> code
> for output 1024 lines).
> Could you advise me, where is problem?
>
> >Based on previous discussions on this mailing list, I think that the
> >'codec' based HTTP example is the preferred method.  If you are
> >placing each line in to a HashMap as per the example, this could be
> >your problem because HashMaps do not guarantee order.  Check the API
> >javadocs for more information.
>
> --
> ..Cheers
>
> Mark
>
>
> On 4/12/07, Eugene Labunsky <el...@trendmedium.com> wrote:
> > Hello,
> >
> > I have problem with http stream output.
> > I have project based on http stream sample and found that MINA can't
> > output
> > more than 200 lines http reply correctly. It mixed lines in reply ;(
> First
> > time it reply correct, then not. I have added small peace of code to
> > public
> > void run() in sample:
> >
> > for (int i=0;i<1024;i++)
> >     out.println("<p>this is line: " + i+"</p>");
> >
> > And ... With all available MINA releases I have problem. MINA can reply
> > correctly or from 284 lines...  Please, let me know where I'm wrong...
>

Regards,
Eugene Labunsky.