001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.labs.encryption;
017
018import org.kuali.rice.core.api.CoreApiServiceLocator;
019import org.kuali.rice.core.api.encryption.EncryptionService;
020import org.kuali.rice.krad.util.GlobalVariables;
021import org.kuali.rice.krad.web.controller.UifControllerBase;
022import org.kuali.rice.krad.web.form.UifFormBase;
023import org.springframework.stereotype.Controller;
024import org.springframework.validation.BindingResult;
025import org.springframework.web.bind.annotation.ModelAttribute;
026import org.springframework.web.bind.annotation.RequestMapping;
027import org.springframework.web.servlet.ModelAndView;
028
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletResponse;
031import java.security.GeneralSecurityException;
032
033/**
034 * Controller for the encrypt/decrypt utility
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038@Controller
039@RequestMapping(value = "/encryption")
040public class EncryptionController extends UifControllerBase {
041
042    public static final String INPUT_FIELD = "input";
043    public static final String ENCRYPTION_ERROR = "labs.encryption.error";
044    private  EncryptionService encryptionService;
045
046    /**
047     * Polulate the encryptionServiceName on the form.
048     */
049    @Override
050    protected EncryptionForm createInitialForm() {
051        EncryptionForm encryptionForm = new EncryptionForm();
052        encryptionForm.setEncryptionServiceName(getEncryptionService().getClass().getSimpleName());
053        return encryptionForm;
054    }
055
056    /**
057     * Encrypt the text of the input field.
058     *
059     * <p>
060     * The encryptedText and decryptedText filds are populated when encryption is successful.  An error message
061     * is displayed otherwise.
062     * </p>
063     */
064    @RequestMapping(params = "methodToCall=encrypt")
065    public ModelAndView encrypt(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
066            HttpServletRequest request, HttpServletResponse response) {
067        EncryptionForm encryptionForm = (EncryptionForm) form;
068
069        try {
070            encryptionForm.setEncryptedText(getEncryptionService().encrypt(
071                    encryptionForm.getInput()));
072            encryptionForm.setDecryptedText(encryptionForm.getInput());
073        }
074        catch (GeneralSecurityException gse) {
075            GlobalVariables.getMessageMap().putError(INPUT_FIELD, ENCRYPTION_ERROR, gse.toString());
076        }
077
078        return getModelAndView(form);
079    }
080
081    /**
082     * Decrypt the text of the input field.
083     *
084     * <p>
085     * The encryptedText and decryptedText filds are populated when decryption is successful.  An error message
086     * is displayed otherwise.
087     * </p>
088     */
089    @RequestMapping(params = "methodToCall=decrypt")
090    public ModelAndView decrypt(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
091            HttpServletRequest request, HttpServletResponse response) {
092        EncryptionForm encryptionForm = (EncryptionForm) form;
093
094        try {
095            encryptionForm.setEncryptedText(encryptionForm.getInput());
096            encryptionForm.setDecryptedText(getEncryptionService().decrypt(
097                    encryptionForm.getInput()));
098        }
099        catch (GeneralSecurityException gse) {
100            GlobalVariables.getMessageMap().putError(INPUT_FIELD, ENCRYPTION_ERROR, gse.toString());
101        }
102
103        return getModelAndView(form);
104    }
105
106    /**
107     * Get the encryption service
108     *
109     * @return EncryptionService
110     */
111    private EncryptionService getEncryptionService() {
112        if (encryptionService == null) {
113            encryptionService = CoreApiServiceLocator.getEncryptionService();
114        }
115
116        return encryptionService;
117    }
118}