Java Code Examples for org.alfresco.repo.content.MimetypeMap#MIMETYPE_TEXT_PLAIN

The following examples show how to use org.alfresco.repo.content.MimetypeMap#MIMETYPE_TEXT_PLAIN . 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: ContentDataDAOTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Ensure that upper and lowercase URLs don't clash
 * @throws Exception
 */
public void testEnsureCaseSensitiveStorage() throws Exception
{
    ContentData contentData = getContentData();
    String contentUrlUpper = contentData.getContentUrl().toUpperCase();
    ContentData contentDataUpper = new ContentData(
            contentUrlUpper, MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, "UTF-8", new Locale("FR"));
    String contentUrlLower = contentData.getContentUrl().toLowerCase();
    ContentData contentDataLower = new ContentData(
            contentUrlLower, MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, "utf-8", new Locale("fr"));
    
    Pair<Long, ContentData> resultPairUpper = create(contentDataUpper);
    getAndCheck(resultPairUpper.getFirst(), contentDataUpper);
    
    Pair<Long, ContentData> resultPairLower = create(contentDataLower);
    getAndCheck(resultPairLower.getFirst(), contentDataLower);
}
 
Example 2
Source File: ContentStoreCleanerScalabilityRunner.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void loadData(final int maxCount)
{
    final MutableInt doneCount = new MutableInt(0);
    // Batches of 1000 objects
    RetryingTransactionCallback<Integer> makeNodesCallback = new RetryingTransactionCallback<Integer>()
    {
        public Integer execute() throws Throwable
        {
            for (int i = 0; i < 1000; i++)
            {
                // We don't need to write anything
                String contentUrl = FileContentStore.createNewFileStoreUrl();
                ContentData contentData = new ContentData(contentUrl, MimetypeMap.MIMETYPE_TEXT_PLAIN, 10, "UTF-8");
                nodeHelper.makeNode(contentData);
                
                int count = doneCount.intValue();
                count++;
                doneCount.setValue(count);
                
                // Do some reporting
                if (count % 1000 == 0)
                {
                    System.out.println(String.format("   " + (new Date()) + "Total created: %6d", count));
                }
                
                // Double check for shutdown
                if (vmShutdownListener.isVmShuttingDown())
                {
                    break;
                }
            }
            return maxCount;
        }
    };
    int repetitions = (int) Math.floor((double)maxCount / 1000.0);
    for (int i = 0; i < repetitions; i++)
    {
        transactionService.getRetryingTransactionHelper().doInTransaction(makeNodesCallback);
    }
}
 
Example 3
Source File: ContentDataDAOTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ContentData getContentData()
{
    ContentContext contentCtx = new ContentContext(null, null);
    String contentUrl = contentStore.getWriter(contentCtx).getContentUrl();
    ContentData contentData = new ContentData(
            contentUrl,
            MimetypeMap.MIMETYPE_TEXT_PLAIN,
            12335L,
            "UTF-8",
            Locale.FRENCH);
    return contentData;
}
 
Example 4
Source File: OpenCmisLocalTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private ContentStreamImpl makeContentStream(String filename, String mimetype, String content) throws IOException
{
    TempStoreOutputStream tos = streamFactory.newOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(tos);
    writer.write(content);
    ContentStreamImpl contentStream = new ContentStreamImpl(filename, BigInteger.valueOf(tos.getLength()), MimetypeMap.MIMETYPE_TEXT_PLAIN, tos.getInputStream());
    return contentStream;
}
 
Example 5
Source File: FolderEmailMessageHandler.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Add content to Alfresco repository
 * 
 * @param spaceNodeRef          Addressed node
 * @param message            Mail message
 * @throws IOException          Exception can be thrown while saving a content into Alfresco repository.
 */
