Search in sources :

Example 6 with JSONWriter

use of org.apache.felix.utils.json.JSONWriter in project sling by apache.

the class ValidationPostResponse method doSend.

@Override
protected void doSend(HttpServletResponse response) throws IOException {
    response.setContentType("application/json");
    PrintWriter printWriter = response.getWriter();
    JSONWriter writer = new JSONWriter(printWriter);
    writer.object();
    boolean validationError = false;
    if (validationResult != null) {
        try {
            writer.key("valid").value(validationResult.isValid());
            writer.key("failures").array();
            for (ValidationFailure failure : validationResult.getFailures()) {
                writer.object();
                writer.key("message").value(failure.getMessage(resourceBundle));
                writer.key("location").value(failure.getLocation());
                writer.key("severity").value(failure.getSeverity());
                writer.endObject();
            }
            writer.endArray();
        } catch (IOException e) {
            LOG.error("JSON error during response send operation.", e);
        }
    } else {
        validationError = true;
    }
    writer.endObject();
    if (validationError) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) ValidationFailure(org.apache.sling.validation.ValidationFailure)

Example 7 with JSONWriter

use of org.apache.felix.utils.json.JSONWriter in project sling by apache.

the class TracerLogServlet method renderContent.

//~-----------------------------------------------< WebConsole Plugin >
@Override
protected void renderContent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (isHtmlRequest(request)) {
        PrintWriter pw = response.getWriter();
        renderStatus(pw);
        renderRequests(pw);
    } else {
        String requestId = getRequestId(request);
        prepareJSONResponse(response);
        try {
            boolean responseDone = false;
            if (requestId != null) {
                JSONRecording recording = cache.getIfPresent(requestId);
                if (recording != null) {
                    boolean shouldGZip = prepareForGZipResponse(request, response);
                    responseDone = recording.render(response.getOutputStream(), shouldGZip);
                }
            }
            if (!responseDone) {
                PrintWriter pw = response.getWriter();
                JSONWriter jw = new JSONWriter(pw);
                jw.object();
                jw.key("error").value("Not found");
                jw.endObject();
            }
        } catch (IOException e) {
            throw new ServletException(e);
        }
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 8 with JSONWriter

use of org.apache.felix.utils.json.JSONWriter in project sling by apache.

the class JSONRecording method toJSON.

private byte[] toJSON() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStream os = baos;
    if (compress) {
        os = new GZIPOutputStream(os);
    }
    OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
    JSONWriter jw = new JSONWriter(osw);
    jw.object();
    jw.key("method").value(method);
    timeTaken = System.currentTimeMillis() - start;
    jw.key("time").value(timeTaken);
    jw.key("timestamp").value(start);
    addRequestProgressLogs(jw);
    queryCollector.done();
    addJson(jw, "queries", queries);
    addJson(jw, "logs", logs);
    addLoggerNames(jw);
    jw.endObject();
    osw.flush();
    os.close();
    return baos.toByteArray();
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 9 with JSONWriter

use of org.apache.felix.utils.json.JSONWriter in project sling by apache.

the class LogTailerWebConsolePlugin method renderContent.

@Override
protected void renderContent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (isAjaxRequest(request)) {
        parseCommand(request, response);
        RandomAccessFile randomAccessFile = null;
        try {
            try {
                randomAccessFile = new RandomAccessFile(errLog, "r");
                log.debug("Tailing file " + fileName + " of length " + randomAccessFile.length());
            } catch (Exception e) {
                log.error("Error reading " + fileName, e);
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return;
            }
            JSONWriter json = new JSONWriter(response.getWriter());
            json.object();
            boolean reverse = false;
            //long created = getCreatedTimestampFromCookie(request);
            /*long modified = getModifiedTimestampFromCookie(request);
            if(errLog.lastModified() == modified) {
                json.endObject();
                return;
            }
            else {
                 persistCookie(response, MODIFIED_COOKIE, String.valueOf(errLog.lastModified()));
            }*/
            long pos = getPositionFromCookie(request);
            if (pos < 0) {
                pos = randomAccessFile.length() - 1;
                reverse = true;
            } else if (pos > randomAccessFile.length()) {
                //file rotated
                pos = 0;
            }
            LogFilter[] query = getQueryFromCookie(request);
            if (reverse) {
                randomAccessFile.seek(pos);
                if (randomAccessFile.read() == '\n') {
                    pos--;
                    randomAccessFile.seek(pos);
                    if (randomAccessFile.read() == '\r') {
                        pos--;
                    }
                }
                json.key("content").array();
                int found = 0;
                StringBuilder sb = new StringBuilder();
                String line = null;
                List<String> lines = new ArrayList<String>();
                while (found != LINES_TO_TAIL && pos > 0) {
                    boolean eol = false;
                    randomAccessFile.seek(pos);
                    int c = randomAccessFile.read();
                    if (c == '\n') {
                        found++;
                        sb = sb.reverse();
                        line = sb.toString();
                        sb = new StringBuilder();
                        eol = true;
                        pos--;
                        if (pos > 0) {
                            randomAccessFile.seek(pos);
                            if (randomAccessFile.read() == '\r') {
                                pos--;
                            }
                        }
                    } else {
                        sb.append((char) c);
                        pos--;
                    }
                    if (eol) {
                        if (filter(line, query)) {
                            lines.add(line);
                        }
                    }
                }
                if (pos < 0) {
                    if (filter(line, query)) {
                        lines.add(line);
                    }
                }
                for (int i = lines.size() - 1; i > -1; i--) {
                    json.object().key("line").value(lines.get(i)).endObject();
                }
                json.endArray();
                json.endObject();
            } else {
                randomAccessFile.seek(pos);
                String line = null;
                int lineCount = 0;
                json.key("content").array();
                boolean read = true;
                while (read) {
                    StringBuilder input = new StringBuilder();
                    int c = -1;
                    boolean eol = false;
                    while (!eol) {
                        switch(c = randomAccessFile.read()) {
                            case -1:
                            case '\n':
                                eol = true;
                                break;
                            case '\r':
                                eol = true;
                                long cur = randomAccessFile.getFilePointer();
                                if ((randomAccessFile.read()) != '\n') {
                                    randomAccessFile.seek(cur);
                                }
                                break;
                            default:
                                input.append((char) c);
                                break;
                        }
                    }
                    if ((c == -1) && (input.length() == 0)) {
                        read = false;
                        continue;
                    }
                    line = input.toString();
                    lineCount++;
                    if (lineCount == LINES_TO_TAIL) {
                        read = false;
                    }
                    pos = randomAccessFile.getFilePointer();
                    if (filter(line, query)) {
                        json.object().key("line").value(line).endObject();
                    }
                }
                json.endArray();
                json.endObject();
            }
            persistCookie(response, POSITION_COOKIE, String.valueOf(randomAccessFile.getFilePointer()));
        } catch (Exception e) {
            log.error("Error tailing " + fileName, e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            try {
                if (randomAccessFile != null) {
                    randomAccessFile.close();
                }
            } catch (Exception e) {
                log.error("Error closing " + fileName, e);
            }
        }
    } else {
        PrintWriter printWriter = response.getWriter();
        printWriter.println("<script type=\"text/javascript\" src=\"/libs/sling/logtail-plugin/js/tail.js\"></script>");
        printWriter.println("<link href=\"/libs/sling/logtail-plugin/css/tail.css\" rel=\"stylesheet\" type=\"text/css\"></link>");
        printWriter.println("<div class=\"header-cont\">");
        printWriter.println("   <div class=\"header\" style=\"display:none;\">");
        printWriter.println("       <table>");
        printWriter.println("           <tr>");
        printWriter.println("               <td><button class=\"numbering\" title=\"Show Line Numbers\" data-numbers=\"false\">Show Line No.</button></td>");
        printWriter.println("               <td><button class=\"pause\" title=\"Pause\">Pause</button></td>");
        printWriter.println("               <td class=\"longer\"><label>Sync frequency(msec)</label>");
        printWriter.println("                   <button class=\"faster\" title=\"Sync Faster\">-</button>");
        printWriter.println("                   <input id=\"speed\" type=\"text\" value=\"3000\"/>");
        printWriter.println("                   <button class=\"slower\" title=\"Sync Slower\">+</button></td>");
        printWriter.println("               <td><button class=\"tail\" title=\"Unfollow Tail\" data-following=\"true\">Unfollow</button></td>");
        printWriter.println("               <td><button class=\"highlighting\" title=\"Highlight\">Highlight</button></td>");
        printWriter.println("               <td><button class=\"clear\" title=\"Clear Display\">Clear</button></td>");
        printWriter.println("               <td class=\"longer\"><input id=\"filter\" type=\"text\"/><span class=\"filterClear ui-icon ui-icon-close\" title=\"Clear Filter\">&nbsp;</span><button class=\"filter\" title=\"Filter Logs\">Filter</button></td>");
        printWriter.println("               <td><button class=\"refresh\" title=\"Reload Logs\">Reload</button></td>");
        printWriter.println("               <td><button class=\"sizeplus\" title=\"Bigger\">a->A</button></td>");
        printWriter.println("               <td><button class=\"sizeminus\" title=\"Smaller\">A->a</button></td>");
        printWriter.println("               <td><button class=\"top\" title=\"Scroll to Top\">Top</button></td>");
        printWriter.println("               <td><button class=\"bottom\" title=\"Scroll to Bottom\">Bottom</button></td>");
        printWriter.println("           </tr>");
        printWriter.println("           <tr>");
        printWriter.println("               <td class=\"loadingstatus\" colspan=\"2\" data-status=\"inactive\"><ul><li></li></ul></td>");
        printWriter.println("               <td>Tailing &nbsp; <select id=\"logfiles\">" + getOptions() + "</select></td>");
        printWriter.println("           </tr>");
        printWriter.println("       </table>");
        printWriter.println("   </div>");
        printWriter.println("   <div class=\"pulldown\" title=\"Click to show options\">&nbsp;==&nbsp;</div>");
        printWriter.println("</div>");
        printWriter.println("");
        printWriter.println("   <div class=\"content\">");
        printWriter.println("");
        printWriter.println("       <div id=\"logarea\"></div>");
        printWriter.println("");
        printWriter.println("   </div>");
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) LogFilter(org.apache.sling.tail.LogFilter) PrintWriter(java.io.PrintWriter)

Example 10 with JSONWriter

use of org.apache.felix.utils.json.JSONWriter in project sling by apache.

the class RepositoryDescriptorsServlet method doGet.

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    try {
        final JSONWriter w = new JSONWriter(response.getWriter());
        w.object();
        w.key("descriptors");
        w.object();
        for (String key : repository.getDescriptorKeys()) {
            w.key(key).value(repository.getDescriptor(key));
        }
        w.endObject();
        w.endObject();
        w.flush();
    } catch (IOException je) {
        throw (IOException) new IOException("JSONException in doGet").initCause(je);
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) IOException(java.io.IOException)

Aggregations

JSONWriter (org.apache.felix.utils.json.JSONWriter)17 IOException (java.io.IOException)10 ServletException (javax.servlet.ServletException)6 PrintWriter (java.io.PrintWriter)4 Map (java.util.Map)2 ScriptEngineFactory (javax.script.ScriptEngineFactory)2 ValidationFailure (org.apache.sling.validation.ValidationFailure)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 RandomAccessFile (java.io.RandomAccessFile)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1