com.lm.lic.manager.controller.YouparkWithdrawLicHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.lm.lic.manager.controller.YouparkWithdrawLicHandler.java

Source

/**
 * $Id$
 */
package com.lm.lic.manager.controller;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;

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

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.servlet.ModelAndView;

import com.hhc.client.AuthenticatorException;
import com.lm.lic.manager.form.WithdrawLicForm;
import com.lm.lic.manager.hibernate.License;
import com.lm.lic.manager.hibernate.LicenseBlock;
import com.lm.lic.manager.hibernate.LicensePaymentStatus;
import com.lm.lic.manager.hibernate.Product;
import com.lm.lic.manager.hibernate.RequestForLicense;

/**
 * @author Ibrahim Mustafa
 */
public class YouparkWithdrawLicHandler extends AbstractWithdrawLicController implements WithdrawLicHandler {

    private final Logger logger = Logger.getLogger(YouparkWithdrawLicHandler.class);

    /**
     * @see com.lm.lic.manager.controller.WithdrawLicHandler#handleWithdrawal(javax.servlet.http.HttpServletRequest,
     *      com.lm.lic.manager.form.WithdrawLicForm)
     */
    @SuppressWarnings("deprecation")
    @Override
    public ModelAndView handleWithdrawal(HttpServletRequest request, HttpServletResponse response,
            WithdrawLicForm wlf) {
        logger.info("Start YOUPARK Handling of License Withdrawal Request");
        boolean valid = verifyYouparkRequest(request);

        logger.info("YOUPARK_REQUEST_VALID? : " + valid);

        Product product = null;
        String isvProdId = wlf.getParamProductID();
        int numLics = wlf.getParamProductQuantity();
        String isvId = wlf.getIsvId();

        Long storeId = lmxContext.findStoreId(ParticipantStoresAtLicmax.Youpark, request);

        if (StringUtils.isNotEmpty(isvId))
            product = productService.findQuickyProductByIsvProdStoreId(isvId, isvProdId, storeId + EMPTY);
        else
            product = productService.findQuickyProductByIsvProdId(isvProdId, storeId);

        if (product != null)
            logger.info("YOUPARK License Withdrawal for product: " + product.getName() + " " + product.getVersion()
                    + " ISV: " + product.getIsv().getName());

        RequestForLicense prevRfl = findExistingLicWithdrawalRecord(product, wlf, request);

        String prodId = product.getId() + EMPTY;
        isvId = product.getIsv().getId() + EMPTY;

        int numOverdraft = 0;
        LicenseBlock licenseBlock = licenseBlockService.findByIsvIdProdId(isvId, prodId);
        String localeLang = extractLocaleLang(request);
        if (licenseBlock == null) {
            numOverdraft = numLics;
            logger.info("Found 0 " + "Lics to withdraw for YOUPARK - Going OVERDRAFT");
            licenseAvailabilityAdvisor.generateLicenses(wlf.getDeviceId(), localeLang, product, numLics,
                    numOverdraft, LicensePaymentStatus.OVERDRAFT);
            licenseBlock = licenseBlockService.findByIsvIdProdId(isvId, prodId);
        }

        List<License> licenses = findDecentLicenses(wlf.getLicKey(), numLics, product, wlf.getDeviceId());
        if (licenses == null || licenses.size() < numLics) {
            int licensesSize = 0;
            if (licenses != null)
                licensesSize = licenses.size();
            numOverdraft = numLics - licensesSize;
            licenseAvailabilityAdvisor.generateLicenses(wlf.getDeviceId(), localeLang, product, numOverdraft,
                    numOverdraft, LicensePaymentStatus.OVERDRAFT);
            licenses = findDecentLicenses(wlf.getLicKey(), numLics, product, wlf.getDeviceId());
        }

        RequestForLicense currRfl = findRequestForLicenseTrace(request, wlf);

        if (licenses != null) {
            adjustDrawnLicenses(wlf, licenses, prevRfl, currRfl);
            licenseService.update(licenses);

            adjustLicenseBlock(licenseBlock, numLics, numOverdraft);
            licenseBlockService.update(licenseBlock);
            logger.info("Found " + licenses.size() + " Lics for YOUPARK");
        } else
            licenses = generateOverDraftLicenses(prevRfl, currRfl);

        for (License l : licenses)
            logger.info("Generated license for YOUPARK request: " + l.getLicKey() + " for product: "
                    + l.getProduct().getName() + " " + l.getProduct().getVersion());

        if (currRfl != null)
            adjustRequestForLicenseTransaction(product, currRfl, prevRfl, licenses, numOverdraft);

        String successView = getSuccessView();
        ModelAndView modelAndView = new ModelAndView(successView);
        modelAndView.addObject("isvId", isvId);
        modelAndView.addObject("prodId", prodId);
        modelAndView.addObject("licenses", licenses);
        modelAndView.addObject("rfl", currRfl);
        modelAndView.addObject("product", product);
        return modelAndView;
    }

    @SuppressWarnings("unchecked")
    public boolean verifyYouparkRequest(HttpServletRequest request) {
        boolean valid = false;
        com.hhc.client.CertificateReader reader = new com.hhc.client.CertificateReader();
        java.security.PublicKey publicKey = null;
        try {
            String pathToClassesDir = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
            int index = pathToClassesDir.indexOf("/WEB-INF/");
            String fullPath = pathToClassesDir.substring(0, index + "/WEB-INF/".length())
                    + "certificates/Handango.cert";

            FileSystemResource fr = new FileSystemResource(fullPath);
            publicKey = reader.getPublicKey(fr.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
            return false;
        }

        com.hhc.client.Authenticator authenticator = new com.hhc.client.Authenticator(publicKey);
        try {
            if (authenticator.verify(request.getParameterMap())) {
                valid = true;
            } else {
                valid = false;
            }
        } catch (AuthenticatorException e) {
            e.printStackTrace();
            return false;
        }

        valid = true;
        return valid;
    }
}