Example usage for org.apache.commons.io Charsets UTF_8

List of usage examples for org.apache.commons.io Charsets UTF_8

Introduction

In this page you can find the example usage for org.apache.commons.io Charsets UTF_8.

Prototype

Charset UTF_8

To view the source code for org.apache.commons.io Charsets UTF_8.

Click Source Link

Document

Eight-bit Unicode Transformation Format.

Usage

From source file:org.eclipse.rdf4j.http.server.ProtocolTest.java

/**
 * Checks that the server accepts a formencoded POST with an update and a timeout parameter.
 *//*from  w ww . jav a 2s.  c om*/
@Test
public void testUpdateForm_POST() throws Exception {
    String update = "delete where { <monkey:pod> ?p ?o . }";
    String location = Protocol.getStatementsLocation(TestServer.REPOSITORY_URL);
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost post = new HttpPost(location);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair(Protocol.UPDATE_PARAM_NAME, update));
    nvps.add(new BasicNameValuePair(Protocol.TIMEOUT_PARAM_NAME, "1"));
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, Charsets.UTF_8);

    post.setEntity(entity);

    CloseableHttpResponse response = httpclient.execute(post);

    System.out.println("Update Form Post Status: " + response.getStatusLine());
    int statusCode = response.getStatusLine().getStatusCode();
    assertEquals(true, statusCode >= 200 && statusCode < 400);
}

From source file:org.esupportail.publisher.config.FileUploadConfiguration.java

@Bean(name = "publicFileUploadHelper")
public FileUploadHelper publicFileUploadHelper() throws IOException, URISyntaxException {
    FileUploadHelper fuh = new FileUploadHelper();
    final String path = getDefinedUploadPath(ENV_UPLOAD_PATH);
    Assert.notNull(path, "You should define a protected file path");

    fuh.setUploadDirectoryPath(path);/*w  w w  .  j  a  va  2 s  .  c  om*/
    fuh.setResourceLocation("file:///" + path);
    fuh.setUrlResourceMapping("files/");
    fuh.setUseDefaultPath(false);
    fuh.setFileMaxSize(env.getProperty(ENV_UPLOAD_IMAGESIZE, long.class, defaultImageMaxSize));
    fuh.setUnremovablePaths(
            Arrays.asList(env.getProperty(ENV_UNREMOVABLE_PATH, String[].class, new String[] {})));
    final String mimeTypesFilePath = env.getProperty(ENV_UPLOAD_FILE_AUTHORIZED_MIME_TYPE,
            defaultFileMimeTypes);
    try {
        List<String> list = Files.readAllLines(Paths.get(new ClassPathResource(mimeTypesFilePath).getURI()),
                Charsets.UTF_8);
        fuh.setAuthorizedMimeType(list);
    } catch (IOException e) {
        log.error("No file describing authorized MimeTypes is readable !", e);
        throw e;
    }
    log.debug("FileUploadConfiguration : " + fuh.toString());
    return fuh;
    /* Default way
    fuh.setUploadDirectoryPath(new ClassPathResource("classpath:static/").getPath());
    fuh.setResourceLocation("classpath:static/");
    fuh.setUrlResourceMapping("files/");
    fuh.setUseDefaultPath(true);
    fuh.setFileMaxSize(env.getProperty(ENV_UPLOAD_IMAGESIZE, long.class, DefaultImageMaxSize));
    log.debug("FileUploadConfiguration : " + fuh.toString());
    return fuh;*/
}

From source file:org.esupportail.publisher.config.FileUploadConfiguration.java

