Java Code Examples for org.apache.tomcat.util.buf.B2CConverter#getCharset()

The following examples show how to use org.apache.tomcat.util.buf.B2CConverter#getCharset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Response.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Overrides the character encoding used in the body of the response. This
 * method must be called prior to writing output using getWriter().
 *
 * @param characterEncoding The name of character encoding.
 */
public void setCharacterEncoding(String characterEncoding) {
    if (isCommitted()) {
        return;
    }
    if (characterEncoding == null) {
        return;
    }

    try {
        this.charset = B2CConverter.getCharset(characterEncoding);
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException(e);
    }
    this.characterEncoding = characterEncoding;
}
 
Example 2
Source File: InputBuffer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public void checkConverter() throws IOException {
    if (conv != null) {
        return;
    }

    Charset charset = null;

    if (coyoteRequest != null) {
        charset = coyoteRequest.getCharset();
    }

    if (charset == null) {
        if (enc == null) {
            charset = org.apache.coyote.Constants.DEFAULT_BODY_CHARSET;
        } else {
            charset = B2CConverter.getCharset(enc);
        }
    }

    SynchronizedStack<B2CConverter> stack = encoders.get(charset);
    if (stack == null) {
        stack = new SynchronizedStack<>();
        encoders.putIfAbsent(charset, stack);
        stack = encoders.get(charset);
    }
    conv = stack.pop();

    if (conv == null) {
        conv = createConverter(charset);
    }
}
 
Example 3
Source File: Request.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Overrides the name of the character encoding used in the body of
 * this request.  This method must be called prior to reading request
 * parameters or reading input using <code>getReader()</code>.
 *
 * @param enc The character encoding to be used
 *
 * @exception UnsupportedEncodingException if the specified encoding
 *  is not supported
 *
 * @since Servlet 2.3
 */
@Override
public void setCharacterEncoding(String enc)
    throws UnsupportedEncodingException {

    if (usingReader) {
        return;
    }

    // Confirm that the encoding name is valid
    B2CConverter.getCharset(enc);

    // Save the validated encoding
    coyoteRequest.setCharacterEncoding(enc);
}
 
Example 4
Source File: Parameters.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private Charset getCharset(String encoding) {
    if (encoding == null) {
        return DEFAULT_CHARSET;
    }
    try {
        return B2CConverter.getCharset(encoding);
    } catch (UnsupportedEncodingException e) {
        return DEFAULT_CHARSET;
    }
}
 
Example 5
Source File: XmlEncodingBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * @param encoding The encoding of the XML source that was used to
 *                 populated this object.
 * @deprecated This method will be removed in Tomcat 9
 */
@Deprecated
public void setEncoding(String encoding) {
    try {
        charset = B2CConverter.getCharset(encoding);
    } catch (UnsupportedEncodingException e) {
        log.warn(sm.getString("xmlEncodingBase.encodingInvalid", encoding, charset.name()), e);
    }
}
 
Example 6
Source File: Parameters.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private Charset getCharset(String encoding, Charset defaultCharset) {
    if (encoding == null) {
        return defaultCharset;
    }
    try {
        return B2CConverter.getCharset(encoding);
    } catch (UnsupportedEncodingException e) {
        return defaultCharset;
    }
}
 
