Java Code Examples for org.alfresco.service.cmr.repository.ContentReader#getEncoding()

The following examples show how to use org.alfresco.service.cmr.repository.ContentReader#getEncoding() . 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: OOoContentTransformerHelper.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Populates a file with the content in the reader, but also converts the encoding to UTF-8.
 */
private void saveContentInUtf8File(ContentReader reader, File file)
{
    String encoding = reader.getEncoding();
    try
    {
        Reader in = new InputStreamReader(reader.getContentInputStream(), encoding);
        Writer out = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file)), "UTF-8");

        FileCopyUtils.copy(in, out);  // both streams are closed
    }
    catch (IOException e)
    {
        throw new ContentIOException("Failed to copy content to file and convert "+encoding+" to UTF-8: \n" +
                "   file: " + file,
                e);
    }
}
 
Example 2
Source File: TextToPdfContentTransformer.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void transformRemote(RemoteTransformerClient remoteTransformerClient, ContentReader reader,
                               ContentWriter writer, TransformationOptions options, String sourceMimetype,
                               String targetMimetype, String sourceExtension, String targetExtension,
                               String targetEncoding) throws Exception
{
    String sourceEncoding = reader.getEncoding();
    long timeoutMs = options.getTimeoutMs();

    TransformationOptionLimits limits = getLimits(reader, writer, options);
    TransformationOptionPair pageLimits = limits.getPagesPair();
    int pageLimit = (int)pageLimits.getValue();

    remoteTransformerClient.request(reader, writer, sourceMimetype, sourceExtension, targetExtension,
            timeoutMs, logger,
            "transformName", "textToPdf",
            "sourceMimetype", sourceMimetype,
            "targetMimetype", targetMimetype,
            "targetExtension", targetExtension,
            SOURCE_ENCODING, sourceEncoding,
            "pageLimit", String.valueOf(pageLimit));
}
 
Example 3
Source File: StringExtractingContentTransformer.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void transformRemote(RemoteTransformerClient remoteTransformerClient, ContentReader reader,
                               ContentWriter writer, TransformationOptions options, String sourceMimetype,
                               String targetMimetype, String sourceExtension, String targetExtension,
                               String targetEncoding) throws Exception
{
    String sourceEncoding = reader.getEncoding();
    long timeoutMs = options.getTimeoutMs();

    remoteTransformerClient.request(reader, writer, sourceMimetype, sourceExtension, targetExtension,
            timeoutMs, logger,
            "transformName", "string",
            "sourceMimetype", sourceMimetype,
            "targetMimetype", targetMimetype,
            "targetExtension", targetExtension,
            SOURCE_ENCODING, sourceEncoding,
            TARGET_ENCODING, targetEncoding);

}
 
Example 4
Source File: HtmlParserContentTransformer.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void transformRemote(RemoteTransformerClient remoteTransformerClient, ContentReader reader,
                               ContentWriter writer, TransformationOptions options, String sourceMimetype,
                               String targetMimetype, String sourceExtension, String targetExtension,
                               String targetEncoding) throws Exception
{
    String sourceEncoding = reader.getEncoding();
    long timeoutMs = options.getTimeoutMs();

    remoteTransformerClient.request(reader, writer, sourceMimetype, sourceExtension, targetExtension,
            timeoutMs, logger,
            "transformName", "html",
            "sourceMimetype", sourceMimetype,
            "sourceExtension", sourceExtension,
            "targetMimetype", targetMimetype,
            SOURCE_ENCODING, sourceEncoding);
}
 
Example 5
Source File: LocalPassThroughTransform.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void transformImpl(ContentReader reader, ContentWriter writer, Map<String, String> transformOptions,
                             String sourceMimetype, String targetMimetype, String sourceExtension,
                             String targetExtension, String renditionName, NodeRef sourceNodeRef)
        throws UnsupportedTransformationException, ContentIOException
{
    if (isToText(sourceMimetype, targetMimetype))
    {
        // Set the encodings if specified.
        String sourceEncoding = reader.getEncoding();
        try (Reader charReader = sourceEncoding == null
                ? new InputStreamReader(reader.getContentInputStream())
                : new InputStreamReader(reader.getContentInputStream(), sourceEncoding))
        {
            String targetEncoding = writer.getEncoding();
            try (Writer charWriter = targetEncoding == null
                ? new OutputStreamWriter(writer.getContentOutputStream())
                : new OutputStreamWriter(writer.getContentOutputStream(), targetEncoding))
            {
                char[] buffer = new char[8192];
                int readCount = 0;
                while (readCount > -1)
                {
                    // write the last read count number of bytes
                    charWriter.write(buffer, 0, readCount);
                    // fill the buffer again
                    readCount = charReader.read(buffer);
                }
            }
        }
        catch (IOException e)
        {
            log.error(e);
        }
    }
    else // simple pass through
    {
        writer.putContent(reader.getContentInputStream());
    }
}
 
Example 6
Source File: OOoContentTransformerHelper.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Populates a file with the content in the reader.
 */
public void saveContentInFile(String sourceMimetype, ContentReader reader, File file) throws ContentIOException
{
    String encoding = reader.getEncoding();
    if (encodeAsUtf8(sourceMimetype, encoding))
    {
        saveContentInUtf8File(reader, file);
    }
    else
    {
        reader.getContent(file);
    }
}
 
