Java tutorial
/** * $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 PocketGearWithdrawLicHandler extends AbstractWithdrawLicController implements WithdrawLicHandler { private final Logger logger = Logger.getLogger(PocketGearWithdrawLicHandler.class); private static final int NUM_POCKETGEAR_REQUESTED_LICENSES = 1; /** * @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 POCKETGEAR Handling of License Withdrawal Request"); boolean valid = verifyPocketGearRequest(request); logger.info("POCKETGEAR_REQUEST_VALID? : " + valid); Product product = null; String isvProdId = wlf.getProductID(); if (StringUtils.isNotEmpty(isvProdId)) { int index = isvProdId.indexOf('='); if (index > 0) isvProdId = isvProdId.substring(index + 1); } int numLics = NUM_POCKETGEAR_REQUESTED_LICENSES; String isvId = wlf.getIsvId(); Long storeId = lmxContext.findStoreId(ParticipantStoresAtLicmax.PocketGear, request); if (StringUtils.isNotEmpty(isvId)) product = productService.findQuickyProductByIsvProdStoreId(isvId, isvProdId, storeId + EMPTY); else product = productService.findQuickyProductByIsvProdId(isvProdId, storeId); if (product != null) logger.info("POCKETGEAR 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 POCKETGEAR - 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 POCKETGEAR"); } else licenses = generateOverDraftLicenses(prevRfl, currRfl); for (License l : licenses) logger.info("Generated license for POCKETGEAR 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 verifyPocketGearRequest(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; } }