API Overview API Index Package Overview Direct link to this page
JavaOnTracks 0.1.2
  net.jot.web.server. JOTWebResponse View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627

font color='#880088'>
package net.jot.web.server;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Vector;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import net.jot.logger.JOTLogger;
import net.jot.logger.JOTLoggerLocation;
import net.jot.utils.JOTTimezoneUtils;

/**
 * HttpServletResponse impl.
 * @author thibautc
 */
public class JOTWebResponse implements HttpServletResponse
{

    static final JOTLoggerLocation logger = new JOTLoggerLocation(JOTLogger.CAT_SERVER, JOTWebResponse.class);
    /** The connection socket to the client*/
    private final Socket socket;
    private final static int DEFAULT_BUFFER_SIZE = 5000;
    /** Default: 5K */
    private int bufferSize = DEFAULT_BUFFER_SIZE;
    /** Default: default java VM Loc.*/
    private Locale loc = Locale.getDefault();
    /** Default: ISO-8859-1*/
    private String encoding = "ISO-8859-1";
    private boolean isCommited = false;
    private boolean bufferUsed = false;
    /** default: unset*/
    private int contentLength = -1;
    /** default: unset*/
    private String contentType = null;    // user print and output streams : only one of the two can be used !
    private MyWriter print = null;
    private MyStream out = null;
    /** Defaults to 200: OK*/
    private int statusCode = SC_OK;
    /** headers (header name -> Vector of values(string)) **/
    Hashtable headers = new Hashtable();
    // vector of Cookie
    Vector cookies = new Vector();
    // we need the request to encode URL's etc...
    private JOTWebRequest request;

    /**
     * You usually would't create a response on your own ut be provided one by the container.
     * @param socket
     * @param request
     */
    public JOTWebResponse(Socket socket, JOTWebRequest request)
    {
        this.socket = socket;
        this.request = request;
    //TODO: read jsessionID from the request (if avail)
    }

    public void addCookie(Cookie cookie)
    {
        if (isCommited)
        {
            throw new IllegalStateException("We already have flushed the buffer, to late!");
        }
        cookies.add(cookie);
    }

    public boolean containsHeader(String headerName)
    {
        return headers.containsKey(headerName);
    }

    public String encodeURL(String url)
    {
        return addSessionIdToURL(url, request.getSessionId());
    }

    public String encodeRedirectURL(String url)
    {
        // TODO: what is the diff with encodeURL ??
        return encodeURL(url);
    }

    /**
     * @deprecated
     * @param arg0
     * @return
     */
    public String encodeUrl(String arg0)
    {
        return encodeURL(arg0);
    }

    /**
     * @deprecated
     * @param arg0
     * @return
     */
    public String encodeRedirectUrl(String arg0)
    {
        return encodeRedirectURL(arg0);
    }

    public void sendError(int statusCode, String err) throws IOException
    {
        if (isCommited)
        {
            throw new IllegalStateException("Response already commited !");
        }
        sendMessage("" + statusCode, err);
        reset();
    }

    public void sendError(int statusCode) throws IOException
    {
        sendError(statusCode, null);
    }

    public void sendRedirect(String url) throws IOException
    {
        url = absoluteURL(url);
        if (isCommited)
        {
            throw new IllegalStateException("Response already commited !");
        }
        setStatus(SC_MOVED_TEMPORARILY);
        setHeader("Location", url);
        flushBuffer();
        isCommited = true;
    }

    public void setDateHeader(String key, long date)
    {
        // UTC/GMT style date
        String value = JOTTimezoneUtils.convertTimezone(new Date(date), "GMT + 0", JOTTimezoneUtils.FORMAT_HEADER) + " GMT";
        setHeader(key, "" + value);
    }

    public void addDateHeader(String key, long date)
    {
        // UTC/GMT style date
        String value = JOTTimezoneUtils.convertTimezone(new Date(date), "GMT + 0", JOTTimezoneUtils.FORMAT_HEADER) + " GMT";
        addHeader(key, "" + value);
    }

    public void setHeader(String key, String value)
    {
        headers.remove(key);
        addHeader(key, value);
    }

    public void addHeader(String key, String value)
    {
        if (isCommited)
        {
            throw new IllegalStateException("We already have flushed the buffer, to late!");
        }
        Vector v = new Vector();
        if (headers.containsKey(key))
        {
            v = (Vector) headers.get(key);
        }
        v.add(value);
        headers.put(key, v);
    }

    public void setIntHeader(String key, int value)
    {
        setHeader(key, "" + value);
    }

    public void addIntHeader(String key, int value)
    {
        addHeader(key, "" + value);
    }

