Example usage for java.io StringWriter flush

List of usage examples for java.io StringWriter flush

Introduction

In this page you can find the example usage for java.io StringWriter flush.

Prototype

public void flush() 

Source Link

Document

Flush the stream.

Usage

From source file:com.bluexml.side.integration.buildHudson.utils.Utils.java

/**
 * Mthode qui retourne le contenu du fichier pass en paramtre
 * /*from w w  w  .  j a v a2s. com*/
 * @param f
 *            Le fichier a retourner
 * @return Le contenu du fichier
 */
private static String loadFile(File f) {
    StringWriter out = null;
    try {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
        out = new StringWriter();
        int b;
        while ((b = in.read()) != -1)
            out.write(b);
        out.flush();
        out.close();
        in.close();
        return out.toString();
    } catch (IOException ie) {
        ie.printStackTrace();
    }
    return out.toString();
}

From source file:org.multicore_association.measure.cycle.generate.CodeGen.java

/**
 * Source generating process.//w  ww  .  jav  a2 s.  c o m
 * This function searches for the setting information parallel
 * with a combination of Architecture and InstructionSetName,
 * and generates source cord.
 * @param archName Architecture configuration name
 * @param instName CommonInstructionSet configuration name
 * @param funcName made measurement function name
 * @param fileName generated file name
 * @return
 */
