<commit_before>/*
 * C5Connector.Java - The Java backend for the filemanager of corefive.
 * It's a bridge between the filemanager and a storage backend and 
 * works like a transparent VFS or proxy.
 * Copyright (C) <NAME>
 * 
 * == BEGIN LICENSE ==
 * 
 * Licensed under the terms of any of the following licenses at your
 * choice:
 * 
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 * 
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 * 
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * == END LICENSE ==
 */
package de.thischwa.c5c.requestcycle;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.thischwa.c5c.FilemanagerAction;
import de.thischwa.c5c.UserObjectProxy;
import de.thischwa.c5c.exception.C5CException;
import de.thischwa.c5c.exception.FilemanagerException;

/**
 * Maintains the base request parameter.
 */
public class Context {
	private static Logger logger = LoggerFactory.getLogger(Context.class);

	/** action of the filemanager */
	private FilemanagerAction mode;
	
	/** the given path from url pram */
	private String urlPath;

	private HttpServletRequest servletRequest;

	Context(HttpServletRequest servletRequest) throws C5CException {
		this.servletRequest = servletRequest;
		urlPath = servletRequest.getParameter("path");
		String paramMode;
		if(servletRequest.getMethod().equals("post")) {
			try {
				paramMode = IOUtils.toString(servletRequest.getPart("mode").getInputStream());
			} catch (Exception e) {
				logger.error("Couldn't retrieve the 'mode' parameter from multipart.");
				throw new C5CException(UserObjectProxy.getFilemanagerErrorMessage(FilemanagerException.Key.ModeError));
			}
		} else {
			paramMode = servletRequest.getParameter("mode");
		}
		if (paramMode == null)
			throw new IllegalArgumentException("Missing 'mode' parameter.");
		try {
			mode = FilemanagerAction.valueOfIgnoreCase(paramMode);
		} catch (IllegalArgumentException e) {
			logger.error("Unknown 'mode': {}", mode);
			throw new C5CException(UserObjectProxy.getFilemanagerErrorMessage(FilemanagerException.Key.ModeError));
		}
	}
	
	/**
	 * Gets the mode (the action of the filemanager).
	 * <commit_msg>fixed: method name must be uppercase<commit_after>/*
 * C5Connector.Java - The Java backend for the filemanager of corefive.
 * It's a bridge between the filemanager and a storage backend and 
 * works like a transparent VFS or proxy.
 * Copyright (C) <NAME>
 * 
 * == BEGIN LICENSE ==
 * 
 * Licensed under the terms of any of the following licenses at your
 * choice:
 * 
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 * 
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 * 
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * == END LICENSE ==
 */
package de.thischwa.c5c.requestcycle;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.thischwa.c5c.FilemanagerAction;
import de.thischwa.c5c.UserObjectProxy;
import de.thischwa.c5c.exception.C5CException;
import de.thischwa.c5c.exception.FilemanagerException;

/**
 * Maintains the base request parameter.
 */
public class Context {
	private static Logger logger = LoggerFactory.getLogger(Context.class);

	/** action of the filemanager */
	private FilemanagerAction mode;
	
	/** the given path from url pram */
	private String urlPath;

	private HttpServletRequest servletRequest;

	Context(HttpServletRequest servletRequest) throws C5CException {
		this.servletRequest = servletRequest;
		urlPath = servletRequest.getParameter("path");
		String paramMode;
		if(servletRequest.getMethod().equals("POST")) {
			try {
				paramMode = IOUtils.toString(servletRequest.getPart("mode").getInputStream());
			} catch (Exception e) {
				logger.error("Couldn't retrieve the 'mode' parameter from multipart.");
				throw new C5CException(UserObjectProxy.getFilemanagerErrorMessage(FilemanagerException.Key.ModeError));
			}
		} else {
			paramMode = servletRequest.getParameter("mode");
		}
		if (paramMode == null)
			throw new IllegalArgumentException("Missing 'mode' parameter.");
		try {
			mode = FilemanagerAction.valueOfIgnoreCase(paramMode);
		} catch (IllegalArgumentException e) {
			logger.error("Unknown 'mode': {}", mode);
			throw new C5CException(UserObjectProxy.getFilemanagerErrorMessage(FilemanagerException.Key.ModeError));
		}
	}
	
	/**
	 * Gets the mode (the action of the filemanager).
	 * 
{'fraction_non_alphanumeric': 0.11320368474923234, 'fraction_numerical': 0.0077789150460593655, 'mean_word_length': 4.22008547008547, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '7554868', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1462, 'n_words': 383}