    public void setStatus(int status)
    {
        if (isCommited)
        {
            throw new IllegalStateException("We already have flushed the buffer, to late!");
        }
        statusCode = status;
    }

    /**
     * @deprecated use sendError() instead
     * @param status
     * @param sm
     */
    public void setStatus(int status, String sm)
    {
        // deprecated, uses sendError instead
        try
        {
            sendError(status, sm);
        } catch (IOException e)
        {
        }
    }

    public String getCharacterEncoding()
    {
        return encoding;
    }

    public String getContentType()
    {
        return contentType;
    }

    public ServletOutputStream getOutputStream() throws IOException
    {
        if (print != null)
        {
            throw new IllegalStateException("Can not gte both an outputstream and a printwriter !");
        }
        if (out == null)
        {
            out = new MyStream(socket.getOutputStream());
        }
        bufferUsed = true;
        return (ServletOutputStream) out;
    }

    public PrintWriter getWriter() throws IOException
    {
        if (out != null)
        {
            throw new IllegalStateException("Can not get both an outputstream and a printwriter !");
        }
        if (print == null)
        {
            print = new MyWriter(socket.getOutputStream());
        }
        bufferUsed = true;
        return print;
    }

    public void setCharacterEncoding(String enc)
    {
        if (isCommited)
        {
            throw new IllegalStateException("We already have flushed the buffer, to late!");
        }
        encoding = enc;
    }

    public void setContentLength(int length)
    {
        if (isCommited)
        {
            throw new IllegalStateException("We already have flushed the buffer, to late!");
        }
        contentLength = length;
    }

    public void setContentType(String type)
    {
        if (isCommited)
        {
            throw new IllegalStateException("We already have flushed the buffer, to late!");
        }
        contentType = type;
    }

    public void setBufferSize(int size)
    {
        if (bufferUsed)
        {
            throw new IllegalStateException("Can't change buffer size, already retrieved.");
        }
        bufferSize = size;
        clearBuffer();
    }

    public int getBufferSize()
    {
        return bufferSize;
    }

    public void flushBuffer() throws IOException
    {
        if(print==null && out==null)
        {
            writePreamble();
        }
        else if (print != null)
        {
            print.flush();
        } else if (out != null)
        {
            out.flush();
        }
    }

    public void resetBuffer()
    {
        if (isCommited)
        {
            throw new IllegalStateException("The buffer was already flushed !");
        }
        clearBuffer();
    }

    public boolean isCommitted()
    {
        return isCommited;
    }

    public void reset()
    {
        clearBuffer();
        statusCode = SC_OK;
        headers.clear();
        cookies.clear();
    }

    public void setLocale(Locale locale)
    {
        loc = locale;
    }

    public Locale getLocale()
    {
        return loc;
    }

    public String absoluteURL(String path)
    {
        if(path==null)
            return null;
        URL url = null;
        try
        {
            url = new URL(path);
            // if we get here we have an absolute url already
        } catch (MalformedURLException e)
        {
            // if this failed this was a relative url
            String ctxURL = request.getRequestURL().toString();
            try
            {
                // building the new URL using the current request URL as the context
                url = new URL(new URL(ctxURL), path);
            } catch (MalformedURLException e2)
            {
                throw new IllegalArgumentException("Could not build absolute URL for: "+ path, e2);
            }
        }
        return (url.toExternalForm());

    }

    private void clearBuffer()
    {
        print = null;
        out = null;
    }

    private String addSessionIdToURL(String url, String sessionId)
    {
        //TODO: only encode if url is relevant to this webapp ? 
        // TODO: what about relative URL's ??

        if ((url == null) || (sessionId == null))
        {
            return (url);
        }
        String path = url;
        String args = "";
        String anchor = "";
        int urlIndex = url.indexOf('?');
        if (urlIndex != -1)
        {
            path = url.substring(0, urlIndex);
            args = url.substring(urlIndex);
        }
        int anchorIndex = path.indexOf('#');
        if (anchorIndex != -1)
        {
            anchor = path.substring(anchorIndex);
            path = path.substring(0, anchorIndex);
        }
        StringBuffer sb = new StringBuffer(path).append(";jsessionid=").append(sessionId).append(anchor).append(args);
        return (sb.toString());
    }

    public void destroy()
    {
        Exception ex = null;
        try
        {
            flushBuffer();
        } catch (Exception e)
        {
            ex = e;
        }
        //TODO: close the streams and socket etc ... ??
        try
        {
            if (out != null)
            {
                out.close();
            }
        } catch (Exception e)
        {
            ex = e;

        }
        try
        {
            if (print != null)
            {
                print.close();
            }
        } catch (Exception e)
        {
            ex = e;

        }
        try
        {
            if (socket != null && !socket.isClosed() && socket.getInputStream() != null)
            {
                socket.getInputStream().close();
            }
        } catch (Exception e)
        {
            ex = e;

        }
        try
        {
            if (socket != null && !socket.isClosed() && socket.getOutputStream() != null)
            {
                socket.getOutputStream().close();
            }
        } catch (Exception e)
        {
            ex = e;

        }
        if (ex != null)
        {
            logger.exception("Errors closing request socket.", ex);
        }
    }