Example 7
Source File: Request.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Get the character encoding used for this request.
 *
 * @return The value set via {@link #setCharacterEncoding(String)} or if no
 *         call has been made to that method try to obtain if from the
 *         content type.
 *
 * @throws UnsupportedEncodingException If the user agent has specified an
 *         invalid character encoding
 */
public Charset getCharset() throws UnsupportedEncodingException {
    if (charset == null) {
        getCharacterEncoding();
        if (characterEncoding != null) {
            charset = B2CConverter.getCharset(characterEncoding);
        }
    }

    return charset;
}
 
Example 8
Source File: Parameters.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private Charset getCharset(String encoding) {
    if (encoding == null) {
        return DEFAULT_CHARSET;
    }
    try {
        return B2CConverter.getCharset(encoding);
    } catch (UnsupportedEncodingException e) {
        return DEFAULT_CHARSET;
    }
}
 
Example 9
Source File: URLEncoder.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * URL encodes the provided path using the given encoding.
 *
 * @param path      The path to encode
 * @param encoding  The encoding to use to convert the path to bytes
 *
 * @return The encoded path
 *
 * @deprecated This will be removed in Tomcat 9.0.x
 */
@Deprecated
public String encode(String path, String encoding) {
    Charset charset;
    try {
        charset = B2CConverter.getCharset(encoding);
    } catch (UnsupportedEncodingException e) {
        charset = Charset.defaultCharset();
    }
    return encode(path, charset);
}
 
Example 10
Source File: RealmBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private Charset getDigestCharset() throws UnsupportedEncodingException {
    String charset = getDigestEncoding();
    if (charset == null) {
        return StandardCharsets.ISO_8859_1;
    } else {
        return B2CConverter.getCharset(charset);
    }
}
 
Example 11
Source File: MessageDigestCredentialHandler.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public void setEncoding(String encodingName) {
    if (encodingName == null) {
        encoding = StandardCharsets.UTF_8;
    } else {
        try {
            this.encoding = B2CConverter.getCharset(encodingName);
        } catch (UnsupportedEncodingException e) {
            log.warn(sm.getString("mdCredentialHandler.unknownEncoding",
                    encodingName, encoding.name()));
        }
    }
}
 
Example 12
Source File: RequestUtil.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Decode and return the specified URL-encoded byte array.
 *
 * @param bytes The url-encoded byte array
 * @param enc The encoding to use; if null, the default encoding is used. If
 * an unsupported encoding is specified null will be returned
 * @param isQuery Is this a query string being processed
 * @exception IllegalArgumentException if a '%' character is not followed
 * by a valid 2-digit hexadecimal number
 */
public static String URLDecode(byte[] bytes, String enc, boolean isQuery) {

    if (bytes == null)
        return null;

    int len = bytes.length;
    int ix = 0;
    int ox = 0;
    while (ix < len) {
        byte b = bytes[ix++];     // Get byte to test
        if (b == '+' && isQuery) {
            b = (byte)' ';
        } else if (b == '%') {
            if (ix + 2 > len) {
                throw new IllegalArgumentException(
                        sm.getString("requestUtil.urlDecode.missingDigit"));
            }
            b = (byte) ((convertHexDigit(bytes[ix++]) << 4)
                        + convertHexDigit(bytes[ix++]));
        }
        bytes[ox++] = b;
    }
    if (enc != null) {
        try {
            return new String(bytes, 0, ox, B2CConverter.getCharset(enc));
        } catch (UnsupportedEncodingException uee) {
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("requestUtil.urlDecode.uee", enc), uee);
            }
            return null;
        }
    }
    return new String(bytes, 0, ox);

}
 
