Example usage for java.nio.charset CodingErrorAction REPORT

List of usage examples for java.nio.charset CodingErrorAction REPORT

Introduction

In this page you can find the example usage for java.nio.charset CodingErrorAction REPORT.

Prototype

CodingErrorAction REPORT

To view the source code for java.nio.charset CodingErrorAction REPORT.

Click Source Link

Document

Action indicating that a coding error is to be reported, either by returning a CoderResult object or by throwing a CharacterCodingException , whichever is appropriate for the method implementing the coding process.

Usage

From source file:com.google.gwtjsonrpc.server.JsonServlet.java

private String readBody(final ActiveCall call) throws IOException {
    if (!isBodyJson(call)) {
        throw new JsonParseException("Invalid Request Content-Type");
    }/* w ww . j  a v  a 2  s. co  m*/
    if (!isBodyUTF8(call)) {
        throw new JsonParseException("Invalid Request Character-Encoding");
    }

    final int len = call.httpRequest.getContentLength();
    if (len < 0) {
        throw new JsonParseException("Invalid Request Content-Length");
    }
    if (len == 0) {
        throw new JsonParseException("Invalid Request POST Body Required");
    }
    if (len > maxRequestSize()) {
        throw new JsonParseException("Invalid Request POST Body Too Large");
    }

    final InputStream in = call.httpRequest.getInputStream();
    if (in == null) {
        throw new JsonParseException("Invalid Request POST Body Required");
    }

    try {
        final byte[] body = new byte[len];
        int off = 0;
        while (off < len) {
            final int n = in.read(body, off, len - off);
            if (n <= 0) {
                throw new JsonParseException("Invalid Request Incomplete Body");
            }
            off += n;
        }

        final CharsetDecoder d = Charset.forName(JsonUtil.JSON_ENC).newDecoder();
        d.onMalformedInput(CodingErrorAction.REPORT);
        d.onUnmappableCharacter(CodingErrorAction.REPORT);
        try {
            return d.decode(ByteBuffer.wrap(body)).toString();
        } catch (CharacterCodingException e) {
            throw new JsonParseException("Invalid Request Not UTF-8", e);
        }
    } finally {
        in.close();
    }
}

From source file:com.microsoft.tfs.util.NewlineUtils.java

/**
 * Converts all of the line-endings in a file to the desired newline
 * sequence. The conversion is done using a temporary file which replaces
 * the original file before the method returns.
 * <p>/*from www . j av a 2 s.c  o  m*/
 * The character set to use for the input and output files may be specified,
 * or left null to use the default.
 * <p>
 * This method uses Java's {@link InputStreamReader} class to read the input
 * file, automatically detects the byte-encoding of the input file
 * (UTF-16LE, UTF-16BE, UTF-8, etc.). The ability of this method to read and
 * convert files is limited to those which can be read by the Java runtime
 * where it is run.
 *
 * @param file
 *        the file to convert (which will be replaced by the converted file
 *        when this method returns). Not null.
 * @param charset
 *        the {@link Charset} to use to read the input file and write the
 *        converted file, or null to use the default encoding.
 * @param desiredNewlineSequence
 *        the desired newline sequence to write to the converted file. Not
 *        null, but may be empty to remove newlines. See public members of
 *        this class for pre-defined newline characters.
 * @throws UnsupportedEncodingException
 *         if the encoding given is not supported by this Java environment.
 * @throws IOException
 *         if an error occurred reading the file or writing the converted
 *         file. This exception may also indicate an error reading the file
 *         with the given character set.
 */