private boolean dumpCSource(String archName, String instName, String funcName, String fileName) {

    ArchConfig archConf = confLoader.searchArchitectureConfig(archName);
    if (archConf == null) {
        System.err.println("Error: archtecture configuration file is not found" + " (" + archName + ")");
        return false;
    }
    InstSetConfig instConf = confLoader.searchInstructionSetConfig(instName);
    if (instConf == null) {
        System.err.println("Error: instruction set configuration file is not found" + " (" + instName + ")");
        return false;
    }

    /* limited by the contents of the SHIM */
    Set<String> shimInstSet = shiml.getInstSet(archName, instName);
    List<Operation> confOpList = instConf.getOperationList();
    List<Operation> removeOpList = new ArrayList<Operation>();
    for (Iterator<Operation> i = confOpList.iterator(); i.hasNext();) {
        Operation op = i.next();
        if (!shimInstSet.contains(op.getName())) {
            removeOpList.add(op);
        }
    }

    if (removeOpList.size() > 0) {
        confOpList.removeAll(removeOpList);
    }

    //      for (Iterator<Operation> i = confOpList.iterator(); i.hasNext();) {
    //         System.out.println(i.next().getName());
    //      }

    /* CALL/RET of the output decision */
    OutputControl outputControl = new OutputControl();
    outputControl.setMeasureCallOp(true);
    outputControl.setMeasureRetOp(true);

    Set<String> shimOpList = shiml.getInstSet(archName, instName);

    if (!shimOpList.contains(CALL_OPERATION)) {
        outputControl.setMeasureCallOp(false);
    }

    if (!shimOpList.contains(RETURN_OPERATION)) {
        outputControl.setMeasureRetOp(false);
    }

    Properties p = new Properties();
    p.setProperty("resource.loader", "class");
    p.setProperty("class.resource.loader.class",
            "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    p.setProperty("input.encoding", "UTF-8");

    Velocity.init(p);
    Velocity.setProperty("file.resource.loader.path", "/");
    VelocityContext context = new VelocityContext();
    context.put(VLT_KEY_INST_SET_CONFIG, instConf);
    context.put(VLT_KEY_ARCH_CONFIG, archConf);
    context.put(VLT_KEY_OUTPUT_CONTROL, outputControl);
    context.put(VLT_KEY_TIMESTAMP, timestamp);
    context.put(VLT_KEY_FUNC_NAME, funcName);
    context.put(VLT_KEY_FILE_NAME, fileName);

    StringWriter writer = new StringWriter();

    Template template = Velocity.getTemplate(VLT_SOURCE_TEMPLATE_NAME);

    template.merge(context, writer);

    File file;
    if (param.getDestDir() != null && !param.getDestDir().equals("")) {
        file = new File(param.getDestDir(), fileName);
    } else {
        file = new File(fileName);
    }

    OutputStream os = null;
    Writer wr = null;
    BufferedWriter bwr = null;
    try {
        os = new FileOutputStream(file);
        wr = new OutputStreamWriter(os, "UTF-8");
        bwr = new BufferedWriter(wr);
        bwr.write(writer.toString());
        bwr.flush();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (bwr != null)
            try {
                bwr.close();
            } catch (IOException e) {
            }
        if (wr != null)
            try {
                wr.close();
            } catch (IOException e) {
            }
        if (os != null)
            try {
                os.close();
            } catch (IOException e) {
            }
    }

    writer.flush();

    return true;
}

From source file:org.openmrs.module.eidinterface.web.controller.PatientStatusController.java

@ResponseBody
@RequestMapping(value = "module/eidinterface/getPatientStatus", method = RequestMethod.POST)
public String getPatientStatus(HttpServletResponse response, @RequestBody String identifiers)
        throws IOException {

    StringWriter sw = new StringWriter();
    CSVWriter csv = new CSVWriter(sw);

    String[] header = { "Identifier", "Status", "CCC Number" };
    csv.writeNext(header);//from   ww w. j av  a2  s .  co  m

    StringReader sr = new StringReader(identifiers);
    Scanner s = new Scanner(sr);

    // iterate through identifiers
    while (s.hasNext()) {

        String identifier = s.next().trim();

        String status;
        String ccc = "";

        String[] parts = identifier.split("-");
        String validIdentifier = new LuhnIdentifierValidator().getValidIdentifier(parts[0]);

        if (!OpenmrsUtil.nullSafeEquals(identifier, validIdentifier)) {
            status = "INVALID IDENTIFIER";
        } else {
            List<Patient> patients = Context.getPatientService().getPatients(null, identifier, null, true);
            if (patients != null && patients.size() == 1) {
                Patient p = patients.get(0);
                PatientIdentifier pi = p.getPatientIdentifier(CCC_NUMBER_PATIENT_IDENTIFIER_ID);
                if (pi != null) {
                    status = "ENROLLED";
                    ccc = pi.getIdentifier();
                } else {
                    status = "NOT ENROLLED";
                }
            } else if (patients != null && patients.size() > 1) {
                status = "MULTIPLE FOUND";
            } else {
                status = "NOT FOUND";
            }
        }

        csv.writeNext(new String[] { identifier, status, ccc });
    }

    // flush the string writer
    sw.flush();

    // set the information
    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");

    // respond with it
    return sw.toString();
}

From source file:org.olat.modules.vitero.manager.ViteroManager.java

private final String serializeViteroBooking(ViteroBooking booking) {
    StringWriter writer = new StringWriter();
    xStream.marshal(booking, new CompactWriter(writer));
    writer.flush();
    return writer.toString();
}

From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java

public XmlElement getXmlElement(final StartElement start, final XMLEventReader reader) throws Exception {

    final XmlElement res = new XmlElement();
    res.setStart(start);//  w  ww  . j a v  a  2  s. com

    StringWriter content = new StringWriter();

    int depth = 1;

    while (reader.hasNext() && depth > 0) {
        final XMLEvent event = reader.nextEvent();

        if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
            depth++;
        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) {
            depth--;
        }

        if (depth == 0) {
            res.setEnd(event.asEndElement());
        } else {
            event.writeAsEncodedUnicode(content);
        }
    }

    content.flush();
    content.close();

    res.setContent(new ByteArrayInputStream(content.toString().getBytes()));

    return res;
}

From source file:com.legstar.cob2xsd.Cob2Xsd.java

/**
 * Serialize the XML Schema to a string.
 * <p/>/*from  w  w w.ja va  2  s  .c  o m*/
 * If we are provided with an XSLT customization file then we transform the
 * XMLSchema.
 * 
 * @param xsd the XML Schema before customization
 * @return a string serialization of the customized XML Schema
 * @throws XsdGenerationException if customization fails
 */