public void addAlfrescoContent(NodeRef spaceNodeRef, EmailMessage message) throws IOException
{
    // Set default values for email fields
    String messageSubject = message.getSubject();
    if (messageSubject == null || messageSubject.length() == 0)
    {
        Date now = new Date();
        messageSubject = I18NUtil.getMessage(MSG_DEFAULT_SUBJECT, new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss").format(now));
    }
    
    String messageFrom = message.getFrom();
    if(messageFrom == null)
    {
        messageFrom = "";
    }

    // Create main content node
    NodeRef contentNodeRef;
    contentNodeRef = addContentNode(getNodeService(), spaceNodeRef, messageSubject, overwriteDuplicates);
    // Add titled aspect
    addTitledAspect(contentNodeRef, messageSubject, messageFrom);
    // Add emailed aspect
    addEmailedAspect(contentNodeRef, message);
    // Write the message content
    if (message.getBody() != null)
    {
        if (message.getBody().getSize() == -1)
        {
            // If message body is empty we write space as a content
            // to make possible rule processing
            // (Rules don't work on empty documents)
            writeSpace(contentNodeRef);
        }
        else
        {
            InputStream contentIs = message.getBody().getContent();
            // The message body is plain text, unless an extension has been provided
            MimetypeService mimetypeService = getMimetypeService();
            String mimetype = mimetypeService.guessMimetype(messageSubject);
            if (mimetype.equals(MimetypeMap.MIMETYPE_BINARY))
            {
                mimetype = MimetypeMap.MIMETYPE_TEXT_PLAIN;
            }
            // Use the default encoding. It will get overridden if the body is text.
            String encoding = message.getBody().getEncoding();

            writeContent(contentNodeRef, contentIs, mimetype, encoding);
        }
    }

    // Add attachments
    addAttachments(spaceNodeRef, contentNodeRef, message);
}
 
Example 6
Source File: SpoofedTextContentReader.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @param url           a URL describing the type of text to produce (see class comments)
 */
public SpoofedTextContentReader(String url)
{
    super(url);
    if (url.length() > 255)
    {
        throw new IllegalArgumentException("A content URL is limited to 255 characters: " + url);
    }
    // Split out data part
    int index = url.indexOf(ContentStore.PROTOCOL_DELIMITER);
    if (index <= 0 || !url.startsWith(FileContentStore.SPOOF_PROTOCOL))
    {
        throw new RuntimeException("URL not supported by this reader: " + url);
    }
    String urlData = url.substring(index + 3, url.length());
    // Parse URL
    try
    {
        JSONParser parser = new JSONParser();
        JSONObject mappedData = (JSONObject) parser.parse(urlData);

        String jsonLocale = mappedData.containsKey(KEY_LOCALE) ? (String) mappedData.get(KEY_LOCALE) : Locale.ENGLISH.toString();
        String jsonSeed = mappedData.containsKey(KEY_SEED) ? (String) mappedData.get(KEY_SEED) : "0";
        String jsonSize = mappedData.containsKey(KEY_SIZE) ? (String) mappedData.get(KEY_SIZE) : "1024";
        JSONArray jsonWords = mappedData.containsKey(KEY_WORDS) ? (JSONArray) mappedData.get(KEY_WORDS) : new JSONArray();
        // Get the text generator
        Locale locale = new Locale(jsonLocale);
        seed = Long.valueOf(jsonSeed);
        size = Long.valueOf(jsonSize);
        words = new String[jsonWords.size()];
        for (int i = 0; i < words.length; i++)
        {
            words[i] = (String) jsonWords.get(i);
        }
        this.textGenerator = SpoofedTextContentReader.getTextGenerator(locale);
        // Set the base class storage for external information
        super.setLocale(locale);
        super.setEncoding("UTF-8");
        super.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
    }
    catch (Exception e)
    {
        throw new RuntimeException("Unable to interpret URL: " + url, e);
    }
    
}
 
Example 7
Source File: PdfBoxContentTransformerTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Added to test a single transform that appeared to have problems.
 * Commented out once issue was fixed, but left in the code to help with
 * future issues.
 * @throws Exception
 */
public void testPdfToTextConversions() throws Exception
{
    final String sourceMimetype = MimetypeMap.MIMETYPE_PDF;
    final String targetMimetype = MimetypeMap.MIMETYPE_TEXT_PLAIN;
    int transforms = 100;
    final String filename = "svn-book.pdf";
    
    final CountDownLatch doneSignal = new CountDownLatch(transforms);
    
    int threadCount = 8;
    final ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
    long time = System.currentTimeMillis();
    for (int i=0; i<transforms; i++)
        
    {
        threadPool.submit(new Runnable() {
            public void run()
            {
                try
                {
                    pdfToTextTransform(filename, sourceMimetype, targetMimetype);
                    doneSignal.countDown();
                }
                catch (IOException e)
                {
                    threadPool.shutdown();
                    e.printStackTrace();
                }
            }});
        if (i < threadCount)
        {
            Thread.sleep(1000);
        }
    }
    boolean okay = doneSignal.await(100, TimeUnit.SECONDS);
    
    time = System.currentTimeMillis() - time;
    transforms = transforms - (int)doneSignal.getCount();
    String message = "Total time "+time+" ms   "+(transforms > 0 ? "average="+(time/transforms)+" ms" : "")+"  threads="+threadCount+"  transforms="+transforms;
    System.out.println(message);
    
    if (!okay)
    {
        // Before the changes to PDFBox, this would fail having only done about 50 transforms.
        // After the change, this takes about 55 seconds
        fail("********** Transforms did not finish ********** "+message);
    }
}
 
Example 8
Source File: SubscriptionServiceActivitiesTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected NodeRef addTextContent(String siteId, String name)
{
    String textData = name;
    String mimeType = MimetypeMap.MIMETYPE_TEXT_PLAIN;
            
    Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>();
    contentProps.put(ContentModel.PROP_NAME, name);
    
    // ensure that the Document Library folder is pre-created so that test code can start creating content straight away.
    // At the time of writing V4.1 does not create this folder automatically, but Thor does
    NodeRef parentRef = siteService.getContainer(siteId, SiteService.DOCUMENT_LIBRARY);
    if (parentRef == null)
    {
        parentRef = siteService.createContainer(siteId, SiteService.DOCUMENT_LIBRARY, ContentModel.TYPE_FOLDER, null);
    }
    
    ChildAssociationRef association = nodeService.createNode(parentRef,
            ContentModel.ASSOC_CONTAINS,
            QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, name),
            ContentModel.TYPE_CONTENT,
            contentProps);
    
    NodeRef content = association.getChildRef();
    
    // add titled aspect (for Web Client display)
    Map<QName, Serializable> titledProps = new HashMap<QName, Serializable>();
    titledProps.put(ContentModel.PROP_TITLE, name);
    titledProps.put(ContentModel.PROP_DESCRIPTION, name);
    nodeService.addAspect(content, ContentModel.ASPECT_TITLED, titledProps);
    
    ContentWriter writer = contentService.getWriter(content, ContentModel.PROP_CONTENT, true);
    
    writer.setMimetype(mimeType);
    writer.setEncoding("UTF-8");
    
    writer.putContent(textData);
    
    activityService.postActivity("org.alfresco.documentlibrary.file-added", siteId, "documentlibrary", content, name, ContentModel.PROP_CONTENT, parentRef);
    
    return content;
}
 