public static void convertFile(final File file, final Charset charset, final String desiredNewlineSequence)
        throws UnsupportedEncodingException, IOException {
    Check.notNull(file, "file"); //$NON-NLS-1$
    Check.notNull(desiredNewlineSequence, "desiredNewlineSequence"); //$NON-NLS-1$

    final File directory = file.getParentFile();
    File temp = File.createTempFile("tfsEOL", ".tmp", directory); //$NON-NLS-1$ //$NON-NLS-2$

    try {
        /*
         * Don't need a buffered reader because the conversion method uses
         * its own buffer.
         */
        Reader reader = null;
        Writer writer = null;
        try {
            /*
             * The concept of the "default" character set is complicated,
             * since it means something different to the different stream
             * layers involved. It's simplest just to use the different
             * constructors.
             */
            if (charset == null) {
                reader = new InputStreamReader(new FileInputStream(file));
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(temp)));
            } else {
                final CharsetDecoder decoder = charset.newDecoder();
                decoder.onMalformedInput(CodingErrorAction.REPORT);
                decoder.onUnmappableCharacter(CodingErrorAction.REPORT);

                reader = new InputStreamReader(new FileInputStream(file), decoder);
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(temp), charset));
            }

            replaceNewlines(reader, writer, desiredNewlineSequence, false);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException e) {
                    log.error("Error closing reader", e); //$NON-NLS-1$
                }
            }

            if (writer != null) {
                try {
                    writer.close();
                } catch (final IOException e) {
                    log.error("Error closing writer", e); //$NON-NLS-1$
                }
            }
        }

        /*
         * Replace the original with the temp file. Delete the original file
         * because renaming to an existing file which is read-only fails on
         * some platforms. Changing the original file to writeable requires
         * JNI methods not available in com.microsoft.tfs.util for
         * dependency reasons.
         */

        if (file.delete() == false) {
            final String messageFormat = "Error deleting file '{0}' for replacement with EOL-converted file."; //$NON-NLS-1$
            final String message = MessageFormat.format(messageFormat, file.getAbsolutePath());
            log.error(message);
            throw new IOException(message);
        }

        if (temp.renameTo(file) == false) {
            final String messageFormat = "Error renaming temporary file '{0}' to '{1}' after EOL conversion."; //$NON-NLS-1$
            final String message = MessageFormat.format(messageFormat, temp.getAbsolutePath(),
                    file.getAbsolutePath());
            log.error(message);
            throw new IOException(message);
        }

        /*
         * File no longer exists, we can skip the deletion for slight
         * performance increase.
         */
        temp = null;
    } finally {
        if (temp != null) {
            temp.delete();
        }
    }
}

From source file:gate.util.Files.java

/**
 * This method updates an XML element in an XML file
 * with a new set of attributes. If the element is not found the XML
 * file is unchanged. The attributes keys and values must all be Strings.
 * We first try to read the file using UTF-8 encoding.  If an error occurs we
 * fall back to the platform default encoding (for backwards-compatibility
 * reasons) and try again.  The file is written back in UTF-8, with an
 * updated encoding declaration./*from www. j av a2s.c  om*/
 *
 * @param xmlFile An XML file.
 * @param elementName The name of the element to update.
 * @param newAttrs The new attributes to place on the element.
 * @return A string of the whole XML file, with the element updated (the
 *   file is also overwritten).
 */
public static String updateXmlElement(File xmlFile, String elementName, Map<String, String> newAttrs)
        throws IOException {
    String newXml = null;
    BufferedReader utfFileReader = null;
    BufferedReader platformFileReader = null;
    Charset utfCharset = Charset.forName("UTF-8");
    try {
        FileInputStream fis = new FileInputStream(xmlFile);
        // try reading with UTF-8, make sure any errors throw an exception
        CharsetDecoder decoder = utfCharset.newDecoder().onUnmappableCharacter(CodingErrorAction.REPORT)
                .onMalformedInput(CodingErrorAction.REPORT);
        utfFileReader = new BomStrippingInputStreamReader(fis, decoder);
        newXml = updateXmlElement(utfFileReader, elementName, newAttrs);
    } catch (CharacterCodingException cce) {
        // File not readable as UTF-8, so try the platform default encoding
        if (utfFileReader != null) {
            utfFileReader.close();
            utfFileReader = null;
        }
        if (DEBUG) {
            Err.prln("updateXmlElement: could not read " + xmlFile + " as UTF-8, " + "trying platform default");
        }
        platformFileReader = new BufferedReader(new FileReader(xmlFile));
        newXml = updateXmlElement(platformFileReader, elementName, newAttrs);
    } finally {
        if (utfFileReader != null) {
            utfFileReader.close();
        }
        if (platformFileReader != null) {
            platformFileReader.close();
        }
    }

    // write the updated file in UTF-8, fixing the encoding declaration
    newXml = newXml.replaceFirst("\\A<\\?xml (.*)encoding=(?:\"[^\"]*\"|'[^']*')",
            "<?xml $1encoding=\"UTF-8\"");
    FileOutputStream fos = new FileOutputStream(xmlFile);
    OutputStreamWriter fileWriter = new OutputStreamWriter(fos, utfCharset);
    fileWriter.write(newXml);
    fileWriter.close();

    return newXml;
}

From source file:cn.howardliu.lucene.extension.ManagedSynonymFilterFactory.java