public String xsdToString(final XmlSchema xsd) throws XsdGenerationException {

    if (_log.isDebugEnabled()) {
        StringWriter writer = new StringWriter();
        xsd.write(writer);
        debug("6. Writing XML Schema: ", writer.toString());
    }

    String errorMessage = "Customizing XML Schema failed.";
    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        try {
            tFactory.setAttribute("indent-number", "4");
        } catch (IllegalArgumentException e) {
            _log.debug("Unable to set indent-number on transfomer factory", e);
        }
        StringWriter writer = new StringWriter();
        Source source = new DOMSource(xsd.getAllSchemas()[0]);
        Result result = new StreamResult(writer);
        Transformer transformer;
        String xsltFileName = getModel().getCustomXsltFileName();
        if (xsltFileName == null || xsltFileName.trim().length() == 0) {
            transformer = tFactory.newTransformer();
        } else {
            Source xsltSource = new StreamSource(new File(xsltFileName));
            transformer = tFactory.newTransformer(xsltSource);
        }
        transformer.setOutputProperty(OutputKeys.ENCODING, getModel().getXsdEncoding());
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "no");

        transformer.transform(source, result);
        writer.flush();

        return writer.toString();
    } catch (TransformerConfigurationException e) {
        _log.error(errorMessage, e);
        throw new XsdGenerationException(e);
    } catch (TransformerFactoryConfigurationError e) {
        _log.error(errorMessage, e);
        throw new XsdGenerationException(e);
    } catch (TransformerException e) {
        _log.error(errorMessage, e);
        throw new XsdGenerationException(e);
    }
}

From source file:cn.sinobest.jzpt.framework.utils.string.StringUtils.java

/**
 * String/*from   w w  w. ja  va  2  s.c o m*/
 *
 * @param cr
 *            
 * @param e
 *            
 * @param pkgStart
 *            
 * @return
 */
public static String exceptionStack(final String cr, Throwable e, final String... pkgStart) {
    StringWriter w = new StringWriter();
    e.printStackTrace(new PrintWriter(w) {
        @Override
        public void println() {
        }

        @Override
        public void write(String x) {
            x = rtrim(x, '\r', '\n', '\t');
            if (x.length() == 0) {
                return;
            }
            if (pkgStart.length == 0) {
                super.write(x, 0, x.length());
                super.write(cr, 0, cr.length());
                return;
            }
            String y = x.trim();
            if (!y.startsWith("at ")) {
                super.write(x, 0, x.length());
                super.write(cr, 0, cr.length());
                return;
            }
            for (String s : pkgStart) {
                if (matchChars(y, 3, s)) {
                    super.write(x, 0, x.length());
                    super.write(cr, 0, cr.length());
                    return;
                }
            }
        }
    });
    w.flush();
    IOUtils.closeQuietly(w);
    return w.getBuffer().toString();
}

From source file:com.aurel.track.user.ResetPasswordAction.java

private boolean sendEmail(TPersonBean personBean) {
    boolean emailSent = false;
    StringWriter w = new StringWriter();
    Template template = null;/*from www.  j a va  2  s. c  o m*/
    Map<String, String> root = new HashMap<String, String>();

    TMailTemplateDefBean mailTemplateDefBean = MailTemplateBL
            .getSystemMailTemplateDefBean(IEventSubscriber.EVENT_POST_USER_FORGOTPASSWORD, personBean);

    String subject;
    if (mailTemplateDefBean != null) {
        String templateStr = mailTemplateDefBean.getMailBody();
        subject = mailTemplateDefBean.getMailSubject();
        try {
            template = new Template("name", new StringReader(templateStr), new Configuration());
        } catch (IOException e) {
            LOGGER.debug("Loading the template failed with " + e.getMessage(), e);
        }
    } else {
        LOGGER.error("Can't get mail template for forget password");
        return false;
    }

    if (template == null) {
        LOGGER.error("No valid template found for registration e-mail (maybe compilation errors).");
        return false;
    }

    TSiteBean siteBean = ApplicationBean.getInstance().getSiteBean();
    //The path starts with a "/" character but does not end with a "/"
    String contextPath = ApplicationBean.getInstance().getServletContext().getContextPath();
    String siteURL = siteBean.getServerURL();
    if (siteURL == null) {
        siteURL = "";
    } else if (siteURL.endsWith("/")) {
        siteURL = siteURL.substring(0, siteURL.lastIndexOf("/"));
    }
    String confirmURL = siteURL + contextPath + "/resetPassword!confirm.action?ctk="
            + personBean.getForgotPasswordKey();
    String serverURL = siteURL + contextPath;

    root.put("loginname", personBean.getUsername());
    root.put("firstname", personBean.getFirstName());
    root.put("lastname", personBean.getLastName());
    root.put("serverurl", serverURL);
    root.put("confirmUrl", confirmURL);
    root.put("ldap", new Boolean(personBean.isLdapUser()).toString());

    try {
        template.process(root, w);
    } catch (Exception e) {
        LOGGER.error(
                "Processing registration template " + template.getName() + " failed with " + e.getMessage());
    }
    w.flush();
    String messageBody = w.toString();

    try {
        // Send mail to newly registered user
        MailSender ms = new MailSender(
                new InternetAddress(siteBean.getTrackEmail(), siteBean.getEmailPersonalName()),
                new InternetAddress(personBean.getEmail(), personBean.getFullName()), subject, messageBody,
                mailTemplateDefBean.isPlainEmail());
        emailSent = ms.send(); // wait for sending email
        LOGGER.debug("emailSend:" + emailSent);

        EventPublisher evp = EventPublisher.getInstance();
        if (evp != null) {
            List<Integer> events = new LinkedList<Integer>();
            events.add(Integer.valueOf(IEventSubscriber.EVENT_POST_USER_FORGOTPASSWORD));
            evp.notify(events, personBean);
        }
    } catch (Exception t) {
        LOGGER.error("SendMail reset password", t);
    }
    return emailSent;
}