@Bean(name = "protectedFileUploadHelper")
public FileUploadHelper protectedFileUploadHelper() throws IOException, URISyntaxException {
    FileUploadHelper fuh = new FileUploadHelper();
    final String path = getDefinedUploadPath(ENV_UPLOAD_PROTECTED_PATH);
    Assert.notNull(path, "You should define a protected file path");

    fuh.setUploadDirectoryPath(path);/* ww  w . ja  v a2  s  .co m*/
    fuh.setResourceLocation("file:///" + path);
    fuh.setUrlResourceMapping(ViewController.FILE_VIEW.replaceFirst("/", ""));
    fuh.setUseDefaultPath(false);
    fuh.setFileMaxSize(env.getProperty(ENV_UPLOAD_FILESIZE, long.class, defaultFileMaxSize));
    fuh.setUnremovablePaths(
            Arrays.asList(env.getProperty(ENV_UNREMOVABLE_PATH, String[].class, new String[] {})));
    final String mimeTypesFilePath = env.getProperty(ENV_UPLOAD_FILE_AUTHORIZED_MIME_TYPE,
            defaultFileMimeTypes);
    try {
        List<String> list = Files.readAllLines(Paths.get(new ClassPathResource(mimeTypesFilePath).getURI()),
                Charsets.UTF_8);
        fuh.setAuthorizedMimeType(list);
    } catch (IOException e) {
        log.error("No file describing authorized MimeTypes is readable !", e);
        throw e;
    }
    log.debug("FileUploadConfiguration : " + fuh.toString());
    return fuh;
}

From source file:org.fenixedu.academic.ui.spring.controller.teacher.authorization.AuthorizationService.java

/***
 * Importation of teacher authorizations from a CSV file
 * //ww  w  . ja v a2s.  c o m
 * @param period the period where to import
 * @param partFile the CSV file
 * @return
 */
public List<TeacherAuthorization> importCSV(ExecutionSemester period, MultipartFile partFile) {
    try {
        return importAuthorizations(period,
                csvService.readCsvFile(partFile.getInputStream(), ",", Charsets.UTF_8.toString()));
    } catch (IOException e) {
        throw new RuntimeException(message("teacher.authorizations.upload.parsing.failed"));
    }
}

From source file:org.fenixedu.academic.ui.spring.controller.teacher.authorization.CsvService.java

public List<Map<String, String>> readCsvFile(InputStream stream, String separator, String encoding)
        throws IOException {
    try (InputStreamReader isr = new InputStreamReader(stream, Charsets.UTF_8)) {
        String content = CharStreams.toString(isr);
        List<String> lines = Splitter.on(System.lineSeparator()).splitToList(content);
        List<String> header = null;
        List<Map<String, String>> csvContent = new Vector<Map<String, String>>(lines.size());
        for (String line : lines) {
            String[] parts = line.split(separator);
            if (header == null) {
                header = new ArrayList<String>();
                for (String part : parts) {
                    header.add(cleanup(part));
                }/*  w w w  .j  a v  a  2 s  . c  o  m*/
            } else {
                int index = 0;
                Map<String, String> linemap = new HashMap<String, String>(header.size());
                for (String column : header) {
                    linemap.put(column, access(parts, index++));
                }
                csvContent.add(linemap);
            }
        }
        return csvContent;
    }
}

From source file:org.finra.dm.service.activiti.ActivitiHelper.java

/**
 * Constructs BPMN model from the XML string and validates
 *
 * @param xmlString the Activiti XML/* w w  w  . j  av a 2s. c o m*/
 *
 * @return the BPMN Model.
 * @throws UnsupportedEncodingException if the encoding wasn't supported.
 * @throws XMLStreamException if the XML couldn't be streamed.
 */
public BpmnModel constructBpmnModelFromXmlAndValidate(String xmlString)
        throws UnsupportedEncodingException, XMLStreamException {
    InputStreamSource source = new InputStreamSource(
            new ByteArrayInputStream(xmlString.trim().getBytes(Charsets.UTF_8)));
    return new BpmnXMLConverter().convertToBpmnModel(source, true, true);
}

From source file:org.finra.dm.service.helper.Hive13DdlGenerator.java

/**
 * Gets the DDL character value based on the specified configured character value. This method supports UTF-8 encoded strings and will "Hive" escape any
 * non-ASCII printable characters using '\(value)'.
 *
 * @param string the configured character value.
 * @param escapeSingleBackslash specifies if we need to escape a single backslash character with an extra backslash
 *
 * @return the DDL character value.//from   www.  ja v  a2  s  . c  o  m
 */
