Rev 1310 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package pt.estgp.estgweb.web;
import jomm.utils.StreamsUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
* @author Fabio
* @date 4/Abr/2008
* @time 11:03:00
* @see pt.estgp.estgweb.web
*/
public class LogsServiceStream
extends HttpServlet
{
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);
public void doGet
(HttpServletRequest request, HttpServletResponse response
)
throws ServletException,
1.5.0/docs/api/java/io/IOException.html">IOException
{
1.5.0/docs/api/java/lang/String.html">String logRequired = request.
getPathInfo();
logRequired = logRequired.
substring(1
);
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
;
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
);
if(!f.
exists())
{
response.
sendError(404
);
return;
}
1.5.0/docs/api/java/lang/String.html">String line = request.
getParameter("l");
if(line
!=
null)
{
processJson
(f,line,request,response
);
return;
}
try
{
response.
setContentType("text/plain");
response.
setCharacterEncoding("UTF-8");
5+0%2Fdocs%2Fapi+OutputStream">OutputStream out = response.
getOutputStream();
response.
setContentLength((int) f.
length());
StreamsUtils.
inputStream2OutputStream(new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(f
),out
);
out.
flush();
out.
close();
}
catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e1
)
{
logger.
error(e1
);
response.
sendError(404
);
}
}
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 {
int l =
1.5.0/docs/api/java/lang/Integer.html">Integer.
parseInt(line
);
response.
setContentType("application/json");
JSONObject obj =
new JSONObject
();
JSONArray lines =
new JSONArray
();
try {
obj.
put("lines",lines
);
obj.
put("ok",
"ok");
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"));
1.5.0/docs/api/java/lang/String.html">String readedLine
;
int i
;
boolean endReached =
false;
for(i =
0; i
< l
;i++
)
{
if(reader.
readLine() ==
null)
{
endReached =
true;
break;
}
}
if(!endReached
)
{
while((readedLine = reader.
readLine())!=
null)
{
lines.
put(readedLine
);
}
}
1.5.0/docs/api/java/lang/String.html">String toWrite = obj.
toString();
response.
setCharacterEncoding("ISO-8859-1");
response.
setContentLength(toWrite.
length());
response.
getOutputStream().
print(toWrite
);
}
catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e
)
{
logger.
error(e,e
);
response.
sendError(500
);
} catch (JSONException e
) {
logger.
error(e,e
);
response.
sendError(500
);
}
}
}