/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.paymentexpress.server;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.*;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.MethodInvocationException;
import com.google.paymentexpress.jwt.MaskedWalletRequest;
import com.google.paymentexpress.server.config.Config;
/**
* Order Page Servlet that renders the order page and
* generates the MaskedWalletRequest.
*
* Templating uses Velocity Template
*
* The selections and information generated by the user are
* posted to the next page the confirm servlet
*
* @author pying(Peng Ying)
*
*/
public class OrderServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -4919105809489724586L;
/**
* Get handler forwards request to post handler
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
doPost(req,resp);
}
/**
* Post handler renders the template generates the maskedWalletRequest
* and injects the JWT into the template
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp){
String origin = Config.getDomain(req);
//So we don't write to velocity.log
Velocity.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogChute");
// Escape HTML
Velocity.setProperty("eventhandler.referenceinsertion.class", "org.apache.velocity.app.event.implement.EscapeHtmlReference");
Velocity.setProperty("eventhandler.escape.html.match", "/.*/");
//Initialize Velocity Templates
Velocity.init();
VelocityContext context = new VelocityContext();
//Create a default MaskedWalletRequest with both pay and ship selectors
MaskedWalletRequest maskedWalletRequest = new MaskedWalletRequest(Config.MERCHANT_ID, Config.MERCHANT_SECRET, MaskedWalletRequest.Select.PAY_SHIP);
maskedWalletRequest.setOrigin(origin);
//Place the masked wallet and correct URLs into Velocity context
context.put("maskedJWT", maskedWalletRequest.generateJWT());
context.put("walletJSUrl", Config.WALLET_JS_URL);
PrintWriter pw;
try {
//Get the response output stream PrintWriter
pw = resp.getWriter();
//Render the template and output to print stream
Velocity.mergeTemplate("order.vm","UTF-8", context, pw);
pw.flush();
pw.close();
} catch (ResourceNotFoundException e) {
e.printStackTrace();
} catch (ParseErrorException e) {
e.printStackTrace();
} catch (MethodInvocationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}