Example usage for java.io Writer close

List of usage examples for java.io Writer close

Introduction

In this page you can find the example usage for java.io Writer close.

Prototype

public abstract void close() throws IOException;

Source Link

Document

Closes the stream, flushing it first.

Usage

From source file:com.marklogic.samplestack.web.security.SamplestackAuthenticationFailureHandler.java

@Override
/**/*w  w w  .j  av  a2s  .c  om*/
 * Override handler that returns 401 after failed authentication.
 */
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {

    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
    responseWrapper.setStatus(HttpStatus.SC_UNAUTHORIZED);

    Writer out = responseWrapper.getWriter();
    errors.writeJsonResponse(out, HttpStatus.SC_UNAUTHORIZED, "Unauthorized");
    out.close();
}

From source file:de.avanux.smartapplianceenabler.Application.java

private void writePidFile() {
    String pidFileName = System.getProperty("sae.pidfile");
    if (pidFileName != null) {
        String name = ManagementFactory.getRuntimeMXBean().getName();
        String pid = name.split("@")[0];
        try {//w  ww.j  a  v a2s  .c  o m
            Writer pidFile = new FileWriter(pidFileName);
            pidFile.write(pid);
            pidFile.close();
            logger.info("PID " + pid + " written to " + pidFileName);
        } catch (IOException e) {
            logger.error("Error writing PID file " + pidFileName, e);
        }
    }
}

From source file:com.legstar.codegen.CodeGenUtil.java

/**
 * Apply a velocity template taken from a code generation make xml.
 * // ww  w  . j a  va2  s .co  m
 * @param generatorName the generator name
 * @param templateName the velocity template to apply
 * @param modelName the model name
 * @param model the model providing data for velocity templates
 * @param parameters additional parameters to pass to template
 * @param targetFile the file to generate
 * @param targetCharsetName the target character set. null is interpreted as
 *            the default encoding
 * @throws CodeGenMakeException if processing fails
 */
public static void processTemplate(final String generatorName, final String templateName,
        final String modelName, final Object model, final Map<String, Object> parameters, final File targetFile,
        final String targetCharsetName) throws CodeGenMakeException {

    if (LOG.isDebugEnabled()) {
        LOG.debug("Processing template");
        LOG.debug("Template name       = " + templateName);
        LOG.debug("Target file         = " + targetFile);
        LOG.debug("Target charset name = " + targetCharsetName);
        if (parameters != null) {
            for (String key : parameters.keySet()) {
                Object value = parameters.get(key);
                LOG.debug("Parameter " + key + " = " + value);
            }
        }
    }
    VelocityContext context = CodeGenUtil.getContext(generatorName);
    context.put(modelName, model);
    context.put("serialVersionID", Long.toString(mRandom.nextLong()) + 'L');
    if (parameters != null) {
        for (String key : parameters.keySet()) {
            context.put(key, parameters.get(key));
        }
    }
    StringWriter w = new StringWriter();

    try {
        Velocity.mergeTemplate(templateName, "UTF-8", context, w);
        Writer out = null;
        try {
            FileOutputStream fos = new FileOutputStream(targetFile);
            OutputStreamWriter osw;
            if (targetCharsetName == null) {
                osw = new OutputStreamWriter(fos);
            } else {
                osw = new OutputStreamWriter(fos, targetCharsetName);
            }
            out = new BufferedWriter(osw);
            out.write(w.toString());
        } catch (IOException e) {
            throw new CodeGenMakeException(e);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    } catch (ResourceNotFoundException e) {
        throw new CodeGenMakeException(e);
    } catch (ParseErrorException e) {
        throw new CodeGenMakeException(e);
    } catch (MethodInvocationException e) {
        throw new CodeGenMakeException(e);
    } catch (Exception e) {
        throw new CodeGenMakeException(e);
    }
}

From source file:com.careerly.common.support.resolver.ControllerExceptionResolver.java

/**
 * @param request/*from www. ja v a 2 s  . c  o  m*/
 * @param response
 * @param handler
 * @return
 */
private void resolveDataException(HttpServletRequest request, HttpServletResponse response, Object handler,
        StandardJsonObject errorJsonObject) {
    try {
        String data = null;
        String callbackValue = request.getParameter("callback");
        if (StringUtils.isNotBlank(callbackValue)) {
            StringBuilder builder = new StringBuilder();
            builder.append(StringEscapeUtils.escapeHtml(callbackValue)).append("(");
            builder.append(JsonUtils.marshalToString(errorJsonObject));
            builder.append(")");
            data = builder.toString();
        } else {
            data = JsonUtils.marshalToString(errorJsonObject);
        }

        response.setContentType("application/json;charset=UTF-8");
        Writer writer = response.getWriter();
        writer.write(data);
        writer.close();
    } catch (IOException e) {
        ControllerExceptionResolver.logger.error("ERROR ## write message happened error, the trace ", e);
    }
}

From source file:org.orderofthebee.addons.support.tools.repo.LogFileDelete.java

/**
 *
 * {@inheritDoc}/*w  w  w .  jav a2  s .c  o m*/
 */
@Override
public void execute(final WebScriptRequest req, final WebScriptResponse res) throws IOException {
    final String servicePath = req.getServicePath();
    final String matchPath = req.getServiceMatch().getPath();
    final String filePath = servicePath.substring(servicePath.indexOf(matchPath) + matchPath.length());

    final File file = this.validateFilePath(filePath);
    if (file.delete()) {
        res.setStatus(Status.STATUS_OK);
        // we "have" to send a dummy JSON as repository admin console client JS always tries to parse
        final Writer writer = res.getWriter();
        try {
            writer.write("{}");
        } finally {
            writer.close();
        }
    } else {
        throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR,
                "Log file could not be deleted immediately");
    }
}

