Package com.globant.google.mendoza.malbec

Source Code of com.globant.google.mendoza.malbec.XmlCartEncoder

/* vim: set ts=2 et sw=2 cindent fo=qroca: */

package com.globant.google.mendoza.malbec;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gargoylesoftware.htmlunit.KeyValuePair;
import com.globant.google.mendoza.malbec.schema._2.CheckoutShoppingCart;

/**
* The xml implementation of the CartEncoder.
* Used to encodes and sign an xml representation of a CheckoutShoppingCart.
*/
public final class XmlCartEncoder implements CartPostEncoder {

  /**
   * The class logger.
   */
  private static Log log = LogFactory.getLog(XmlCartEncoder.class);

  /**
   * Generates the post parameterse needed to place an order:
   * cart: contains the base64 encoded cart.
   * signature: contains the base64 encoded cart signed with the received key.
   * @param cart The cart to be encoded.
   * @param key The key used to sign the encoded cart.
   * @return Returns a key/value pair containing the needed parameters to place
   *  an order.
   */
  public List<KeyValuePair> generateParameters(final CheckoutShoppingCart cart,
      final String key) {
    log.trace("Entering generateParameters");

    final String xmlCart = CartUtils.toXml(cart);

    if (log.isTraceEnabled()) {
      log.trace("Generating parameters for this cart:\n" + xmlCart);
    }
    List<KeyValuePair> parameters = new ArrayList<KeyValuePair>();

    log.debug("Encoding the cart");
    final String encodedCart = Encoder.encode(xmlCart);

    log.debug("Signing the cart");
    final String signedCart = Encoder.sign(xmlCart, key);

    parameters.add(new KeyValuePair("cart", encodedCart));

    parameters.add(new KeyValuePair("signature", signedCart));

    log.trace("Leaving generateParameters");
    return parameters;
  }

  /**
   * Generates the post parameters needed to place an order:
   * cart: contains the base64 encoded cart.
   * signature: contains the base64 encoded cart signed with the received key.
   * @param theCart The cart to be posted.
   * @param theKey The key used to sign the encoded cart.
   * @return Returns a key/value pair containing the needed parameters to place
   *  an order.
   */
  public List<KeyValuePair> generateParameters(final String theCart,
      final String theKey) {
    List<KeyValuePair> parameters = new ArrayList<KeyValuePair>();
    if (log.isTraceEnabled()) {
      log.trace("Generating parameters for this cart:\n" + theCart);
    }
    final String encodedCart = Encoder.encode(theCart);
    final String signedCart = Encoder.sign(theCart, theKey);

    parameters.add(new KeyValuePair("cart", encodedCart));
    parameters.add(new KeyValuePair("signature", signedCart));
    return parameters;
  }

  /**
   * Returns the body of the request for posting the cart. As the cart goes as
   * an encoded parameter, this method just returns null.
   * @param theCart the cart to be posted.
   * @return null.
   */
  public String getBodyForCart(final CheckoutShoppingCart theCart) {
    return null;
  }

  /**
   * Returns the body of the request for posting the cart. As the cart goes as
   * an encoded parameter, this method just returns null.
   * @param theCart the cart to be posted.
   * @return null.
   */
  public String getBodyForCart(final String theCart) {
    return null;
  }
}
TOP

Related Classes of com.globant.google.mendoza.malbec.XmlCartEncoder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.