public String getDdlCharacterValue(String string, boolean escapeSingleBackslash) {
    // Assume the empty string for the return value.
    StringBuilder returnValueStringBuilder = new StringBuilder();

    // If we have an actual character, set the return value based on our rules.
    if (StringUtils.isNotEmpty(string)) {
        // Convert the string to UTF-8 so we can the proper characters that were sent via XML.
        String utf8String = new String(string.getBytes(Charsets.UTF_8), Charsets.UTF_8);

        // Loop through each character and add each one to the return value.
        for (int i = 0; i < utf8String.length(); i++) {
            // Default to the character itself.
            Character character = string.charAt(i);
            String nextValue = character.toString();

            // If the character isn't ASCII printable, then "Hive" escape it.
            if (!CharUtils.isAsciiPrintable(character)) {
                // If the character is unprintable, then display it as the ASCII octal value in \000 format.
                nextValue = String.format("\\%03o", (int) character);
            }

            // Add this character to the return value.
            returnValueStringBuilder.append(nextValue);
        }

        // Check if we need to escape a single backslash character with an extra backslash.
        if (escapeSingleBackslash && returnValueStringBuilder.toString().equals("\\")) {
            returnValueStringBuilder.append('\\');
        }
    }

    // Return the value.
    return returnValueStringBuilder.toString();
}

From source file:org.finra.dm.tools.common.databridge.DataBridgeManifestReader.java

/**
 * Reads a JSON manifest file into a JSON manifest object.
 *
 * @param jsonManifestFile the JSON manifest file.
 *
 * @return the manifest object./*  w  w  w  .  j a va 2s .  c  om*/
 * @throws java.io.IOException if any errors were encountered reading the JSON file.
 * @throws IllegalArgumentException if the manifest file has validation errors.
 */
public M readJsonManifest(File jsonManifestFile) throws IOException, IllegalArgumentException {
    // Verify that the file exists and can be read.
    DmFileUtils.verifyFileExistsAndReadable(jsonManifestFile);

    // Deserialize the JSON manifest.
    BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(jsonManifestFile));
    BufferedReader reader = new BufferedReader(new InputStreamReader(buffer, Charsets.UTF_8));
    ObjectMapper objectMapper = new ObjectMapper();
    M manifest = getManifestFromReader(reader, objectMapper);

    // Validate the manifest and return it.
    validateManifest(manifest);
    return manifest;
}

From source file:org.finra.herd.tools.common.databridge.DataBridgeManifestReader.java

/**
 * Reads a JSON manifest file into a JSON manifest object.
 *
 * @param jsonManifestFile the JSON manifest file.
 *
 * @return the manifest object./* w w  w .  j  a v  a  2 s. com*/
 * @throws java.io.IOException if any errors were encountered reading the JSON file.
 * @throws IllegalArgumentException if the manifest file has validation errors.
 */
public M readJsonManifest(File jsonManifestFile) throws IOException, IllegalArgumentException {
    // Verify that the file exists and can be read.
    HerdFileUtils.verifyFileExistsAndReadable(jsonManifestFile);

    // Deserialize the JSON manifest.
    BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(jsonManifestFile));
    BufferedReader reader = new BufferedReader(new InputStreamReader(buffer, Charsets.UTF_8));
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    objectMapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
    objectMapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE);
    M manifest = getManifestFromReader(reader, objectMapper);

    // Validate the manifest and return it.
    validateManifest(manifest);
    return manifest;
}

From source file:org.gbif.occurrence.parsing.response_file.ResponseSchemaDetectorTest.java

@Test
public void testAbcd1() throws IOException {
    String xml = Resources.toString(Resources.getResource("response_schema/abcd1.xml"), Charsets.UTF_8);
    OccurrenceSchemaType result = detector.detectSchema(xml);
    assertEquals(OccurrenceSchemaType.ABCD_1_2, result);
}