Java XPath Get GetMobivateSessionId()

Here you can find the source of GetMobivateSessionId()

Description

Get Mobivate Session Id

License

Apache License

Declaration

private static String GetMobivateSessionId() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    static final String LoginUrlModifier = "/login/%s/%s";
    static final String BaseApiUrl = "http://app.mobivatebulksms.com/bulksms/xmlapi";
    static final String Username = "info@hallmark-jewellers.com";
    static final String Password = "Fraser";

    private static String GetMobivateSessionId() {
        try {//www .j av a  2  s.  co  m
            String loginUri = String.format(LoginUrlModifier,
                    URLEncoder.encode(Username, "ISO-8859-1"), Password);
            Document document = DoApiCall(loginUri, null);
            return extractData(document, "/xaresponse/session");
        } catch (XPathExpressionException | UnsupportedEncodingException ex) {
            return null;
        }

    }

    private static Document DoApiCall(String requestUri, String requestXml) {
        URLConnection connection;
        try {
            URL url = new URL(BaseApiUrl + requestUri);
            connection = url.openConnection();
            connection.setRequestProperty("ContentType",
                    "application/x-www-form-urlencoded");
            connection.setRequestProperty("User-Agent",
                    "HALLMARK_STOCK_SYSTEM");
            connection.setDoOutput(true);
        } catch (IOException ex) {
            return null;
        }

        if (requestXml != null) {
            try (OutputStreamWriter out = new OutputStreamWriter(
                    connection.getOutputStream())) {
                out.write(requestXml);
            } catch (IOException io) {
                return null;
            }
        }

        String response;
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));

            String decodedString;
            StringBuilder responseBlock = new StringBuilder();

            while ((decodedString = in.readLine()) != null) {
                responseBlock.append(decodedString);
            }
            in.close();
            response = responseBlock.toString();
        } catch (IOException io) {
            return null;
        }

        Document doc;
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource source = new InputSource(new ByteArrayInputStream(
                    response.getBytes("utf-8")));
            doc = db.parse(source);
        } catch (IOException | ParserConfigurationException | SAXException ex) {
            return null;
        }

        return doc;

        //        var request = (HttpWebRequest)WebRequest.Create(HttpUtility.HtmlEncode(BaseApiUrl + requestUri));
        //
        //        request.ContentType = "application/x-www-form-urlencoded";
        //        request.UserAgent = "HALLMARK_STOCK_SYSTEM";
        //
        //        if (requestXml != null && string.IsNullOrWhiteSpace(requestXml) == false)
        //        {
        //            request.Method = WebRequestMethods.Http.Post;
        //            request.ContentLength = requestXml.Length;
        //            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
        //            {
        //                writer.Write(requestXml);
        //                writer.Close();
        //            }
        //        }
        //        else
        //        {
        //            request.Method = WebRequestMethods.Http.Get;
        //        }
        //
        //        // probably need usings here
        //        var response = request.GetResponse();
        //        var responseString = string.Empty;
        //        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        //        {
        //            responseString = reader.ReadToEnd();
        //            response.Close();
        //        }
        //
        //        return XDocument.Parse(responseString);
    }

    private static String extractData(Document document, String xpathNode)
            throws XPathExpressionException {
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();

        return xpath.evaluate(xpathNode, document);
    }
}

Related

  1. getInt(Object node, XPathExpression expression)
  2. getLatestVersion(String tempDir, String url, String groupId, String artifactId, String version)
  3. getListValue(Node node, XPathExpression expression)
  4. getListValue(String targetDoc, List xpathExps, String encoding)
  5. getMessage(XPathExpressionException e)
  6. getNameBasedXPath(Node n, boolean includeCurrent)
  7. getNewXPath()
  8. getNewXPath()
  9. getNode(Node node, XPathExpression xpath)