Example 7
Source File: HtmlParserContentTransformer.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void transformLocal(ContentReader reader, ContentWriter writer, TransformationOptions options)
        throws Exception
{

    // We can only work from a file
    File htmlFile = TempFileProvider.createTempFile("HtmlParserContentTransformer_", ".html");
    reader.getContent(htmlFile);

    // Fetch the encoding of the HTML, as set in the ContentReader
    // This will default to 'UTF-8' if not specifically set
    String encoding = reader.getEncoding();

    // Create the extractor
    EncodingAwareStringBean extractor = new EncodingAwareStringBean();
    extractor.setCollapse(false);
    extractor.setLinks(false);
    extractor.setReplaceNonBreakingSpaces(false);
    extractor.setURL(htmlFile, encoding);
    // get the text
    String text = extractor.getStrings();
    // write it to the writer
    writer.putContent(text);

    // Tidy up
    htmlFile.delete();
}
 
Example 8
Source File: MessageServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ResourceBundle getRepoResourceBundle(
        final StoreRef storeRef,
        final String path,
        final Locale locale) throws IOException
{   
    // TODO - need to replace basic strategy with a more complete
    // search & instantiation strategy similar to ResourceBundle.getBundle()
    // Consider search query with basename* and then apply strategy ...
    
    // Avoid permission exceptions
    RunAsWork<ResourceBundle> getBundleWork = new RunAsWork<ResourceBundle>()
    {
        @Override
        public ResourceBundle doWork() throws Exception
        {
            NodeRef rootNode = nodeService.getRootNode(storeRef);

            // first attempt - with locale        
            NodeRef nodeRef = getNode(rootNode, path+"_"+locale+PROPERTIES_FILE_SUFFIX);
            
            if (nodeRef == null)
            {
                // second attempt - basename 
                nodeRef = getNode(rootNode, path+PROPERTIES_FILE_SUFFIX);
            }
            
            if (nodeRef == null)
            {
                logger.debug("Could not find message resource bundle " + storeRef + "/" + path);
                return null;
            }
            
            ContentReader cr = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
            ResourceBundle resBundle = new MessagePropertyResourceBundle(
                    new InputStreamReader(cr.getContentInputStream(), cr.getEncoding()));
            return resBundle;
        }
    };
    return AuthenticationUtil.runAs(getBundleWork, AuthenticationUtil.getSystemUserName());
}
 
Example 9
Source File: RepoStore.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Reader getReader()
{
    ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
    try
    {
        return new InputStreamReader(getInputStream(), reader.getEncoding());
    }
    catch (UnsupportedEncodingException e)
    {
        throw new AlfrescoRuntimeException("Unsupported Encoding", e);
    }
}
 
Example 10
Source File: BinaryProperty.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * This is the preferred constructor to use. Takes the properties from content reader that it needs.
 * @param reader ContentReader
 */
public BinaryProperty(ContentReader reader)
{
    super();
    this.mimeType = reader.getMimetype();
    this.encoding = reader.getEncoding();
    this.length = reader.getSize();
    this.locale = reader.getLocale();
}
 
Example 11
Source File: LocalTransformImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected void transformImpl(ContentReader reader,
                             ContentWriter writer, Map<String, String> transformOptions,
                             String sourceMimetype, String targetMimetype,
                             String sourceExtension, String targetExtension,
                             String renditionName, NodeRef sourceNodeRef)
{
    transformOptions = new HashMap<>(transformOptions);
    // Dynamic transform options
    String sourceEncoding = reader.getEncoding();
    transformOptions.put(SOURCE_ENCODING, sourceEncoding);
    if (transformOptions.containsKey(SOURCE_NODE_REF) && transformOptions.get(SOURCE_NODE_REF) == null)
    {
        transformOptions.put(SOURCE_NODE_REF, sourceNodeRef.toString());
    }

    // Build an array of option names and values and extract the timeout.
    long timeoutMs = 0;
    int nonOptions = transformOptions.containsKey(RenditionDefinition2.TIMEOUT) ? 1 : 0;
    int size = (transformOptions.size() - nonOptions + 3) * 2;
    String[] args = new String[size];
    int i = 0;
    for (Map.Entry<String, String> option : transformOptions.entrySet())
    {
        String name = option.getKey();
        String value = option.getValue();
        if (RenditionDefinition2.TIMEOUT.equals(name))
        {
            if (value != null)
            {
                timeoutMs = Long.parseLong(value);
            }
        }
        else
        {
            args[i++] = name;
            args[i++] = value;
        }
    }

    // These 3 values are commonly needed and are always supplied in the TransformRequest (message to the T-Router).
    // The targetExtension is also supplied in the TransformRequest, but in the case of local and legacy transformers
    // is added by the remoteTransformerClient.request call for historic reasons, so does not need to be added here.
    args[i++] = "sourceMimetype";
    args[i++] = sourceMimetype;
    args[i++] = "sourceExtension";
    args[i++] = sourceExtension;
    args[i++] = "targetMimetype";
    args[i++] = targetMimetype;

    remoteTransformerClient.request(reader, writer, sourceMimetype, sourceExtension, targetExtension,
            timeoutMs, log, args);
}