Subversion Repositories bacoAlunos

Rev

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