    /**
     * internal message page (errorrs etc..)
     * Does not set status_code, do that beforehand
     * @param string
     * @param err
     */
    private void sendMessage(String title, String message) throws IOException
    {
        setStatus(statusCode);
        sendText(JOTWebHelper.MSG_HEAD +"ERROR "+ title + JOTWebHelper.MSG_HEAD2 + message + JOTWebHelper.MSG_TAIL);
        flushBuffer();
    }

    /**
     * Send some message to the browser.
     * Used mostlt for error messages
     * @param err
     */
    private void sendText(String text)
    {
        setContentType("text/html");
        setContentLength(text.length());
        try
        {
            if (print != null)
            {
                print.print(text);
            } else if (out != null)
            {
                out.write(text.getBytes());
            } else
            {
                getOutputStream().write(text.getBytes());
            }
        } catch (Exception e)
        {/* TODO */
            logger.exception("Failed adding text to response.", e);
        }

    }

    void writePreamble()
    {
        if (!isCommited)
        {
            if(request.getSessionId()!=null)
            {
                Cookie cookie=new Cookie("JSESSIONID",request.getSessionId());
                //cookie.setMaxAge(request.get);
                cookies.add(cookie);
            }
            try
            {
                PrintWriter p = new PrintWriter(socket.getOutputStream());
                p.println("HTTP/1.1 " + statusCode);
                if (!headers.containsKey("Location"))
                    p.println("Location: " + request.getServletPath());
                //content-type
                p.println("Content-encoding: " + encoding);
                if (contentType != null)
                {
                    p.println("Content-type: " + contentType);
                }
                if (contentLength != -1)
                {
                    p.println("Content-length: " + contentLength);
                }
                //headers
                p.println("Status: " + statusCode);
                if (!headers.containsKey("Server"))
                {
                    p.println("Server: " + JOTWebHelper.INFOS);
                }
                if (!headers.containsKey("Date"))
                {
                    String now = JOTTimezoneUtils.convertTimezone(new Date(), "GMT + 0", JOTTimezoneUtils.FORMAT_HEADER);
                    p.println("Date: " + now + " GMT");
                }
                Enumeration e = headers.keys();
                while (e.hasMoreElements())
                {
                    String key = (String) e.nextElement();
                    StringBuffer header = new StringBuffer(key).append(": ");
                    Vector values = (Vector) headers.get(key);
                    for (int i = 0; i != values.size(); i++)
                    {
                        header.append((String) (values.get(i)));
                        if (i < values.size() - 1)
                        {
                            header.append(";");
                        }
                    }
                    p.println(header);
                }
                //cookies
                if (cookies.size() > 0)
                {
                    StringBuffer header = new StringBuffer("Cookies: ");
                    for (int i = 0; i != cookies.size(); i++)
                    {
                        Cookie cookie = (Cookie) cookies.get(i);
                        header.append(cookie.getName()).append("=").append(cookie.getValue());
                        if (i < cookies.size() - 1)
                        {
                            header.append(";");
                        }
                    }
                    p.println(header);
                }
                //end headers, add empty line
                p.println();
                p.flush();
            } catch (Exception e)
            {
                logger.exception("Failure while writing headers to response.", e);
            }
        }
        isCommited = true;
    }

    class MyStream extends ServletOutputStream
    {

        BufferedOutputStream stream;

        MyStream(OutputStream out)
        {
            stream = new BufferedOutputStream(out, bufferSize);
        }

        /**
         * Override, once it's flushed the first time, setting to commited
         * @throws java.io.IOException
         */
        public synchronized void flush() throws IOException
        {
            //call the response flushbuffer (ie: commit)
            writePreamble();
            // then proceed with comitting the actual content.
            stream.flush();
        }

        public void write(int b) throws IOException
        {
            stream.write(b);
        }
    }

    class MyWriter extends PrintWriter
    {

        MyWriter(OutputStream out)
        {
            super(out);
            setBufferSize(bufferSize);
        }

        /**
         * Override, once it's flushed the first time, setting to commited
         * @throws java.io.IOException
         */
        public synchronized void flush()
        {
            writePreamble();
            super.flush();
        }
    }
}

Generated By: JavaOnTracks Doclet 0.1.5     ©Thibaut Colar