<commit_before>/**   
 * License Agreement for Jaeksoft OpenSearchServer
 *
 * Copyright (C) 2011 <NAME> / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of Jaeksoft OpenSearchServer.
 *
 * Jaeksoft OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * Jaeksoft OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Jaeksoft OpenSearchServer. 
 *  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.jaeksoft.searchlib.web;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import com.jaeksoft.searchlib.Client;
import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.crawler.web.database.CredentialItem;
import com.jaeksoft.searchlib.crawler.web.database.CredentialManager;
import com.jaeksoft.searchlib.crawler.web.screenshot.ScreenshotManager;
import com.jaeksoft.searchlib.user.Role;
import com.jaeksoft.searchlib.user.User;

public class ScreenshotServlet extends AbstractServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3693856378071358552L;

	private void doCapture(ServletTransaction transaction,
			ScreenshotManager screenshotManager,
			CredentialManager credentialManager, URL url)
			throws SearchLibException, MalformedURLException {
		if (!screenshotManager.getMethod().doScreenshot(url))
			throw new SearchLibException(
					"The capture is not allowed by the current method");
		CredentialItem credentialItem = credentialManager.getCredential(url
				.toExternalForm());
		screenshotManager.capture(url, credentialItem, true, 300);
		transaction.addXmlResponse("Status", "OK");
	}

	private void doImage(ServletTransaction transaction,
			ScreenshotManager screenshotManager, URL url)
			throws SearchLibException {
		File file = screenshotManager.getPngFile(url);
		if (file == null) {
			transaction.addXmlResponse("Error", "File not found");
			return;
		}
		StringBuffer sb = new StringBuffer(file.getName());
		File p = file.getParentFile();
		sb.insert(0, p.getName());
		p = p.getParentFile();
		sb.insert(0, p.getName());
		transaction.sendFile(file, sb.toString(), "image/png");
	}

	public final static String captureUrl(StringBuffer sbBaseUrl,
			Client client, User user, URL screenshotUrl)
			throws UnsupportedEncodingException {
		StringBuffer sb = getApiUrl(sbBaseUrl, "/screenshot", client, user);
		sb.append("&action=capture&url=");
		sb.append(URLEncoder.encode(screenshotUrl.toExternalForm(), "UTF-8"));
		return sb.toString();
	}

	public final static String imageUrl(StringBuffer sbBbaseUrl, Client client,
			User user, URL screenshotUrl) throws UnsupportedEncodingException {
		StringBuffer sb = getApiUrl(sbBbaseUrl, "/screenshot", client, user);
		sb.append("&action=image&url=");
		sb.append(URLEncoder.encode(screenshotUrl.toExternalForm(), "UTF-8"));
		return sb.toString();
	}

	@Override
	protected void doRequest(ServletTransaction transaction)
			throws ServletException {
		try {
			User user = transaction.getLoggedUser();
			if (user != null
					&& !user.hasRole(transaction.getIndexName(),
							Role.INDEX_QUERY))
				throw new SearchLibException("Not permitted");

			Client client = transaction.getClient();
			ScreenshotManager screenshotManager = client.getScreenshotManager();
			CredentialManager credentialManager = client
					.getWebCredentialManager();
			String action = transaction.getParameterString("action");
			URL url = new URL(transaction.getParameterString("url"));
			if ("capture".equalsIgnoreCase(action))
				doCapture(transaction, screenshotManager, credentialManager,
						url);
			if ("image".equalsIgnoreCase(action))
				doImage(transaction, screenshotManager, url);

		} catch (Exception e) {
			throw new ServletException(e);
		}
	}

}<commit_msg>Add the check action to the screenshot API<commit_after>/**   
 * License Agreement for Jaeksoft OpenSearchServer
 *
 * Copyright (C) 2011 <NAME> / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of Jaeksoft OpenSearchServer.
 *
 * Jaeksoft OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * Jaeksoft OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Jaeksoft OpenSearchServer. 
 *  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.jaeksoft.searchlib.web;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import com.jaeksoft.searchlib.Client;
import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.crawler.web.database.CredentialItem;
import com.jaeksoft.searchlib.crawler.web.database.CredentialManager;
import com.jaeksoft.searchlib.crawler.web.screenshot.ScreenshotManager;
import com.jaeksoft.searchlib.user.Role;
import com.jaeksoft.searchlib.user.User;

public class ScreenshotServlet extends AbstractServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3693856378071358552L;

	private void doCapture(ServletTransaction transaction,
			ScreenshotManager screenshotManager,
			CredentialManager credentialManager, URL url)
			throws SearchLibException, MalformedURLException {
		if (!screenshotManager.getMethod().doScreenshot(url))
			throw new SearchLibException(
					"The capture is not allowed by the current method");
		CredentialItem credentialItem = credentialManager.getCredential(url
				.toExternalForm());
		screenshotManager.capture(url, credentialItem, true, 300);
		transaction.addXmlResponse("Status", "OK");
	}

	final private String getPublicFileName(File file) {
		StringBuffer sb = new StringBuffer(file.getName());
		File p = file.getParentFile();
		sb.insert(0, p.getName());
		p = p.getParentFile();
		sb.insert(0, p.getName());
		return sb.toString();
	}

	private void doImage(ServletTransaction transaction,
			ScreenshotManager screenshotManager, URL url)
			throws SearchLibException {
		File file = screenshotManager.getPngFile(url);
		if (file == null) {
			transaction.addXmlResponse("Error", "File not found");
			return;
		}
		transaction.sendFile(file, getPublicFileName(file), "image/png");
	}

	private void doCheck(ServletTransaction transaction,
			ScreenshotManager screenshotManager, URL url)
			throws SearchLibException {
		File file = screenshotManager.getPngFile(url);
		if (file == null) {
			transaction.addXmlResponse("Error", "File not found");
			return;
		}
		transaction.addXmlResponse("Check", getPublicFileName(file));
	}

	public final static String captureUrl(StringBuffer sbBaseUrl,
			Client client, User user, URL screenshotUrl)
			throws UnsupportedEncodingException {
		StringBuffer sb = getApiUrl(sbBaseUrl, "/screenshot", client, user);
		sb.append("&action=capture&url=");
		sb.append(URLEncoder.encode(screenshotUrl.toExternalForm(), "UTF-8"));
		return sb.toString();
	}

	public final static String imageUrl(StringBuffer sbBbaseUrl, Client client,
			User user, URL screenshotUrl) throws UnsupportedEncodingException {
		StringBuffer sb = getApiUrl(sbBbaseUrl, "/screenshot", client, user);
		sb.append("&action=image&url=");
		sb.append(URLEncoder.encode(screenshotUrl.toExternalForm(), "UTF-8"));
		return sb.toString();
	}

	@Override
	protected void doRequest(ServletTransaction transaction)
			throws ServletException {
		try {
			User user = transaction.getLoggedUser();
			if (user != null
					&& !user.hasRole(transaction.getIndexName(),
							Role.INDEX_QUERY))
				throw new SearchLibException("Not permitted");

			Client client = transaction.getClient();
			ScreenshotManager screenshotManager = client.getScreenshotManager();
			CredentialManager credentialManager = client
					.getWebCredentialManager();
			String action = transaction.getParameterString("action");
			URL url = new URL(transaction.getParameterString("url"));
			if ("capture".equalsIgnoreCase(action))
				doCapture(transaction, screenshotManager, credentialManager,
						url);
			else if ("image".equalsIgnoreCase(action))
				doImage(transaction, screenshotManager, url);
			else if ("check".equalsIgnoreCase(action))
				doCheck(transaction, screenshotManager, url);

		} catch (Exception e) {
			throw new ServletException(e);
		}
	}

}
{'fraction_non_alphanumeric': 0.10120665289705402, 'fraction_numerical': 0.006739863028590064, 'mean_word_length': 4.943152454780361, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '2366932', 'n_tokens_mistral': 2939, 'n_tokens_neox': 2612, 'n_words': 710}