Search in sources :

Example 51 with ServletOutputStream

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();
    }
}
Also used : User(com.alibaba.dubbo.registry.common.domain.User) ServletOutputStream(javax.servlet.ServletOutputStream)

Example 52 with ServletOutputStream

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;
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) MAttachmentEntry(org.compiere.model.MAttachmentEntry) IOException(java.io.IOException)

Example 53 with ServletOutputStream

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;
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 54 with ServletOutputStream

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;
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) MAttachmentEntry(org.compiere.model.MAttachmentEntry) IOException(java.io.IOException)

Example 55 with ServletOutputStream

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();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) RemoteSession(org.apache.jackrabbit.oak.remote.RemoteSession) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) RemoteRevision(org.apache.jackrabbit.oak.remote.RemoteRevision)

Aggregations

ServletOutputStream (javax.servlet.ServletOutputStream)509 IOException (java.io.IOException)212 HttpServletResponse (javax.servlet.http.HttpServletResponse)147 Test (org.junit.Test)112 HttpServletRequest (javax.servlet.http.HttpServletRequest)109 ServletException (javax.servlet.ServletException)91 InputStream (java.io.InputStream)62 File (java.io.File)57 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 FileInputStream (java.io.FileInputStream)40 CountDownLatch (java.util.concurrent.CountDownLatch)27 WriteListener (javax.servlet.WriteListener)27 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)27 PrintWriter (java.io.PrintWriter)26 HttpServlet (javax.servlet.http.HttpServlet)25 AsyncContext (javax.servlet.AsyncContext)23 ServletInputStream (javax.servlet.ServletInputStream)22 ArrayList (java.util.ArrayList)21 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)20 Date (java.util.Date)18