package edu.wisc.doit.tcrypt.controller;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.wisc.doit.tcrypt.BouncyCastleTokenEncrypter;
import edu.wisc.doit.tcrypt.TokenEncrypter;
import edu.wisc.doit.tcrypt.exception.ServiceErrorException;
import edu.wisc.doit.tcrypt.services.TCryptServices;
import edu.wisc.doit.tcrypt.vo.EncryptToken;
import edu.wisc.doit.tcrypt.vo.ServiceKey;

@Controller
public class EncryptAjaxController {
	
	protected static final Logger logger = LoggerFactory.getLogger(EncryptAjaxController.class);
	
	private TCryptServices tcryptServices;
	private HashMap<String,TokenEncrypter> tokenEncrypters;
	
	@Autowired
	public EncryptAjaxController(TCryptServices tcryptServices) {
		this.tcryptServices = tcryptServices;
		tokenEncrypters = new HashMap<String,TokenEncrypter>();
	}
	
	@RequestMapping(value = "/encryptAjax", method = RequestMethod.POST)
	public @ResponseBody EncryptToken encryptTextAjax(@ModelAttribute (value="encryptToken") EncryptToken token, BindingResult result, HttpServletResponse response) {

		try {
			TokenEncrypter tokenEncrypter = getTokenEncrypter(token.getServiceKeyName());
			token.setEncryptedText(tokenEncrypter.encrypt(token.getUnencryptedText()));
		} catch (Exception e) {
			logger.error("Could not encrypt text",e);
			response.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
			token.setErrorMessage("There was an issue encrypting the text, please contact an admin if this behavior continues");
		}
		
		return token;
	}
	
	//	private method

	private TokenEncrypter getTokenEncrypter (String serviceName) throws ServiceErrorException, IOException {
		
		TokenEncrypter tokenEncrypter = null;
		if(tokenEncrypters.containsKey(serviceName)) {
			tokenEncrypter = tokenEncrypters.get(serviceName);
		} else {
			ServiceKey sk = tcryptServices.readServiceKeyFromFileSystem(serviceName);
			if(sk != null  && sk.getPublicKey() != null) {
				tokenEncrypter = new BouncyCastleTokenEncrypter(sk.getPublicKey());
				tokenEncrypters.put(sk.getServiceName(), tokenEncrypter);
			} else {
				throw new ServiceErrorException(serviceName,"error.serviceKeyNotFound");
			}
			
		}
		return tokenEncrypter;
	}
	
}

@@ -1,8 +1,835 @@
+/**%0A * Copyright 2012, Board of Regents of the University of%0A * Wisconsin System. See the NOTICE file distributed with%0A * this work for additional information regarding copyright%0A * ownership. Board of Regents of the University of Wisconsin%0A * System licenses this file to you under the Apache License,%0A * Version 2.0 (the %22License%22); you may not use this file%0A * except in compliance with the License. You may obtain a%0A * copy of the License at:%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing,%0A * software distributed under the License is distributed on%0A * an %22AS IS%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY%0A * KIND, either express or implied. See the License for the%0A * specific language governing permissions and limitations%0A * under the License.%0A */%0A
 package 

Fix licens header