From source file:io.warp10.continuum.egress.EgressFetchHandler.java

@Override
public void handle(String target, Request baseRequest, HttpServletRequest req, HttpServletResponse resp)
        throws IOException, ServletException {
    boolean fromArchive = false;
    boolean splitFetch = false;
    boolean writeTimestamp = false;

    if (Constants.API_ENDPOINT_FETCH.equals(target)) {
        baseRequest.setHandled(true);//from   w  ww.j av a  2s.c  o  m
        fromArchive = false;
    } else if (Constants.API_ENDPOINT_AFETCH.equals(target)) {
        baseRequest.setHandled(true);
        fromArchive = true;
    } else if (Constants.API_ENDPOINT_SFETCH.equals(target)) {
        baseRequest.setHandled(true);
        splitFetch = true;
    } else if (Constants.API_ENDPOINT_CHECK.equals(target)) {
        baseRequest.setHandled(true);
        resp.setStatus(HttpServletResponse.SC_OK);
        return;
    } else {
        return;
    }

    try {
        // Labels for Sensision
        Map<String, String> labels = new HashMap<String, String>();

        labels.put(SensisionConstants.SENSISION_LABEL_TYPE, target);

        //
        // Add CORS header
        //

        resp.setHeader("Access-Control-Allow-Origin", "*");

        String start = null;
        String stop = null;

        long now = Long.MIN_VALUE;
        long timespan = 0L;

        String nowParam = null;
        String timespanParam = null;
        String dedupParam = null;
        String showErrorsParam = null;

        if (splitFetch) {
            nowParam = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_NOW_HEADERX));
            timespanParam = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_TIMESPAN_HEADERX));
            showErrorsParam = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_SHOW_ERRORS_HEADERX));
        } else {
            start = req.getParameter(Constants.HTTP_PARAM_START);
            stop = req.getParameter(Constants.HTTP_PARAM_STOP);

            nowParam = req.getParameter(Constants.HTTP_PARAM_NOW);
            timespanParam = req.getParameter(Constants.HTTP_PARAM_TIMESPAN);
            dedupParam = req.getParameter(Constants.HTTP_PARAM_DEDUP);
            showErrorsParam = req.getParameter(Constants.HTTP_PARAM_SHOW_ERRORS);
        }

        String maxDecoderLenParam = req.getParameter(Constants.HTTP_PARAM_MAXSIZE);
        int maxDecoderLen = null != maxDecoderLenParam ? Integer.parseInt(maxDecoderLenParam)
                : Constants.DEFAULT_PACKED_MAXSIZE;

        String suffix = req.getParameter(Constants.HTTP_PARAM_SUFFIX);
        if (null == suffix) {
            suffix = Constants.DEFAULT_PACKED_CLASS_SUFFIX;
        }

        boolean unpack = null != req.getParameter(Constants.HTTP_PARAM_UNPACK);

        long chunksize = Long.MAX_VALUE;

        if (null != req.getParameter(Constants.HTTP_PARAM_CHUNKSIZE)) {
            chunksize = Long.parseLong(req.getParameter(Constants.HTTP_PARAM_CHUNKSIZE));
        }

        if (chunksize <= 0) {
            throw new IOException("Invalid chunksize.");
        }

        boolean showErrors = null != showErrorsParam;
        boolean dedup = null != dedupParam && "true".equals(dedupParam);

        if (null != start && null != stop) {
            long tsstart = fmt.parseDateTime(start).getMillis() * Constants.TIME_UNITS_PER_MS;
            long tsstop = fmt.parseDateTime(stop).getMillis() * Constants.TIME_UNITS_PER_MS;

            if (tsstart < tsstop) {
                now = tsstop;
                timespan = tsstop - tsstart;
            } else {
                now = tsstart;
                timespan = tsstart - tsstop;
            }
        } else if (null != nowParam && null != timespanParam) {
            if ("now".equals(nowParam)) {
                now = TimeSource.getTime();
            } else {
                try {
                    now = Long.parseLong(nowParam);
                } catch (Exception e) {
                    now = fmt.parseDateTime(nowParam).getMillis() * Constants.TIME_UNITS_PER_MS;
                }
            }

            timespan = Long.parseLong(timespanParam);
        }

        if (Long.MIN_VALUE == now) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Missing now/timespan or start/stop parameters.");
            return;
        }

        String selector = splitFetch ? null : req.getParameter(Constants.HTTP_PARAM_SELECTOR);

        //
        // Extract token from header
        //

        String token = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_TOKENX));

        // If token was not found in header, extract it from the 'token' parameter
        if (null == token && !splitFetch) {
            token = req.getParameter(Constants.HTTP_PARAM_TOKEN);
        }

        String fetchSig = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_FETCH_SIGNATURE));

        //
        // Check token signature if it was provided
        //

        boolean signed = false;

        if (splitFetch) {
            // Force showErrors
            showErrors = true;
            signed = true;
        }

        if (null != fetchSig) {
            if (null != fetchPSK) {
                String[] subelts = fetchSig.split(":");
                if (2 != subelts.length) {
                    throw new IOException("Invalid fetch signature.");
                }
                long nowts = System.currentTimeMillis();
                long sigts = new BigInteger(subelts[0], 16).longValue();
                long sighash = new BigInteger(subelts[1], 16).longValue();

                if (nowts - sigts > 10000L) {
                    throw new IOException("Fetch signature has expired.");
                }

                // Recompute hash of ts:token

                String tstoken = Long.toString(sigts) + ":" + token;

                long checkedhash = SipHashInline.hash24(fetchPSK, tstoken.getBytes(Charsets.ISO_8859_1));

                if (checkedhash != sighash) {
                    throw new IOException("Corrupted fetch signature");
                }

                signed = true;
            } else {
                throw new IOException("Fetch PreSharedKey is not set.");
            }
        }

        ReadToken rtoken = null;

        String format = splitFetch ? "wrapper" : req.getParameter(Constants.HTTP_PARAM_FORMAT);

        if (!splitFetch) {
            try {
                rtoken = Tokens.extractReadToken(token);

                if (rtoken.getHooksSize() > 0) {
                    throw new IOException("Tokens with hooks cannot be used for fetching data.");
                }
            } catch (WarpScriptException ee) {
                throw new IOException(ee);
            }

            if (null == rtoken) {
                resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Missing token.");
                return;
            }
        }

        boolean showAttr = "true".equals(req.getParameter(Constants.HTTP_PARAM_SHOWATTR));

        boolean sortMeta = "true".equals(req.getParameter(Constants.HTTP_PARAM_SORTMETA));

        //
        // Extract the class and labels selectors
        // The class selector and label selectors are supposed to have
        // values which use percent encoding, i.e. explicit percent encoding which
        // might have been re-encoded using percent encoding when passed as parameter
        //
        //

        Set<Metadata> metadatas = new HashSet<Metadata>();
        List<Iterator<Metadata>> iterators = new ArrayList<Iterator<Metadata>>();

        if (!splitFetch) {

            if (null == selector) {
                throw new IOException("Missing '" + Constants.HTTP_PARAM_SELECTOR + "' parameter.");
            }

            String[] selectors = selector.split("\\s+");

            for (String sel : selectors) {
                Matcher m = SELECTOR_RE.matcher(sel);

                if (!m.matches()) {
                    resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
                    return;
                }

                String classSelector = URLDecoder.decode(m.group(1), "UTF-8");
                String labelsSelection = m.group(2);

                Map<String, String> labelsSelectors;

                try {
                    labelsSelectors = GTSHelper.parseLabelsSelectors(labelsSelection);
                } catch (ParseException pe) {
                    throw new IOException(pe);
                }

                //
                // Force 'producer'/'owner'/'app' from token
                //

                labelsSelectors.remove(Constants.PRODUCER_LABEL);
                labelsSelectors.remove(Constants.OWNER_LABEL);
                labelsSelectors.remove(Constants.APPLICATION_LABEL);

                labelsSelectors.putAll(Tokens.labelSelectorsFromReadToken(rtoken));

                List<Metadata> metas = null;

                List<String> clsSels = new ArrayList<String>();
                List<Map<String, String>> lblsSels = new ArrayList<Map<String, String>>();

                clsSels.add(classSelector);
                lblsSels.add(labelsSelectors);

                try {
                    metas = directoryClient.find(clsSels, lblsSels);
                    metadatas.addAll(metas);
                } catch (Exception e) {
                    //
                    // If metadatas is not empty, create an iterator for it, then clear it
                    //
                    if (!metadatas.isEmpty()) {
                        iterators.add(metadatas.iterator());
                        metadatas.clear();
                    }
                    iterators.add(directoryClient.iterator(clsSels, lblsSels));
                }
            }
        } else {
            //
            // Add an iterator which reads splits from the request body
            //

            boolean gzipped = false;

            if (null != req.getHeader("Content-Type")
                    && "application/gzip".equals(req.getHeader("Content-Type"))) {
                gzipped = true;
            }

            BufferedReader br = null;

            if (gzipped) {
                GZIPInputStream is = new GZIPInputStream(req.getInputStream());
                br = new BufferedReader(new InputStreamReader(is));
            } else {
                br = req.getReader();
            }

            final BufferedReader fbr = br;

            MetadataIterator iterator = new MetadataIterator() {

                private List<Metadata> metadatas = new ArrayList<Metadata>();

                private boolean done = false;

                private String lasttoken = "";

                @Override
                public void close() throws Exception {
                    fbr.close();
                }

                @Override
                public Metadata next() {
                    if (!metadatas.isEmpty()) {
                        Metadata meta = metadatas.get(metadatas.size() - 1);
                        metadatas.remove(metadatas.size() - 1);
                        return meta;
                    } else {
                        if (hasNext()) {
                            return next();
                        } else {
                            throw new NoSuchElementException();
                        }
                    }
                }

                @Override
                public boolean hasNext() {
                    if (!metadatas.isEmpty()) {
                        return true;
                    }

                    if (done) {
                        return false;
                    }

                    String line = null;

                    try {
                        line = fbr.readLine();
                    } catch (IOException ioe) {
                        throw new RuntimeException(ioe);
                    }

                    if (null == line) {
                        done = true;
                        return false;
                    }

                    //
                    // Decode/Unwrap/Deserialize the split
                    //

                    byte[] data = OrderPreservingBase64.decode(line.getBytes(Charsets.US_ASCII));
                    if (null != fetchAES) {
                        data = CryptoUtils.unwrap(fetchAES, data);
                    }

                    if (null == data) {
                        throw new RuntimeException("Invalid wrapped content.");
                    }

                    TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());

                    GTSSplit split = new GTSSplit();

                    try {
                        deserializer.deserialize(split, data);
                    } catch (TException te) {
                        throw new RuntimeException(te);
                    }

                    //
                    // Check the expiry
                    //

                    long instant = System.currentTimeMillis();

                    if (instant - split.getTimestamp() > maxSplitAge || instant > split.getExpiry()) {
                        throw new RuntimeException("Split has expired.");
                    }

                    this.metadatas.addAll(split.getMetadatas());

                    // We assume there was at least one metadata instance in the split!!!
                    return true;
                }
            };

            iterators.add(iterator);
        }

        List<Metadata> metas = new ArrayList<Metadata>();
        metas.addAll(metadatas);

        if (!metas.isEmpty()) {
            iterators.add(metas.iterator());
        }

        //
        // Loop over the iterators, storing the read metadata to a temporary file encrypted on disk
        // Data is encrypted using a onetime pad
        //

        final byte[] onetimepad = new byte[(int) Math.min(65537, System.currentTimeMillis() % 100000)];
        new Random().nextBytes(onetimepad);

        final File cache = File.createTempFile(
                Long.toHexString(System.currentTimeMillis()) + "-" + Long.toHexString(System.nanoTime()),
                ".dircache");
        cache.deleteOnExit();

        FileWriter writer = new FileWriter(cache);

        TSerializer serializer = new TSerializer(new TCompactProtocol.Factory());

        int padidx = 0;

        for (Iterator<Metadata> itermeta : iterators) {
            try {
                while (itermeta.hasNext()) {
                    Metadata metadata = itermeta.next();

                    try {
                        byte[] bytes = serializer.serialize(metadata);
                        // Apply onetimepad
                        for (int i = 0; i < bytes.length; i++) {
                            bytes[i] = (byte) (bytes[i] ^ onetimepad[padidx++]);
                            if (padidx >= onetimepad.length) {
                                padidx = 0;
                            }
                        }
                        OrderPreservingBase64.encodeToWriter(bytes, writer);
                        writer.write('\n');
                    } catch (TException te) {
                    }
                }

                if (!itermeta.hasNext() && (itermeta instanceof MetadataIterator)) {
                    try {
                        ((MetadataIterator) itermeta).close();
                    } catch (Exception e) {
                    }
                }
            } catch (Throwable t) {
                throw t;
            } finally {
                if (itermeta instanceof MetadataIterator) {
                    try {
                        ((MetadataIterator) itermeta).close();
                    } catch (Exception e) {
                    }
                }
            }
        }

        writer.close();

        //
        // Create an iterator based on the cache
        //

        MetadataIterator cacheiterator = new MetadataIterator() {

            BufferedReader reader = new BufferedReader(new FileReader(cache));

            private Metadata current = null;
            private boolean done = false;

            private TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());

            int padidx = 0;

            @Override
            public boolean hasNext() {
                if (done) {
                    return false;
                }

                if (null != current) {
                    return true;
                }

                try {
                    String line = reader.readLine();
                    if (null == line) {
                        done = true;
                        return false;
                    }
                    byte[] raw = OrderPreservingBase64.decode(line.getBytes(Charsets.US_ASCII));
                    // Apply one time pad
                    for (int i = 0; i < raw.length; i++) {
                        raw[i] = (byte) (raw[i] ^ onetimepad[padidx++]);
                        if (padidx >= onetimepad.length) {
                            padidx = 0;
                        }
                    }
                    Metadata metadata = new Metadata();
                    try {
                        deserializer.deserialize(metadata, raw);
                        this.current = metadata;
                        return true;
                    } catch (TException te) {
                        LOG.error("", te);
                    }
                } catch (IOException ioe) {
                    LOG.error("", ioe);
                }

                return false;
            }

            @Override
            public Metadata next() {
                if (null != this.current) {
                    Metadata metadata = this.current;
                    this.current = null;
                    return metadata;
                } else {
                    throw new NoSuchElementException();
                }
            }

            @Override
            public void close() throws Exception {
                this.reader.close();
                cache.delete();
            }
        };

        iterators.clear();
        iterators.add(cacheiterator);

        metas = new ArrayList<Metadata>();

        PrintWriter pw = resp.getWriter();

        AtomicReference<Metadata> lastMeta = new AtomicReference<Metadata>(null);
        AtomicLong lastCount = new AtomicLong(0L);

        long fetchtimespan = timespan;

        for (Iterator<Metadata> itermeta : iterators) {
            while (itermeta.hasNext()) {
                metas.add(itermeta.next());

                //
                // Access the data store every 'FETCH_BATCHSIZE' GTS or at the end of each iterator
                //

                if (metas.size() > FETCH_BATCHSIZE || !itermeta.hasNext()) {
                    try (GTSDecoderIterator iterrsc = storeClient.fetch(rtoken, metas, now, fetchtimespan,
                            fromArchive, writeTimestamp)) {
                        GTSDecoderIterator iter = iterrsc;

                        if (unpack) {
                            iter = new UnpackingGTSDecoderIterator(iter, suffix);
                            timespan = Long.MIN_VALUE + 1;
                        }

                        if ("text".equals(format)) {
                            textDump(pw, iter, now, timespan, false, dedup, signed, showAttr, lastMeta,
                                    lastCount, sortMeta);
                        } else if ("fulltext".equals(format)) {
                            textDump(pw, iter, now, timespan, true, dedup, signed, showAttr, lastMeta,
                                    lastCount, sortMeta);
                        } else if ("raw".equals(format)) {
                            rawDump(pw, iter, dedup, signed, timespan, lastMeta, lastCount, sortMeta);
                        } else if ("wrapper".equals(format)) {
                            wrapperDump(pw, iter, dedup, signed, fetchPSK, timespan, lastMeta, lastCount);
                        } else if ("json".equals(format)) {
                            jsonDump(pw, iter, now, timespan, dedup, signed, lastMeta, lastCount);
                        } else if ("tsv".equals(format)) {
                            tsvDump(pw, iter, now, timespan, false, dedup, signed, lastMeta, lastCount,
                                    sortMeta);
                        } else if ("fulltsv".equals(format)) {
                            tsvDump(pw, iter, now, timespan, true, dedup, signed, lastMeta, lastCount,
                                    sortMeta);
                        } else if ("pack".equals(format)) {
                            packedDump(pw, iter, now, timespan, dedup, signed, lastMeta, lastCount,
                                    maxDecoderLen, suffix, chunksize, sortMeta);
                        } else if ("null".equals(format)) {
                            nullDump(iter);
                        } else {
                            textDump(pw, iter, now, timespan, false, dedup, signed, showAttr, lastMeta,
                                    lastCount, sortMeta);
                        }
                    } catch (Throwable t) {
                        LOG.error("", t);
                        Sensision.update(SensisionConstants.CLASS_WARP_FETCH_ERRORS, Sensision.EMPTY_LABELS, 1);
                        if (showErrors) {
                            pw.println();
                            StringWriter sw = new StringWriter();
                            PrintWriter pw2 = new PrintWriter(sw);
                            t.printStackTrace(pw2);
                            pw2.close();
                            sw.flush();
                            String error = URLEncoder.encode(sw.toString(), "UTF-8");
                            pw.println(Constants.EGRESS_FETCH_ERROR_PREFIX + error);
                        }
                        throw new IOException(t);
                    } finally {
                        if (!itermeta.hasNext() && (itermeta instanceof MetadataIterator)) {
                            try {
                                ((MetadataIterator) itermeta).close();
                            } catch (Exception e) {
                            }
                        }
                    }

                    //
                    // Reset 'metas'
                    //

                    metas.clear();
                }
            }

            if (!itermeta.hasNext() && (itermeta instanceof MetadataIterator)) {
                try {
                    ((MetadataIterator) itermeta).close();
                } catch (Exception e) {
                }
            }
        }

        Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_FETCH_REQUESTS, labels, 1);
    } catch (Exception e) {
        if (!resp.isCommitted()) {
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
            return;
        }
    }
}

