<commit_before>/* Copyright 2010 University of Cambridge
 * Licensed under the Educational Community License (ECL), Version 2.0. You may not use this file except in 
 * compliance with this License.
 *
 * You may obtain a copy of the ECL 2.0 License at https://source.collectionspace.org/collection-space/LICENSE.txt
 */
package org.collectionspace.chain.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.fileupload.FileItemHeaders;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.*;
import org.collectionspace.csp.api.ui.Operation;
import org.collectionspace.csp.api.ui.TTYOutputter;
import org.collectionspace.csp.api.ui.UIException;
import org.collectionspace.csp.api.ui.UIRequest;
import org.collectionspace.csp.api.ui.UISession;
import org.collectionspace.csp.api.ui.UIUmbrella;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WebUIRequest implements UIRequest {
	private static final Logger log = LoggerFactory.getLogger(WebUIRequest.class);
	private static final String COOKIENAME="CSPACESESSID";
	private Integer lifeInMins = 45;

	private HttpServletRequest request;
	private HttpServletResponse response;
	private String[] ppath,rpath=null;
	private boolean failure=false,secondary_redirect=false;
	private Exception failing_exception;
	private Operation operation_performed=Operation.READ;
	private Map<String,String> rargs=new HashMap<String,String>();
	private PrintWriter out=null;
	private OutputStream out_stream=null;
	private String out_data=null; // We store to allow late changes to headers
	private byte[] out_binary_data=null;
	private String body; // XXX what if it's binary?
	private FileItemHeaders contentHeaders; 
	private String contenttype; 
	private byte[] bytebody;
	private String uploadName;
	private WebUIUmbrella umbrella;
	private WebUISession session;
	private boolean solidified=false;

	private void initRequest(UIUmbrella umbrella,HttpServletRequest request,HttpServletResponse response) throws IOException, UIException{
		this.request=request;
		this.response=response;
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if(isMultipart){
			// Create a new file upload handler
			ServletFileUpload upload = new ServletFileUpload();

			// Parse the request
			FileItemIterator iter;
			try {
				iter = upload.getItemIterator(request);
				while (iter.hasNext()) {
					FileItemStream item = iter.next();
					String name = item.getFieldName();
					//InputStream stream = item.openStream();
					if (item.isFormField()) {
					//	System.out.println("Form field " + name + " with value "
			        //    + Streams.asString(stream) + " detected.");
					} else {
					//	System.out.println("File field " + name + " with file name "
			        //    + item.getName() + " detected.");
			        // Process the input stream
						contentHeaders = item.getHeaders();
						uploadName = item.getName();

			            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
			            if (item != null) {
			                InputStream stream = item.openStream();
			                int len;
			                byte[] buffer = new byte[38192];
			                while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
			                  byteOut.write(buffer, 0, len);
			                }
			               
			            }
			            bytebody = byteOut.toByteArray();
					}
				}
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else{
			body=IOUtils.toString(request.getInputStream(),"UTF-8");
		}
		List<String> p=new ArrayList<String>();
		for(String part : request.getPathInfo().split("/")) {
			if("".equals(part))<commit_msg>nojira<commit_after>/* Copyright 2010 University of Cambridge
 * Licensed under the Educational Community License (ECL), Version 2.0. You may not use this file except in 
 * compliance with this License.
 *
 * You may obtain a copy of the ECL 2.0 License at https://source.collectionspace.org/collection-space/LICENSE.txt
 */
package org.collectionspace.chain.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.TeeInputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.fileupload.FileItemHeaders;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.*;
import org.collectionspace.csp.api.ui.Operation;
import org.collectionspace.csp.api.ui.TTYOutputter;
import org.collectionspace.csp.api.ui.UIException;
import org.collectionspace.csp.api.ui.UIRequest;
import org.collectionspace.csp.api.ui.UISession;
import org.collectionspace.csp.api.ui.UIUmbrella;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WebUIRequest implements UIRequest {
	private static final Logger log = LoggerFactory.getLogger(WebUIRequest.class);
	private static final String COOKIENAME="CSPACESESSID";
	private Integer lifeInMins = 45;

	private HttpServletRequest request;
	private HttpServletResponse response;
	private String[] ppath,rpath=null;
	private boolean failure=false,secondary_redirect=false;
	private Exception failing_exception;
	private Operation operation_performed=Operation.READ;
	private Map<String,String> rargs=new HashMap<String,String>();
	private PrintWriter out=null;
	private OutputStream out_stream=null;
	private String out_data=null; // We store to allow late changes to headers
	private byte[] out_binary_data=null;
	private String body; // XXX what if it's binary?
	private FileItemHeaders contentHeaders; 
	private String contenttype; 
	private byte[] bytebody;
	private String uploadName;
	private WebUIUmbrella umbrella;
	private WebUISession session;
	private boolean solidified=false;

	private void initRequest(UIUmbrella umbrella,HttpServletRequest request,HttpServletResponse response) throws IOException, UIException{
		this.request=request;
		this.response=response;
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if(isMultipart){
			// Create a new file upload handler
			ServletFileUpload upload = new ServletFileUpload();

			// Parse the request
			FileItemIterator iter;
			try {
				iter = upload.getItemIterator(request);
				while (iter.hasNext()) {
					FileItemStream item = iter.next();
					String name = item.getFieldName();
					//InputStream stream = item.openStream();
					if (item.isFormField()) {
					//	System.out.println("Form field " + name + " with value "
			        //    + Streams.asString(stream) + " detected.");
					} else {
					//	System.out.println("File field " + name + " with file name "
			        //    + item.getName() + " detected.");
			        // Process the input stream
						contentHeaders = item.getHeaders();
						uploadName = item.getName();

			            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
			            if (item != null) {
			                InputStream stream = item.openStream();
				            IOUtils.copy(stream,byteOut);
				            new TeeInputStream(stream,byteOut);
			               
			            }
			            bytebody = byteOut.toByteArray();
					}
				}
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else{
			body=IOUtils.toString(request.getInputStream(),"UTF-8");
		}
		List<String> p=new ArrayList<String>();
		for(String part : request.getPathInfo().split("/")) {
			if("".equals(part))
{'fraction_non_alphanumeric': 0.10238761023876103, 'fraction_numerical': 0.0036567003656700365, 'mean_word_length': 3.9753879079721774, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '1894179', 'n_tokens_mistral': 2890, 'n_tokens_neox': 2626, 'n_words': 637}