<commit_before>/**
 * Copyright 2010 JBoss Inc
 *
 * 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.drools.repository.remoteapi;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import org.drools.repository.AssetHistoryIterator;
import org.drools.repository.AssetItem;
import org.drools.repository.PackageItem;
import org.drools.repository.RulesRepository;
import org.drools.repository.RulesRepositoryException;
import org.drools.repository.remoteapi.Response.Binary;
import org.drools.repository.remoteapi.Response.Text;

/**
 * This provides a simple REST style remote friendly API.
 *
 * @author <NAME>
 *
 */
public class RestAPI {

	private final RulesRepository repo;

	private static Properties TEXT_ASSET_TYPES = loadAssetTypes();

	public RestAPI(RulesRepository repo) {
		this.repo = repo;
	}

	private static Properties loadAssetTypes() {
		Properties p = new Properties();
		try {
			p.load(RestAPI.class.getResourceAsStream("text_assets.properties"));
		} catch (IOException e) {
			System.err.println("Unable to load asset text types properties.");
			return null;
		}
		return p;
	}

	/**
	 * This works off:
	 * packages/packageName --> returns list of : asset name=ISO date time last modified,versionNumber
	 * packages/packageName/.package --> the contents of the package header
	 * packages/packageName/assetName --> the contents of the package
	 */
	public Response get(String path) throws UnsupportedEncodingException {
		String[] bits = split(path);
		if (bits.length == 1) return new Response.Text("");
		if (bits[0].equals("packages")) {
			String pkgName = bits[1];
			if (bits.length == 2) {
				return listPackage(pkgName);
			} else {
				String resourceFile = bits[2];
				return loadContent(pkgName, resourceFile);
			}
		} else {
			return new Response.Text("");//throw new IllegalArgumentException("Unable to deal with " + path);
		}
<commit_msg>findbugs: may fail to clean up java.io.InputStream / mail fail to close stream + do not eat exceptions<commit_after>/**
 * Copyright 2010 JBoss Inc
 *
 * 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.drools.repository.remoteapi;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import org.drools.repository.AssetHistoryIterator;
import org.drools.repository.AssetItem;
import org.drools.repository.PackageItem;
import org.drools.repository.RulesRepository;
import org.drools.repository.RulesRepositoryException;
import org.drools.repository.remoteapi.Response.Binary;
import org.drools.repository.remoteapi.Response.Text;
import org.drools.repository.utils.IOUtils;

/**
 * This provides a simple REST style remote friendly API.
 *
 * @author <NAME>
 *
 */
public class RestAPI {

	private final RulesRepository repo;

	private static Properties TEXT_ASSET_TYPES = loadAssetTypes();

	public RestAPI(RulesRepository repo) {
		this.repo = repo;
	}

	private static Properties loadAssetTypes() {
        InputStream in = null;
		try {
            in = RestAPI.class.getResourceAsStream("text_assets.properties");
		    Properties p = new Properties();
            p.load(in);
            return p;
		} catch (IOException e) {
			throw new IllegalStateException("Unable to load asset text types properties.", e);
		} finally {
            IOUtils.closeQuietly(in);
        }
	}

	/**
	 * This works off:
	 * packages/packageName --> returns list of : asset name=ISO date time last modified,versionNumber
	 * packages/packageName/.package --> the contents of the package header
	 * packages/packageName/assetName --> the contents of the package
	 */
	public Response get(String path) throws UnsupportedEncodingException {
		String[] bits = split(path);
		if (bits.length == 1) return new Response.Text("");
		if (bits[0].equals("packages")) {
			String pkgName = bits[1];
			if (bits.length == 2) {
				return listPackage(pkgName);
			} else {
				String resourceFile = bits[2];
				return loadContent(pkgName, resourceFile);
			}
		} else {
			return new Response.Text("");//throw new IllegalArgumentException("Unable to deal with " + path);
		}

{'fraction_non_alphanumeric': 0.10035460992907802, 'fraction_numerical': 0.004609929078014184, 'mean_word_length': 4.100361663652803, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '4560887', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1536, 'n_words': 507}