Search in sources :

Example 16 with StringWriter

use of java.io.StringWriter in project flink by apache.

the class AbstractMetricsHandler method getMetricsValues.

private String getMetricsValues(Map<String, String> pathParams, String requestedMetricsList) throws IOException {
    if (requestedMetricsList.isEmpty()) {
        /**
			 * The WebInterface doesn't check whether the list of available metrics was empty. This can lead to a 
			 * request for which the "get" parameter is an empty string.
			 */
        return "";
    }
    MetricStore metricStore = fetcher.getMetricStore();
    synchronized (metricStore) {
        Map<String, String> metrics = getMapFor(pathParams, metricStore);
        if (metrics == null) {
            return "";
        }
        String[] requestedMetrics = requestedMetricsList.split(",");
        StringWriter writer = new StringWriter();
        JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
        gen.writeStartArray();
        for (String requestedMetric : requestedMetrics) {
            Object metricValue = metrics.get(requestedMetric);
            if (metricValue != null) {
                gen.writeStartObject();
                gen.writeStringField("id", requestedMetric);
                gen.writeStringField("value", metricValue.toString());
                gen.writeEndObject();
            }
        }
        gen.writeEndArray();
        gen.close();
        return writer.toString();
    }
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 17 with StringWriter

use of java.io.StringWriter in project flink by apache.

the class JarListHandler method handleJsonRequest.

@Override
public String handleJsonRequest(Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager) throws Exception {
    try {
        StringWriter writer = new StringWriter();
        JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
        gen.writeStartObject();
        gen.writeStringField("address", queryParams.get(RuntimeMonitorHandler.WEB_MONITOR_ADDRESS_KEY));
        gen.writeArrayFieldStart("files");
        File[] list = jarDir.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".jar");
            }
        });
        for (File f : list) {
            // separate the uuid and the name parts.
            String id = f.getName();
            int startIndex = id.indexOf("_");
            if (startIndex < 0) {
                continue;
            }
            String name = id.substring(startIndex + 1);
            if (name.length() < 5 || !name.endsWith(".jar")) {
                continue;
            }
            gen.writeStartObject();
            gen.writeStringField("id", id);
            gen.writeStringField("name", name);
            gen.writeNumberField("uploaded", f.lastModified());
            gen.writeArrayFieldStart("entry");
            String[] classes = new String[0];
            try {
                JarFile jar = new JarFile(f);
                Manifest manifest = jar.getManifest();
                String assemblerClass = null;
                if (manifest != null) {
                    assemblerClass = manifest.getMainAttributes().getValue(PackagedProgram.MANIFEST_ATTRIBUTE_ASSEMBLER_CLASS);
                    if (assemblerClass == null) {
                        assemblerClass = manifest.getMainAttributes().getValue(PackagedProgram.MANIFEST_ATTRIBUTE_MAIN_CLASS);
                    }
                }
                if (assemblerClass != null) {
                    classes = assemblerClass.split(",");
                }
            } catch (IOException ignored) {
            // we simply show no entries here
            }
            // show every entry class that can be loaded later on.
            for (String clazz : classes) {
                clazz = clazz.trim();
                PackagedProgram program = null;
                try {
                    program = new PackagedProgram(f, clazz, new String[0]);
                } catch (Exception ignored) {
                // ignore jar files which throw an error upon creating a PackagedProgram
                }
                if (program != null) {
                    gen.writeStartObject();
                    gen.writeStringField("name", clazz);
                    String desc = program.getDescription();
                    gen.writeStringField("description", desc == null ? "No description provided" : desc);
                    gen.writeEndObject();
                }
            }
            gen.writeEndArray();
            gen.writeEndObject();
        }
        gen.writeEndArray();
        gen.writeEndObject();
        gen.close();
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException("Failed to fetch jar list: " + e.getMessage(), e);
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) IOException(java.io.IOException) FilenameFilter(java.io.FilenameFilter) PackagedProgram(org.apache.flink.client.program.PackagedProgram) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 18 with StringWriter

use of java.io.StringWriter in project flink by apache.

the class JarPlanHandler method handleJsonRequest.

@Override
public String handleJsonRequest(Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager) throws Exception {
    try {
        JarActionHandlerConfig config = JarActionHandlerConfig.fromParams(pathParams, queryParams);
        JobGraph graph = getJobGraphAndClassLoader(config).f0;
        StringWriter writer = new StringWriter();
        JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
        gen.writeStartObject();
        gen.writeFieldName("plan");
        gen.writeRawValue(JsonPlanGenerator.generatePlan(graph));
        gen.writeEndObject();
        gen.close();
        return writer.toString();
    } catch (Exception e) {
        return sendError(e);
    }
}
Also used : JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 19 with StringWriter

use of java.io.StringWriter in project flink by apache.

the class JobAccumulatorsHandler method createJobAccumulatorsJson.

public static String createJobAccumulatorsJson(AccessExecutionGraph graph) throws IOException {
    StringWriter writer = new StringWriter();
    JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
    StringifiedAccumulatorResult[] allAccumulators = graph.getAccumulatorResultsStringified();
    gen.writeStartObject();
    gen.writeArrayFieldStart("job-accumulators");
    // empty for now
    gen.writeEndArray();
    gen.writeArrayFieldStart("user-task-accumulators");
    for (StringifiedAccumulatorResult acc : allAccumulators) {
        gen.writeStartObject();
        gen.writeStringField("name", acc.getName());
        gen.writeStringField("type", acc.getType());
        gen.writeStringField("value", acc.getValue());
        gen.writeEndObject();
    }
    gen.writeEndArray();
    gen.writeEndObject();
    gen.close();
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) StringifiedAccumulatorResult(org.apache.flink.runtime.accumulators.StringifiedAccumulatorResult) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 20 with StringWriter

use of java.io.StringWriter in project flink by apache.

the class JobManagerConfigHandler method handleJsonRequest.

@Override
public String handleJsonRequest(Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager) throws Exception {
    StringWriter writer = new StringWriter();
    JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
    gen.writeStartArray();
    for (String key : config.keySet()) {
        gen.writeStartObject();
        gen.writeStringField("key", key);
        // Mask key values which contain sensitive information
        if (key.toLowerCase().contains("password")) {
            String value = config.getString(key, null);
            if (value != null) {
                value = "******";
            }
            gen.writeStringField("value", value);
        } else {
            gen.writeStringField("value", config.getString(key, null));
        }
        gen.writeEndObject();
    }
    gen.writeEndArray();
    gen.close();
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Aggregations

StringWriter (java.io.StringWriter)3175 PrintWriter (java.io.PrintWriter)1057 Test (org.junit.Test)612 IOException (java.io.IOException)516 StringReader (java.io.StringReader)232 Writer (java.io.Writer)211 StreamResult (javax.xml.transform.stream.StreamResult)207 File (java.io.File)194 InputStreamReader (java.io.InputStreamReader)140 HashMap (java.util.HashMap)136 Transformer (javax.xml.transform.Transformer)125 InputStream (java.io.InputStream)119 Map (java.util.Map)116 ArrayList (java.util.ArrayList)106 DOMSource (javax.xml.transform.dom.DOMSource)99 BufferedReader (java.io.BufferedReader)96 ByteArrayInputStream (java.io.ByteArrayInputStream)84 Reader (java.io.Reader)77 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)75 HttpServletResponse (javax.servlet.http.HttpServletResponse)73