/**
 * Called once, during core initialization, to initialize any analysis components
 * that depend on the data managed by this resource. It is important that the
 * analysis component is only initialized once during core initialization so that
 * text analysis is consistent, especially in a distributed environment, as we
 * don't want one server applying a different set of stop words than other servers.
 *//*from  ww  w  .  j  a  v  a  2 s .c o m*/
@SuppressWarnings("unchecked")
@Override
public void onManagedResourceInitialized(NamedList<?> initArgs, final ManagedResource res)
        throws SolrException {
    NamedList<Object> args = (NamedList<Object>) initArgs;
    args.add("synonyms", getResourceId());
    args.add("expand", "false");
    args.add("format", "solr");

    Map<String, String> filtArgs = new HashMap<>();
    for (Map.Entry<String, ?> entry : args) {
        filtArgs.put(entry.getKey(), entry.getValue().toString());
    }
    // create the actual filter factory that pulls the synonym mappings
    // from synonymMappings using a custom parser implementation
    delegate = new SynonymFilterFactory(filtArgs) {
        @Override
        protected SynonymMap loadSynonyms(ResourceLoader loader, String cname, boolean dedup, Analyzer analyzer)
                throws IOException, ParseException {
            CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder()
                    .onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
            ManagedSynonymParser parser = new ManagedSynonymParser((SynonymManager) res, dedup, analyzer);
            // ???
            InputStreamReader in = null;
            if (StringUtils.isNotBlank(synonymFile)) {
                in = new InputStreamReader(loader.openResource(synonymFile), decoder);
            }
            parser.parse(in);
            return parser.build();
        }
    };
    try {
        delegate.inform(res.getResourceLoader());
    } catch (IOException e) {
        throw new SolrException(ErrorCode.SERVER_ERROR, e);
    }
}

From source file:org.archive.modules.fetcher.FetchHTTPRequest.java

protected HttpClientConnectionManager buildConnectionManager() {
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE).register("https",
                    new SSLConnectionSocketFactory(fetcher.sslContext(), new AllowAllHostnameVerifier()) {

                        @Override
                        public Socket createLayeredSocket(final Socket socket, final String target,
                                final int port, final HttpContext context) throws IOException {

                            return super.createLayeredSocket(socket, isDisableSNI() ? "" : target, port,
                                    context);
                        }/*from  w  ww .j  ava  2  s  .  c o m*/
                    })
            .build();

    DnsResolver dnsResolver = new ServerCacheResolver(fetcher.getServerCache());

    ManagedHttpClientConnectionFactory connFactory = new ManagedHttpClientConnectionFactory() {
        private static final int DEFAULT_BUFSIZE = 8 * 1024;

        @Override
        public ManagedHttpClientConnection create(HttpRoute route, ConnectionConfig config) {
            final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
            CharsetDecoder chardecoder = null;
            CharsetEncoder charencoder = null;
            final Charset charset = cconfig.getCharset();
            final CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null
                    ? cconfig.getMalformedInputAction()
                    : CodingErrorAction.REPORT;
            final CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null
                    ? cconfig.getUnmappableInputAction()
                    : CodingErrorAction.REPORT;
            if (charset != null) {
                chardecoder = charset.newDecoder();
                chardecoder.onMalformedInput(malformedInputAction);
                chardecoder.onUnmappableCharacter(unmappableInputAction);
                charencoder = charset.newEncoder();
                charencoder.onMalformedInput(malformedInputAction);
                charencoder.onUnmappableCharacter(unmappableInputAction);
            }
            return new RecordingHttpClientConnection(DEFAULT_BUFSIZE, DEFAULT_BUFSIZE, chardecoder, charencoder,
                    cconfig.getMessageConstraints(), null, null, DefaultHttpRequestWriterFactory.INSTANCE,
                    DefaultHttpResponseParserFactory.INSTANCE);
        }
    };
    BasicHttpClientConnectionManager connMan = new BasicHttpClientConnectionManager(socketFactoryRegistry,
            connFactory, null, dnsResolver);

    SocketConfig.Builder socketConfigBuilder = SocketConfig.custom();
    socketConfigBuilder.setSoTimeout(fetcher.getSoTimeoutMs());
    connMan.setSocketConfig(socketConfigBuilder.build());

    return connMan;
}

From source file:com.maxl.java.aips2xml.Aips2Xml.java