Example 13
Source File: TesterParametersPerformance.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private long doCreateString(String input, int size,
        boolean defensiveCopyWorkAround) {
    int loops = 10000;
    byte[] inputBytes = input.getBytes();
    byte[] bytes = new byte[size];
    int inputLength = inputBytes.length;

    System.arraycopy(inputBytes, 0, bytes, 0, inputLength);

    String[] result = new String[loops];
    Charset charset = null;
    try {
        charset = B2CConverter.getCharset("ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    long start = System.nanoTime();
    for (int i = 0; i < loops; i++) {
        if (defensiveCopyWorkAround) {
            byte[] tmp = new byte[inputLength];
            System.arraycopy(bytes, 0, tmp, 0, inputLength);
            result[i] = new String(tmp, 0, inputLength, charset);
        } else {
            result[i] = new String(bytes, 0, inputLength, charset);
        }
    }

    return System.nanoTime() - start;
}
 
Example 14
Source File: Request.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Overrides the name of the character encoding used in the body of
 * this request.  This method must be called prior to reading request
 * parameters or reading input using <code>getReader()</code>.
 *
 * @param enc The character encoding to be used
 *
 * @exception UnsupportedEncodingException if the specified encoding
 *  is not supported
 *
 * @since Servlet 2.3
 */
@Override
public void setCharacterEncoding(String enc) throws UnsupportedEncodingException {

    if (usingReader) {
        return;
    }

    // Confirm that the encoding name is valid
    Charset charset = B2CConverter.getCharset(enc);

    // Save the validated encoding
    coyoteRequest.setCharset(charset);
}
 
Example 15
Source File: TesterParametersPerformance.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private long doCreateString(String input, int size,
        boolean defensiveCopyWorkAround) {
    int loops = 10000;
    byte[] inputBytes = input.getBytes();
    byte[] bytes = new byte[size];
    int inputLength = inputBytes.length;

    System.arraycopy(inputBytes, 0, bytes, 0, inputLength);

    String[] result = new String[loops];
    Charset charset = null;
    try {
        charset = B2CConverter.getCharset("ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    long start = System.nanoTime();
    for (int i = 0; i < loops; i++) {
        if (defensiveCopyWorkAround) {
            byte[] tmp = new byte[inputLength];
            System.arraycopy(bytes, 0, tmp, 0, inputLength);
            result[i] = new String(tmp, 0, inputLength, charset);
        } else {
            result[i] = new String(bytes, 0, inputLength, charset);
        }
    }

    return System.nanoTime() - start;
}
 
Example 16
Source File: TesterParametersPerformance.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateString() throws UnsupportedEncodingException {
    B2CConverter.getCharset("ISO-8859-1");
    doCreateStringMultiple("foo");
}
 
Example 17
Source File: TesterParametersPerformance.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateString() throws UnsupportedEncodingException {
    B2CConverter.getCharset("ISO-8859-1");
    doCreateStringMultiple("foo");
}
 
Example 18
Source File: SSIServletExternalResolver.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public String getFileText(String originalPath, boolean virtual)
        throws IOException {
    try {
        ServletContextAndPath csAndP = getServletContextAndPath(
                originalPath, virtual);
        ServletContext context = csAndP.getServletContext();
        String path = csAndP.getPath();
        RequestDispatcher rd = context.getRequestDispatcher(path);
        if (rd == null) {
            throw new IOException(
                    "Couldn't get request dispatcher for path: " + path);
        }
        ByteArrayServletOutputStream basos =
            new ByteArrayServletOutputStream();
        ResponseIncludeWrapper responseIncludeWrapper =
            new ResponseIncludeWrapper(context, req, res, basos);
        rd.include(req, responseIncludeWrapper);
        //We can't assume the included servlet flushed its output
        responseIncludeWrapper.flushOutputStreamOrWriter();
        byte[] bytes = basos.toByteArray();

        //Assume platform default encoding unless otherwise specified
        String retVal;
        if (inputEncoding == null) {
            retVal = new String( bytes );
        } else {
            retVal = new String (bytes,
                    B2CConverter.getCharset(inputEncoding));
        }

        //make an assumption that an empty response is a failure. This is
        // a problem
        // if a truly empty file
        //were included, but not sure how else to tell.
        if (retVal.equals("") && !req.getMethod().equalsIgnoreCase(
                org.apache.coyote.http11.Constants.HEAD)) {
            throw new IOException("Couldn't find file: " + path);
        }
        return retVal;
    } catch (ServletException e) {
        throw new IOException("Couldn't include file: " + originalPath
                + " because of ServletException: " + e.getMessage());
    }
}
 
Example 19
Source File: CoyoteAdapter.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Extract the path parameters from the request. This assumes parameters are
 * of the form /path;name=value;name2=value2/ etc. Currently only really
 * interested in the session ID that will be in this form. Other parameters
 * can safely be ignored.
 *
 * @param req
 * @param request
 */
protected void parsePathParameters(org.apache.coyote.Request req,
        Request request) {

    // Process in bytes (this is default format so this is normally a NO-OP
    req.decodedURI().toBytes();

    ByteChunk uriBC = req.decodedURI().getByteChunk();
    int semicolon = uriBC.indexOf(';', 0);

    // What encoding to use? Some platforms, eg z/os, use a default
    // encoding that doesn't give the expected result so be explicit
    String enc = connector.getURIEncoding();
    if (enc == null) {
        enc = "ISO-8859-1";
    }
    Charset charset = null;
    try {
        charset = B2CConverter.getCharset(enc);
    } catch (UnsupportedEncodingException e1) {
        log.warn(sm.getString("coyoteAdapter.parsePathParam",
                enc));
    }

    if (log.isDebugEnabled()) {
        log.debug(sm.getString("coyoteAdapter.debug", "uriBC",
                uriBC.toString()));
        log.debug(sm.getString("coyoteAdapter.debug", "semicolon",
                String.valueOf(semicolon)));
        log.debug(sm.getString("coyoteAdapter.debug", "enc", enc));
    }

    while (semicolon > -1) {
        // Parse path param, and extract it from the decoded request URI
        int start = uriBC.getStart();
        int end = uriBC.getEnd();

        int pathParamStart = semicolon + 1;
        int pathParamEnd = ByteChunk.findBytes(uriBC.getBuffer(),
                start + pathParamStart, end,
                new byte[] {';', '/'});

        String pv = null;

        if (pathParamEnd >= 0) {
            if (charset != null) {
                pv = new String(uriBC.getBuffer(), start + pathParamStart,
                            pathParamEnd - pathParamStart, charset);
            }
            // Extract path param from decoded request URI
            byte[] buf = uriBC.getBuffer();
            for (int i = 0; i < end - start - pathParamEnd; i++) {
                buf[start + semicolon + i]
                    = buf[start + i + pathParamEnd];
            }
            uriBC.setBytes(buf, start,
                    end - start - pathParamEnd + semicolon);
        } else {
            if (charset != null) {
                pv = new String(uriBC.getBuffer(), start + pathParamStart,
                            (end - start) - pathParamStart, charset);
            }
            uriBC.setEnd(start + semicolon);
        }

        if (log.isDebugEnabled()) {
            log.debug(sm.getString("coyoteAdapter.debug", "pathParamStart",
                    String.valueOf(pathParamStart)));
            log.debug(sm.getString("coyoteAdapter.debug", "pathParamEnd",
                    String.valueOf(pathParamEnd)));
            log.debug(sm.getString("coyoteAdapter.debug", "pv", pv));
        }

        if (pv != null) {
            int equals = pv.indexOf('=');
            if (equals > -1) {
                String name = pv.substring(0, equals);
                String value = pv.substring(equals + 1);
                request.addPathParameter(name, value);
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("coyoteAdapter.debug", "equals",
                            String.valueOf(equals)));
                    log.debug(sm.getString("coyoteAdapter.debug", "name",
                            name));
                    log.debug(sm.getString("coyoteAdapter.debug", "value",
                            value));
                }
            }
        }

        semicolon = uriBC.indexOf(';', semicolon);
    }
}
 
Example 20
Source File: SSIServletExternalResolver.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public String getFileText(String originalPath, boolean virtual)
        throws IOException {
    try {
        ServletContextAndPath csAndP = getServletContextAndPath(
                originalPath, virtual);
        ServletContext context = csAndP.getServletContext();
        String path = csAndP.getPath();
        RequestDispatcher rd = context.getRequestDispatcher(path);
        if (rd == null) {
            throw new IOException(
                    "Couldn't get request dispatcher for path: " + path);
        }
        ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream();
        ResponseIncludeWrapper responseIncludeWrapper = new ResponseIncludeWrapper(res, basos);
        rd.include(req, responseIncludeWrapper);
        //We can't assume the included servlet flushed its output
        responseIncludeWrapper.flushOutputStreamOrWriter();
        byte[] bytes = basos.toByteArray();

        //Assume platform default encoding unless otherwise specified
        String retVal;
        if (inputEncoding == null) {
            retVal = new String( bytes );
        } else {
            retVal = new String (bytes,
                    B2CConverter.getCharset(inputEncoding));
        }

        //make an assumption that an empty response is a failure. This is
        // a problem
        // if a truly empty file
        //were included, but not sure how else to tell.
        if (retVal.equals("") && !req.getMethod().equalsIgnoreCase("HEAD")) {
            throw new IOException("Couldn't find file: " + path);
        }
        return retVal;
    } catch (ServletException e) {
        throw new IOException("Couldn't include file: " + originalPath
                + " because of ServletException: " + e.getMessage());
    }
}