Servlet.Cancel.java Source code

Java tutorial

Introduction

Here is the source code for Servlet.Cancel.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import com.sun.jersey.api.client.Client;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import response.GetResponse;

/**
 *
 * @author aHLai
 */
@WebServlet(name = "Cancel", urlPatterns = { "/Cancel" })
public class Cancel extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {

            String eventUrl = request.getParameter("eventUrl");
            System.out.print(eventUrl);

            String valid = "valid";

            OAuthConsumer consumer = new DefaultOAuthConsumer("test6-40942", "oQ4q4oBiv9y4jPr7");
            URL url = new URL(eventUrl);
            HttpURLConnection requestUrl = (HttpURLConnection) url.openConnection();
            consumer.sign(requestUrl);
            requestUrl.connect();

            GetResponse gr = new GetResponse();
            gr.validateUrl(valid);

            String result = null;
            StringBuilder sb = new StringBuilder();
            requestUrl.setRequestMethod("GET");
            BufferedReader br = new BufferedReader(new InputStreamReader(requestUrl.getInputStream()));
            String inputLine = "";
            while ((inputLine = br.readLine()) != null) {
                sb.append(inputLine);
            }

            result = sb.toString();

            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            InputSource src = new InputSource();
            src.setCharacterStream(new StringReader(result));

            //parsing elements by name tags from event url
            Document doc = builder.parse(src);
            String creatorName = doc.getElementsByTagName("fullName").item(0).getTextContent();
            String companyName = doc.getElementsByTagName("name").item(0).getTextContent();
            String edition = doc.getElementsByTagName("editionCode").item(0).getTextContent();
            String returnUrl = doc.getElementsByTagName("returnUrl").item(0).getTextContent();

            System.out.print(creatorName);
            System.out.print(companyName);
            System.out.print(edition);
            System.out.print(returnUrl);

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost xmlResponse = new HttpPost(returnUrl);
            StringEntity input = new StringEntity(
                    "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><result><success>true</success><message>Account change successful</message>");
            input.setContentType("text/xml");
            xmlResponse.setEntity(input);
            httpClient.execute(xmlResponse);

        } catch (OAuthMessageSignerException ex) {
            Logger.getLogger(Cancel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (OAuthExpectationFailedException ex) {
            Logger.getLogger(Cancel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (OAuthCommunicationException ex) {
            Logger.getLogger(Cancel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Cancel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Cancel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}