/** * Set the value of the {@linkplain #AUTHORIZATION Authorization} header to * Basic Authentication based on the given username and password. * @param username the username * @param password the password * @param charset the charset to use to convert the credentials into an octet * sequence. Defaults to {@linkplain StandardCharsets#ISO_8859_1 ISO-8859-1}. * @throws IllegalArgumentException if {@code username} or {@code password} * contains characters that cannot be encoded to the given charset * @since 5.1 * @see <a href="https://tools.ietf.org/html/rfc7617">RFC 7617</a> */ public void setBasicAuth(String username, String password, @Nullable Charset charset) { Assert.notNull(username, "Username must not be null"); Assert.notNull(password, "Password must not be null"); if (charset == null) { charset = StandardCharsets.ISO_8859_1; } CharsetEncoder encoder = charset.newEncoder(); if (!encoder.canEncode(username) || !encoder.canEncode(password)) { throw new IllegalArgumentException( "Username or password contains characters that cannot be encoded to " + charset.displayName()); } String credentialsString = username + ":" + password; byte[] encodedBytes = Base64.getEncoder().encode(credentialsString.getBytes(charset)); String encodedCredentials = new String(encodedBytes, charset); set(AUTHORIZATION, "Basic " + encodedCredentials); }
private static Set<String> getStandardCharsetNames() { return STANDARD_CHARSETS.stream().map(c -> c.displayName()).collect(Collectors.toSet()); }
private static Set<String> getStandardCharsetNames() { return STANDARD_CHARSETS.stream().map(c -> c.displayName()).collect(Collectors.toSet()); }
private String[] getCharsets() { if ( charsets == null ) { Collection<Charset> charsetCol = Charset.availableCharsets().values(); charsets = new String[charsetCol.size()]; int i = 0; for ( Charset charset : charsetCol ) { charsets[i++] = charset.displayName(); } } return charsets; }
/** * Check whether UTF-8 is the default character encoding. * Differing character encodings across cluster may lead to erratic behavior. */ private void checkFileEncoding() { String encodingDisplayName = Charset.defaultCharset().displayName(Locale.ENGLISH); if (!"UTF-8".equals(encodingDisplayName)) { U.quietAndWarn(log, "Default character encoding is " + encodingDisplayName + ". Specify UTF-8 character encoding by setting -Dfile.encoding=UTF-8 JVM parameter. " + "Differing character encodings across cluster may lead to erratic behavior."); } }
public static String guessEncodingName( File file ) throws FileNotFoundException, IOException { return guessEncoding( file, 4096 ).displayName(); }
/** * Override Tomcat's default locale mappings to align with other servers. See * {@code org.apache.catalina.util.CharsetMapperDefault.properties}. * @param context the context to reset */ private void resetDefaultLocaleMapping(TomcatEmbeddedContext context) { context.addLocaleEncodingMappingParameter(Locale.ENGLISH.toString(), DEFAULT_CHARSET.displayName()); context.addLocaleEncodingMappingParameter(Locale.FRENCH.toString(), DEFAULT_CHARSET.displayName()); }
/** * Returns an array of {@link AllowableValue} elements for each {@link Charset}. Only the charsets in {@link StandardCharsets} are returned to be consistent across JVM instances. * * @return an ordered {@code AllowableValue[]} containing the values */ public static AllowableValue[] buildCharacterSetAllowableValues() { final List<Charset> charsets = getSupportedCharsets(); return charsets.stream().map(cs -> new AllowableValue(cs.name(), cs.displayName(), cs == StandardCharsets.UTF_16 ? UTF_16_DESCRIPTION : cs.displayName()) ).toArray(AllowableValue[]::new); }
private String extractCharset(HeaderMap headers) { String contentType = headers.getFirst(Headers.CONTENT_TYPE); if (contentType != null) { String value = Headers.extractQuotedValueFromHeader(contentType, "charset"); if (value != null) { return value; } //if it is text we default to ISO_8859_1 if(contentType.startsWith("text/")) { return StandardCharsets.ISO_8859_1.displayName(); } return null; } return null; }
private static DefaultInputModule createModule(ProjectDefinition def, int scannerComponentId) { LOG.debug(" Init module '{}'", def.getName()); DefaultInputModule module = new DefaultInputModule(def, scannerComponentId); LOG.debug(" Base dir: {}", module.getBaseDir().toAbsolutePath().toString()); LOG.debug(" Working dir: {}", module.getWorkDir().toAbsolutePath().toString()); LOG.debug(" Module global encoding: {}, default locale: {}", module.getEncoding().displayName(), Locale.getDefault()); return module; }
private void setEncodings() { // Encoding of the text file: if ( !gotEncodings ) { gotEncodings = true; wEncoding.removeAll(); List<Charset> values = new ArrayList<Charset>( Charset.availableCharsets().values() ); for ( int i = 0; i < values.size(); i++ ) { Charset charSet = values.get( i ); wEncoding.add( charSet.displayName() ); } // Now select the default! String defEncoding = Const.getEnvironmentVariable( "file.encoding", "UTF-8" ); int idx = Const.indexOfString( defEncoding, wEncoding.getItems() ); if ( idx >= 0 ) { wEncoding.select( idx ); } } }
private void setEncodings() { // Encoding of the text file: if ( !gotEncodings ) { gotEncodings = true; wEncoding.removeAll(); List<Charset> values = new ArrayList<Charset>( Charset.availableCharsets().values() ); for ( int i = 0; i < values.size(); i++ ) { Charset charSet = values.get( i ); wEncoding.add( charSet.displayName() ); } // Now select the default! String defEncoding = Const.getEnvironmentVariable( "file.encoding", "UTF-8" ); int idx = Const.indexOfString( defEncoding, wEncoding.getItems() ); if ( idx >= 0 ) { wEncoding.select( idx ); } } }
private void setEncodings( ComboVar var ) { // Encoding of the text file: String encoding = Const.NVL( var.getText(), Const.getEnvironmentVariable( "file.encoding", "UTF-8" ) ); var.removeAll(); ArrayList<Charset> values = new ArrayList<Charset>( Charset.availableCharsets().values() ); for ( int i = 0; i < values.size(); i++ ) { Charset charSet = values.get( i ); var.add( charSet.displayName() ); } // Now select the default! int idx = Const.indexOfString( encoding, var.getItems() ); if ( idx >= 0 ) { var.select( idx ); } } }
public static String readFromFile(File f) throws IOException { FileInputStream fileInputStream = new FileInputStream(f); try { ByteArrayOutputStream byteArrayOutputStream = IOUtils.readFully(fileInputStream); return byteArrayOutputStream.toString(IOUtils.UTF8.displayName()); } finally { IOUtils.closeQuietly(fileInputStream); } } }
@Test public void testMultiByteBreak() throws Exception { System.out.println("testMultiByteBreak() Default charset: "+Charset.defaultCharset().displayName()); final long delay = 50; final File origin = new File(this.getClass().getResource("/test-file-utf8.bin").toURI());
private void setEncodings() { // Encoding of the text file: if ( !gotEncodings ) { gotEncodings = true; wEncoding.removeAll(); java.util.List<Charset> values = new ArrayList<Charset>( Charset.availableCharsets().values() ); for ( Charset charSet : values ) { wEncoding.add( charSet.displayName() ); } // Now select the default! String defEncoding = Const.getEnvironmentVariable( "file.encoding", "UTF-8" ); int idx = Const.indexOfString( defEncoding, wEncoding.getItems() ); if ( idx >= 0 ) { wEncoding.select( idx ); } } }
private void setEncodings() { // Encoding of the text file: if ( !gotEncodings ) { gotEncodings = true; wEncoding.removeAll(); List<Charset> values = new ArrayList<>( Charset.availableCharsets().values() ); for ( Charset charSet : values ) { wEncoding.add( charSet.displayName() ); } // Now select the default! String defEncoding = Const.getEnvironmentVariable( "file.encoding", "UTF-8" ); int idx = Const.indexOfString( defEncoding, wEncoding.getItems() ); if ( idx >= 0 ) { wEncoding.select( idx ); } } }
private void setEncodings() { // Encoding of the text file: if ( !gotEncodings ) { gotEncodings = true; wEncoding.removeAll(); java.util.List<Charset> values = new ArrayList<Charset>( Charset.availableCharsets().values() ); for ( Charset charSet : values ) { wEncoding.add( charSet.displayName() ); } // Now select the default! String defEncoding = Const.getEnvironmentVariable( "file.encoding", "UTF-8" ); int idx = Const.indexOfString( defEncoding, wEncoding.getItems() ); if ( idx >= 0 ) { wEncoding.select( idx ); } } } }
private void setEncodings() { // Encoding of the text file: if ( !gotEncodings ) { gotEncodings = true; wEncoding.removeAll(); List<Charset> values = new ArrayList<Charset>( Charset.availableCharsets().values() ); for ( Charset charSet : values ) { wEncoding.add( charSet.displayName() ); } // Now select the default! String defEncoding = Const.getEnvironmentVariable( "file.encoding", "UTF-8" ); int idx = Const.indexOfString( defEncoding, wEncoding.getItems() ); if ( idx >= 0 ) { wEncoding.select( idx ); } } }
public DefaultInputProject provide(ProjectBuildersExecutor projectBuildersExecutor, ProjectReactorValidator validator, ProjectReactor projectReactor, ScannerComponentIdGenerator scannerComponentIdGenerator) { if (project == null) { // 1 Apply project builders projectBuildersExecutor.execute(projectReactor); // 2 Validate final reactor validator.validate(projectReactor); // 3 Create project project = new DefaultInputProject(projectReactor.getRoot(), scannerComponentIdGenerator.getAsInt()); LOG.info("Project key: {}", project.key()); LOG.info("Base dir: {}", project.getBaseDir().toAbsolutePath().toString()); LOG.info("Working dir: {}", project.getWorkDir().toAbsolutePath().toString()); LOG.debug("Project global encoding: {}, default locale: {}", project.getEncoding().displayName(), Locale.getDefault()); } return project; } }