From source file:io.aos.t4f.hadoop.mapred.WordCountTest.java

private void createTextInputFile() throws IOException {
    OutputStream os = getFileSystem().create(new Path(input, "wordcount"));
    Writer wr = new OutputStreamWriter(os);
    wr.write("b a a\n");
    wr.close();
}

From source file:de.terrestris.shogun.security.ShogunAuthProcessingFilter.java

/**
 * React on unsuccessful authentication.
 * We again intercept the response and return a JSON object with a flag indicating unsuccessful login.
 *
 * @see WebContent/client/login.js// w w  w .  j av a2s .c om
 */
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException failed) throws IOException, ServletException {
    super.unsuccessfulAuthentication(request, response, failed);
    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
    Writer out = responseWrapper.getWriter();
    out.write("{success:false}");
    out.close();
}

From source file:net.emphased.lastcontact.CookieFileStore.java

public void save(File file) throws IOException, CookieFileStoreException {
    Writer writer = new FileWriter(file);
    try {//from  w w  w.  ja  v  a  2s. c  o  m
        save(writer);
    } finally {
        writer.close();
    }
}

From source file:com.omricat.yacc.backend.servlets.CurrenciesProcessor.java

public void writeToStore(@NotNull final CurrencyDataset currencyDataset) throws IOException {

    final Writer stream = currenciesStore.getWriter();
    mapper.writeValue(stream, currencyDataset);
    stream.close();

}

From source file:com.apress.progwt.server.web.controllers.SimpleAnnotatedController.java

/**
 * this is the gwtLoginTargetURL. When GWT does a form based login, it
 * will redirect here. GWT can then read this 'ok' in to know that it
 * was ok.//w w  w .j  a v  a2 s .  c  o  m
 * 
 * @param w
 * @throws IOException
 */
@RequestMapping("/secure/gwtLoginOK.html")
public void gwtLoginHandler(Writer w) throws IOException {
    w.write("OK");
    w.close();
}