<commit_before>/*
 * Copyright  1999-2005 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.lenya.cms.editors;

import java.util.ArrayList;
import java.util.List;

import org.apache.lenya.cms.publication.Resource;
import org.apache.lenya.cms.publication.ResourcesManager;
import org.apache.lenya.cms.usecase.DocumentUsecase;
import org.apache.lenya.cms.usecase.UsecaseException;
import org.apache.lenya.cms.usecase.UsecaseInvoker;

/**
 * Usecase to insert an image into a document.
 * 
 * @version $Id$
 */
public class InsertAsset extends DocumentUsecase {

    /**
     * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
     */
    protected void initParameters() {
        super.initParameters();
        loadResources();
    }

    protected void loadResources() {
        ResourcesManager resourcesManager = null;

        try {
            resourcesManager = (ResourcesManager) this.manager.lookup(ResourcesManager.ROLE);
            Resource[] resources = resourcesManager.getResources(getSourceDocument());

            List selectedResources = new ArrayList();
            String mimeTypePrefix = getParameterAsString("mimeTypePrefix", "");
            for (int i = 0; i < resources.length; i++) {
                if (resources[i].getMimeType().startsWith(mimeTypePrefix)) {
                    selectedResources.add(resources[i]);
                }
            }

            setParameter("assets", selectedResources);
        } catch (final Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (resourcesManager != null) {
                this.manager.release(resourcesManager);
            }
        }
    }

    /**
     * Delegates to the main assets usecase; the name of
     * the usecase being delegated to is set in the
     * configuration parameter "asset-usecase".
     *
     * @see org.apache.lenya.cms.usecase.Usecase#advance()
     */
    public void advance() throws UsecaseException {
        super.advance();
        if (getParameter("file") != null) {
            UsecaseInvoker invoker = null;
            try {
                invoker = (UsecaseInvoker) this.manager.lookup(UsecaseInvoker.ROLE);
                String usecaseName = getParameterAsString("asset-usecase");

                if (getLogger().isDebugEnabled())
                    getLogger().debug("InsertAsset::advance() calling invoker with usecaseName [" + usecaseName + "]");
                invoker.invoke(getSourceURL(), usecaseName, getParameters());
                loadResources();
            }
            catch (Exception e) {
                throw new UsecaseException(e);
            } finally {
                if (invoker != null) {
                    this.manager.release(invoker);
                }
            }
        }
    }

}
<commit_msg>added check to see if resource.getMimeType() returns null, resolves Bug 35759<commit_after>/*
 * Copyright  1999-2005 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.lenya.cms.editors;

import java.util.ArrayList;
import java.util.List;

import org.apache.lenya.cms.publication.Resource;
import org.apache.lenya.cms.publication.ResourcesManager;
import org.apache.lenya.cms.usecase.DocumentUsecase;
import org.apache.lenya.cms.usecase.UsecaseException;
import org.apache.lenya.cms.usecase.UsecaseInvoker;

/**
 * Usecase to insert an image into a document.
 * 
 * @version $Id$
 */
public class InsertAsset extends DocumentUsecase {

    /**
     * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
     */
    protected void initParameters() {
        super.initParameters();
        loadResources();
    }

    protected void loadResources() {
        ResourcesManager resourcesManager = null;

        try {
            resourcesManager = (ResourcesManager) this.manager.lookup(ResourcesManager.ROLE);
            Resource[] resources = resourcesManager.getResources(getSourceDocument());

            List selectedResources = new ArrayList();
            String mimeTypePrefix = getParameterAsString("mimeTypePrefix", "");
            for (int i = 0; i < resources.length; i++) {
                String resMimeType = resources[i].getMimeType();
                if(resMimeType == null)
                    resMimeType = "unknown";
                if (resMimeType.startsWith(mimeTypePrefix)) {
                    selectedResources.add(resources[i]);
                }
            }

            setParameter("assets", selectedResources);
        } catch (final Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (resourcesManager != null) {
                this.manager.release(resourcesManager);
            }
        }
    }

    /**
     * Delegates to the main assets usecase; the name of
     * the usecase being delegated to is set in the
     * configuration parameter "asset-usecase".
     *
     * @see org.apache.lenya.cms.usecase.Usecase#advance()
     */
    public void advance() throws UsecaseException {
        super.advance();
        if (getParameter("file") != null) {
            UsecaseInvoker invoker = null;
            try {
                invoker = (UsecaseInvoker) this.manager.lookup(UsecaseInvoker.ROLE);
                String usecaseName = getParameterAsString("asset-usecase");

                if (getLogger().isDebugEnabled())
                    getLogger().debug("InsertAsset::advance() calling invoker with usecaseName [" + usecaseName + "]");
                invoker.invoke(getSourceURL(), usecaseName, getParameters());
                loadResources();
            }
            catch (Exception e) {
                throw new UsecaseException(e);
            } finally {
                if (invoker != null) {
                    this.manager.release(invoker);
                }
            }
        }
    }

}

{'fraction_non_alphanumeric': 0.09969918349806618, 'fraction_numerical': 0.004440624552356396, 'mean_word_length': 2.293396226415094, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '132651', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1841, 'n_words': 481}