Example 9
Source File: TransformActionExecuterTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void transformName() throws Exception
{
    MimetypeService dummyMimetypeService = new DummyMimetypeService("txt");
    
    // Case 1. A simple name with a reasonable extension.
    String original = "Letter to Bank Manager.doc";
    final String newMimetype = MimetypeMap.MIMETYPE_TEXT_PLAIN;
    String newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, true);
    assertEquals("Letter to Bank Manager.txt", newName);

    // Case 2. String after '.' is clearly not an extension
    original = "No.1 - First Document Title";
    newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, true);
    assertEquals(original + ".txt", newName);

    // Case 2b. String after '.' is clearly not an extension. Don't always add
    original = "No.1 - First Document Title";
    newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, false);
    assertEquals(original, newName);

    // Case 3. A name with no extension
    original = "Letter to Bank Manager";
    newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, true);
    assertEquals(original + ".txt", newName);
    
    // Case 3b. A name with no extension. Don't always add
    original = "Letter to Bank Manager";
    newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, false);
    assertEquals(original, newName);
    
    // Case 4. A name ending in a dot
    original = "Letter to Bank Manager.";
    newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, true);
    assertEquals(original + "txt", newName);
    
    // Case 4b. A name ending in a dot. Don't always add
    original = "Letter to Bank Manager.";
    newName = TransformActionExecuter.transformName(dummyMimetypeService, original, newMimetype, false);
    assertEquals(original, newName);
    
    // Case 5. Unknown mime type
    original = "Letter to Bank Manager.txt";
    final String unknownMimetype = "Marcel/Marceau";
    // The real MimetypeService returns a 'bin' extension for unknown mime types.
    dummyMimetypeService = new DummyMimetypeService("bin");
    
    newName = TransformActionExecuter.transformName(dummyMimetypeService, original, unknownMimetype, true);
    assertEquals("Letter to Bank Manager.bin", newName);
}