<commit_before>/***
 * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
 * All rights reserved.
 *
 * 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 br.com.caelum.vraptor.observer.download;

import static org.slf4j.LoggerFactory.getLogger;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.enterprise.event.Observes;
import javax.inject.Inject;

import org.slf4j.Logger;

import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.events.MethodExecuted;

/**
 * Observer that return a File or an InputStream when method return type is a download type.
 *
 * @author filipesabella
 * @author <NAME>
 */
public class DownloadObserver {

	private static final Logger logger = getLogger(DownloadObserver.class);

	private final Result result;

	/**
	 * @deprecated CDI eyes only
	 */
	protected DownloadObserver() {
		this(null);
	}

	@Inject
	public DownloadObserver(Result result) {
		this.result = result;
	}

	public void download(@Observes MethodExecuted event) throws IOException  {

		Object methodResult = event.getMethodInfo().getResult();
		Download download = resolveDownload(methodResult);

		if (download != null) {
			logger.debug("Sending a file to the client");
			if (this.result.used()) return;
			this.result.use(DownloadView.class).of(download);
		}
	}

	public Download resolveDownload(Object result) throws IOException {

		if (result == null) return null;

		if (result instanceof InputStream) {
			return new InputStreamDownload((InputStream) result, null, null);
		}<commit_msg>DownloadObserver are now application scoped<commit_after>/***
 * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
 * All rights reserved.
 *
 * 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 br.com.caelum.vraptor.observer.download;

import static org.slf4j.LoggerFactory.getLogger;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;

import org.slf4j.Logger;

import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.events.MethodExecuted;

/**
 * Observer that return a File or an InputStream when method return type is a download type.
 *
 * @author filipesabella
 * @author <NAME>
 */
@ApplicationScoped
public class DownloadObserver {

	private static final Logger logger = getLogger(DownloadObserver.class);

	public void download(@Observes MethodExecuted event, Result result) throws IOException  {

		Object methodResult = event.getMethodInfo().getResult();
		Download download = resolveDownload(methodResult);

		if (download != null) {
			logger.debug("Sending a file to the client");
			if (result.used()) return;
			result.use(DownloadView.class).of(download);
		}
	}

	public Download resolveDownload(Object result) throws IOException {

		if (result == null) return null;

		if (result instanceof InputStream) {
			return new InputStreamDownload((InputStream) result, null, null);
		}
{'fraction_non_alphanumeric': 0.09504851953222195, 'fraction_numerical': 0.004976362279173924, 'mean_word_length': 4.381526104417671, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 31, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2502088', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1126, 'n_words': 392}