Subversion Repositories bacoAlunos

Rev

Rev 1076 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1076 jmachado 1
package pt.estgp.estgweb.web;
2
 
3
import jomm.utils.StreamsUtils;
4
import org.apache.log4j.Logger;
5
import org.json.JSONArray;
6
import org.json.JSONException;
7
import org.json.JSONObject;
8
 
9
import javax.servlet.ServletException;
10
import javax.servlet.http.HttpServlet;
11
import javax.servlet.http.HttpServletRequest;
12
import javax.servlet.http.HttpServletResponse;
13
import java.io.*;
14
 
15
/**
16
 * @author Fabio
17
 * @date 4/Abr/2008
18
 * @time 11:03:00
19
 * @see pt.estgp.estgweb.web
20
 */
21
public class LogsServiceStream extends HttpServlet
22
{
23
 
24
    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(LogsServiceStream.class);
25
 
26
 
27
    public void doGet(HttpServletRequest request, HttpServletResponse response)
28
            throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException
29
    {
30
 
31
 
32
 
33
 
34
 
35
        1.5.0/docs/api/java/lang/String.html">String logRequired = request.getPathInfo();
36
        logRequired = logRequired.substring(1);
37
 
38
 
39
        1.5.0/docs/api/java/lang/String.html">String logFile = pt.estgp.estgweb.Globals.JOB_SERVICES_LOG_DIR + java.io.1.5.0/docs/api/java/io/File.html">File.separator + logRequired;
40
        java.io.1.5.0/docs/api/java/io/File.html">File f = new java.io.1.5.0/docs/api/java/io/File.html">File(logFile);
41
        if(!f.exists())
42
        {
43
            response.sendError(404);
44
            return;
45
        }
46
 
47
        1.5.0/docs/api/java/lang/String.html">String line = request.getParameter("l");
48
        if(line != null)
49
        {
50
            processJson(f,line,request,response);
51
            return;
52
        }
53
 
54
 
55
        try
56
        {
57
            response.setContentType("text/plain");
58
            5+0%2Fdocs%2Fapi+OutputStream">OutputStream out = response.getOutputStream();
59
            response.setContentLength((int) f.length());
60
            StreamsUtils.inputStream2OutputStream(new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(f),out);
61
            out.flush();
62
            out.close();
63
        }
64
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e1)
65
        {
66
            logger.error(e1 );
67
            response.sendError(404);
68
        }
69
 
70
    }
71
 
72
    private void processJson(1.5.0/docs/api/java/io/File.html">File f, 1.5.0/docs/api/java/lang/String.html">String line, HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/io/IOException.html">IOException {
73
        int l = 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(line);
74
        response.setContentType("application/json");
75
 
76
        JSONObject obj = new JSONObject();
77
        JSONArray lines = new JSONArray();
78
 
79
        try {
80
            obj.put("lines",lines);
81
            obj.put("ok","ok");
82
            1.5.0/docs/api/java/io/BufferedReader.html">BufferedReader reader = new 1.5.0/docs/api/java/io/BufferedReader.html">BufferedReader(new 1.5.0/docs/api/java/io/InputStreamReader.html">InputStreamReader(new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(f),"UTF-8"));
83
            1.5.0/docs/api/java/lang/String.html">String readedLine;
84
            int i;
85
            boolean endReached = false;
86
            for(i = 0; i < l;i++)
87
            {
88
                if(reader.readLine() == null)
89
                {
90
                    endReached = true;
91
                    break;
92
                }
93
            }
94
            if(!endReached)
95
            {
96
                while((readedLine = reader.readLine())!=null)
97
                {
98
                    lines.put(readedLine);
99
                }
100
            }
101
 
102
            1.5.0/docs/api/java/lang/String.html">String toWrite = obj.toString();
103
            response.setCharacterEncoding("ISO-8859-1");
104
            response.setContentLength(toWrite.length());
105
            response.getOutputStream().print(toWrite);
106
 
107
        }
108
        catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e)
109
        {
110
            logger.error(e,e);
111
            response.sendError(500);
112
        } catch (JSONException e) {
113
            logger.error(e,e);
114
            response.sendError(500);
115
        }
116
 
117
    }
118
 
119
}
120