use of javax.servlet.ServletOutputStream in project dubbo by alibaba.
the class Restful method execute.
public void execute(Map<String, Object> context) throws Exception {
Result result = new Result();
if (request.getParameter("url") != null) {
url = URL.valueOf(URL.decode(request.getParameter("url")));
}
if (context.get(WebConstants.CURRENT_USER_KEY) != null) {
User user = (User) context.get(WebConstants.CURRENT_USER_KEY);
currentUser = user;
operator = user.getUsername();
role = user.getRole();
context.put(WebConstants.CURRENT_USER_KEY, user);
}
operatorAddress = (String) context.get("clientid");
if (operatorAddress == null || operatorAddress.isEmpty()) {
operatorAddress = (String) context.get("request.remoteHost");
}
context.put("operator", operator);
context.put("operatorAddress", operatorAddress);
String jsonResult = null;
try {
result = doExecute(context);
result.setStatus("OK");
} catch (IllegalArgumentException t) {
result.setStatus("ERROR");
result.setCode(3);
result.setMessage(t.getMessage());
}// }
catch (Throwable t) {
result.setStatus("ERROR");
result.setCode(1);
result.setMessage(t.getMessage());
}
response.setContentType("application/javascript");
ServletOutputStream os = response.getOutputStream();
try {
jsonResult = JSON.toJSONString(result);
os.print(jsonResult);
} catch (Exception e) {
response.setStatus(500);
os.print(e.getMessage());
} finally {
os.flush();
}
}
use of javax.servlet.ServletOutputStream in project adempiere by adempiere.
the class WebUtil method streamAttachment.
// getClosePopupButton
/**
* Stream Attachment Entry
* @param response response
* @param attachment attachment
* @param attachmentIndex logical index
* @return error message or null
*/
public static String streamAttachment(HttpServletResponse response, MAttachment attachment, int attachmentIndex) {
if (attachment == null)
return "No Attachment";
int realIndex = -1;
MAttachmentEntry[] entries = attachment.getEntries();
for (int i = 0; i < entries.length; i++) {
MAttachmentEntry entry = entries[i];
if (entry.getIndex() == attachmentIndex) {
realIndex = i;
break;
}
}
if (realIndex < 0) {
log.fine("No Attachment Entry for Index=" + attachmentIndex + " - " + attachment);
return "Attachment Entry not found";
}
MAttachmentEntry entry = entries[realIndex];
if (entry.getData() == null) {
log.fine("Empty Attachment Entry for Index=" + attachmentIndex + " - " + attachment);
return "Attachment Entry empty";
}
// Stream Attachment Entry
try {
// 2k Buffer
int bufferSize = 2048;
int fileLength = entry.getData().length;
//
response.setContentType(entry.getContentType());
response.setBufferSize(bufferSize);
response.setContentLength(fileLength);
//
log.fine(entry.toString());
// timer start
long time = System.currentTimeMillis();
//
ServletOutputStream out = response.getOutputStream();
out.write(entry.getData());
out.flush();
out.close();
//
time = System.currentTimeMillis() - time;
double speed = (fileLength / 1024) / ((double) time / 1000);
log.info("Length=" + fileLength + " - " + time + " ms - " + speed + " kB/sec - " + entry.getContentType());
} catch (IOException ex) {
log.log(Level.SEVERE, ex.toString());
return "Streaming error - " + ex;
}
return null;
}
use of javax.servlet.ServletOutputStream in project adempiere by adempiere.
the class WebUtil method streamFile.
// streamAttachment
/**
* Stream File
* @param response response
* @param file file to stream
* @return error message or null
*/
public static String streamFile(HttpServletResponse response, File file) {
if (file == null)
return "No File";
if (!file.exists())
return "File not found: " + file.getAbsolutePath();
MimeType mimeType = MimeType.get(file.getAbsolutePath());
// Stream File
try {
// 2k Buffer
int bufferSize = 2048;
int fileLength = (int) file.length();
//
response.setContentType(mimeType.getMimeType());
response.setBufferSize(bufferSize);
response.setContentLength(fileLength);
//
log.fine(file.toString());
// timer start
long time = System.currentTimeMillis();
// Get Data
FileInputStream in = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
int c = 0;
while ((c = in.read()) != -1) out.write(c);
//
out.flush();
out.close();
in.close();
//
time = System.currentTimeMillis() - time;
double speed = (fileLength / 1024) / ((double) time / 1000);
log.info("Length=" + fileLength + " - " + time + " ms - " + speed + " kB/sec - " + mimeType);
} catch (IOException ex) {
log.log(Level.SEVERE, ex.toString());
return "Streaming error - " + ex;
}
return null;
}
use of javax.servlet.ServletOutputStream in project adempiere by adempiere.
the class MobileUtil method streamAttachment.
// getClosePopupButton
/**
* Stream Attachment Entry
* @param response response
* @param attachment attachment
* @param attachmentIndex logical index
* @return error message or null
*/
public static String streamAttachment(HttpServletResponse response, MAttachment attachment, int attachmentIndex) {
if (attachment == null)
return "No Attachment";
int realIndex = -1;
MAttachmentEntry[] entries = attachment.getEntries();
for (int i = 0; i < entries.length; i++) {
MAttachmentEntry entry = entries[i];
if (entry.getIndex() == attachmentIndex) {
realIndex = i;
break;
}
}
if (realIndex < 0) {
log.fine("No Attachment Entry for Index=" + attachmentIndex + " - " + attachment);
return "Attachment Entry not found";
}
MAttachmentEntry entry = entries[realIndex];
if (entry.getData() == null) {
log.fine("Empty Attachment Entry for Index=" + attachmentIndex + " - " + attachment);
return "Attachment Entry empty";
}
// Stream Attachment Entry
try {
// 2k Buffer
int bufferSize = 2048;
int fileLength = entry.getData().length;
//
response.setContentType(entry.getContentType());
response.setBufferSize(bufferSize);
response.setContentLength(fileLength);
//
log.fine(entry.toString());
// timer start
long time = System.currentTimeMillis();
//
ServletOutputStream out = response.getOutputStream();
out.write(entry.getData());
out.flush();
out.close();
//
time = System.currentTimeMillis() - time;
double speed = (fileLength / 1024) / ((double) time / 1000);
log.info("Length=" + fileLength + " - " + time + " ms - " + speed + " kB/sec - " + entry.getContentType());
} catch (IOException ex) {
log.log(Level.SEVERE, ex.toString());
return "Streaming error - " + ex;
}
return null;
}
use of javax.servlet.ServletOutputStream in project jackrabbit-oak by apache.
the class GetLastRevisionHandler method handle.
@Override
public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
RemoteSession session = (RemoteSession) request.getAttribute("session");
if (session == null) {
sendInternalServerError(response, "session not found");
return;
}
RemoteRevision revision = session.readLastRevision();
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/json");
ServletOutputStream stream = response.getOutputStream();
JsonGenerator generator = new JsonFactory().createJsonGenerator(stream, JsonEncoding.UTF8);
generator.writeStartObject();
generator.writeStringField("revision", revision.asString());
generator.writeEndObject();
generator.flush();
stream.close();
}
Aggregations