static void writeToFile(String string_to_write, String dir_name, String file_name) {
    try {//from w  w w .j  a va 2s . c  om
        File wdir = new File(dir_name);
        if (!wdir.exists())
            wdir.mkdirs();
        File wfile = new File(dir_name + file_name);
        if (!wfile.exists())
            wfile.createNewFile();
        // FileWriter fw = new FileWriter(wfile.getAbsoluteFile());
        CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
        encoder.onMalformedInput(CodingErrorAction.REPORT);
        encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(wfile.getAbsoluteFile()), encoder);
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write(string_to_write);
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:nu.validator.servlet.MultipartFormDataFilter.java

private static String utf8ByteStreamToString(InputStream stream) throws IOException {
    CharsetDecoder dec = Charset.forName("UTF-8").newDecoder();
    dec.onMalformedInput(CodingErrorAction.REPORT);
    dec.onUnmappableCharacter(CodingErrorAction.REPORT);
    Reader reader = new InputStreamReader(stream, dec);
    StringBuilder builder = new StringBuilder();
    int c;/*from   w ww.  j  ava 2 s. c o  m*/
    int i = 0;
    while ((c = reader.read()) != -1) {
        if (i > 2048) {
            throw new IOException("Form field value too large.");
        }
        builder.append((char) c);
        i++;
    }
    return builder.toString();
}

From source file:org.alfresco.repo.webdav.auth.AuthenticationFilter.java

/**
 * Run the authentication filter//from   w  ww  .j av a2 s  .  c o m
 * 
 * @param context ServletContext
 * @param req ServletRequest
 * @param resp ServletResponse
 * @param chain FilterChain
 * @exception ServletException
 * @exception IOException
 */
public void doFilter(ServletContext context, ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
    if (logger.isDebugEnabled())
        logger.debug("Entering AuthenticationFilter.");

    // Assume it's an HTTP request

    HttpServletRequest httpReq = (HttpServletRequest) req;
    HttpServletResponse httpResp = (HttpServletResponse) resp;

    // Get the user details object from the session
    SessionUser user = getSessionUser(context, httpReq, httpResp, false);

    if (user == null) {
        if (logger.isDebugEnabled())
            logger.debug("There is no user in the session.");
        // Get the authorization header

        String authHdr = httpReq.getHeader("Authorization");

        if (authHdr != null && authHdr.length() > 5 && authHdr.substring(0, 5).equalsIgnoreCase("BASIC")) {
            if (logger.isDebugEnabled())
                logger.debug("Basic authentication details present in the header.");
            byte[] encodedString = Base64.decodeBase64(authHdr.substring(5).getBytes());

            // ALF-13621: Due to browser inconsistencies we have to try a fallback path of encodings
            Set<String> attemptedAuths = new HashSet<String>(ENCODINGS.length * 2);
            for (String encoding : ENCODINGS) {
                CharsetDecoder decoder = Charset.forName(encoding).newDecoder()
                        .onMalformedInput(CodingErrorAction.REPORT);
                try {
                    // Attempt to decode using this charset 
                    String basicAuth = decoder.decode(ByteBuffer.wrap(encodedString)).toString();

                    // It decoded OK but we may already have tried this string.
                    if (!attemptedAuths.add(basicAuth)) {
                        // Already tried - no need to try again
                        continue;
                    }

                    String username = null;
                    String password = null;

                    // Split the username and password
                    int pos = basicAuth.indexOf(":");
                    if (pos != -1) {
                        username = basicAuth.substring(0, pos);
                        password = basicAuth.substring(pos + 1);
                    } else {
                        username = basicAuth;
                        password = "";
                    }

                    // Go to the repo and authenticate
                    Authorization auth = new Authorization(username, password);
                    if (auth.isTicket()) {
                        authenticationService.validate(auth.getTicket());
                    } else {
                        authenticationService.authenticate(username, password.toCharArray());
                        authenticationListener.userAuthenticated(new BasicAuthCredentials(username, password));
                    }

                    user = createUserEnvironment(httpReq.getSession(),
                            authenticationService.getCurrentUserName(),
                            authenticationService.getCurrentTicket(), false);

                    // Success so break out
                    break;
                } catch (CharacterCodingException e) {
                    if (logger.isDebugEnabled())
                        logger.debug("Didn't decode using " + decoder.getClass().getName(), e);
                } catch (AuthenticationException ex) {
                    if (logger.isDebugEnabled())
                        logger.debug("Authentication error ", ex);
                } catch (NoSuchPersonException e) {
                    if (logger.isDebugEnabled())
                        logger.debug("There is no such person error ", e);
                }
            }
        } else {
            // Check if the request includes an authentication ticket

            String ticket = req.getParameter(ARG_TICKET);

            if (ticket != null && ticket.length() > 0) {
                // PowerPoint bug fix
                if (ticket.endsWith(PPT_EXTN)) {
                    ticket = ticket.substring(0, ticket.length() - PPT_EXTN.length());
                }

                // Debug

                if (logger.isDebugEnabled())
                    logger.debug("Logon via ticket from " + req.getRemoteHost() + " (" + req.getRemoteAddr()
                            + ":" + req.getRemotePort() + ")" + " ticket=" + ticket);

                // Validate the ticket

                authenticationService.validate(ticket);
                authenticationListener.userAuthenticated(new TicketCredentials(ticket));

                // Need to create the User instance if not already available

                String currentUsername = authenticationService.getCurrentUserName();

                user = createUserEnvironment(httpReq.getSession(), currentUsername, ticket, false);
            }
        }

        // Check if the user is authenticated, if not then prompt again

        if (user == null) {
            if (logger.isDebugEnabled())
                logger.debug("No user/ticket, force the client to prompt for logon details.");

            httpResp.setHeader("WWW-Authenticate", "BASIC realm=\"Alfresco DAV Server\"");
            httpResp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

            httpResp.flushBuffer();
            return;
        }
    } else {
        authenticationListener.userAuthenticated(new TicketCredentials(user.getTicket()));
    }

    // Chain other filters

    chain.doFilter(req, resp);
}

From source file:org.apache.hadoop.mapreduce.lib.input.CSVRecordReader.java

protected Reader getReader(InputStream is, Configuration conf) throws UnsupportedEncodingException {
    String encoding = conf.get(CSVFileInputFormat.FORMAT_ENCODING, CSVFileInputFormat.DEFAULT_ENCODING);
    boolean isEncodingSupported = false;
    for (String supportedEncoding : CSVTextInputFormat.SUPPORTED_ENCODINGS) {
        if (supportedEncoding.equals(encoding)) {
            isEncodingSupported = true;//from   w ww .ja  va 2 s  .  com
            break;
        }
    }
    if (!isEncodingSupported) {
        StringBuilder errSb = new StringBuilder();
        for (String supportedEncoding : CSVTextInputFormat.SUPPORTED_ENCODINGS) {
            if (errSb.length() != 0) {
                errSb.append(", ");
            }
            errSb.append("'");
            errSb.append(supportedEncoding);
            errSb.append("'");
        }
        throw new UnsupportedEncodingException(
                "CSVInputFormat only supports the following encodings: " + errSb.toString());
    }
    if (encoding.equals("ISO-8859-1")) {
        isAscii = true;
    }
    CharsetDecoder decoder = Charset.forName(encoding).newDecoder();
    // NOTE: we are very strict about encoded characters since if we replace or ignore, we may unwittingly mess up
    //       our split points...the reader doesn't tell us how many bytes were malformed/unmappable
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    return new BufferedReader(new InputStreamReader(is, decoder));
}

From source file:org.apache.http.HC4.impl.conn.ManagedHttpClientConnectionFactory.java

@Override
public ManagedHttpClientConnection create(final HttpRoute route, final ConnectionConfig config) {
    final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
    CharsetDecoder chardecoder = null;
    CharsetEncoder charencoder = null;
    final Charset charset = cconfig.getCharset();
    final CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null
            ? cconfig.getMalformedInputAction()
            : CodingErrorAction.REPORT;
    final CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null
            ? cconfig.getUnmappableInputAction()
            : CodingErrorAction.REPORT;
    if (charset != null) {
        chardecoder = charset.newDecoder();
        chardecoder.onMalformedInput(malformedInputAction);
        chardecoder.onUnmappableCharacter(unmappableInputAction);
        charencoder = charset.newEncoder();
        charencoder.onMalformedInput(malformedInputAction);
        charencoder.onUnmappableCharacter(unmappableInputAction);
    }/*from  w  ww  .j a va2 s.  c  o m*/
    final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
    return new LoggingManagedHttpClientConnection(id, log, headerlog, wirelog, cconfig.getBufferSize(),
            cconfig.getFragmentSizeHint(), chardecoder, charencoder, cconfig.getMessageConstraints(), null,
            null, requestWriterFactory, responseParserFactory);
}