From source file:org.kepler.objectmanager.ActorMetadata.java

/**
 * try to locate and parse a moml file as a class
 *///from   w  w  w. j av a 2 s. c  o m
protected ComponentEntity parseMoMLFile(String className) throws Exception {
    if (isDebugging)
        log.debug("parseMoMLFile(" + className + ")");

    JarFile jarFile = null;
    InputStream xmlStream = null;
    try {
        // first we need to find the file and read it
        File classFile = searchClasspath(className);
        StringWriter sw = new StringWriter();
        if (classFile.getName().endsWith(".jar")) {
            jarFile = new JarFile(classFile);
            ZipEntry entry = jarFile.getEntry(className.replace('.', '/') + ".xml");
            xmlStream = jarFile.getInputStream(entry);
        } else {
            xmlStream = new FileInputStream(classFile);
        }

        byte[] b = new byte[1024];
        int numread = xmlStream.read(b, 0, 1024);
        while (numread != -1) {
            String s = new String(b, 0, numread);
            sw.write(s);
            numread = xmlStream.read(b, 0, 1024);
        }
        sw.flush();
        // get the moml document
        String xmlDoc = sw.toString();
        sw.close();

        if (isDebugging)
            log.debug("**** MoMLParser ****");

        // use the moml parser to parse the doc
        MoMLParser parser = new MoMLParser();
        parser.reset();

        // System.out.println("processing " + className);
        NamedObj obj = parser.parse(xmlDoc);
        return (ComponentEntity) obj;
    } finally {
        if (jarFile != null) {
            jarFile.close();
        }
        if (xmlStream != null) {
            xmlStream.close();
        }
    }
}