Forum Stats

  • 3,734,722 Users
  • 2,247,033 Discussions
  • 7,857,471 Comments

Discussions

How to send data to another site, get response, redirect and get new response

morellik
morellik Member Posts: 19

Dear all,

I've to write a Java EE application that have to become an interface to a payment platform. The protocol require to send a POST string containing some information (including an error and response page) to an external host, get the response and redirect the user to another host using the previous response. When the user ends the transaction the host redirect the user to my error or response page.

For the moment my user access to my application using glassfish REALM authentication. After that I write the following code:

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> paymentPrepare</span><span class="pun">()</span><span class="pln"> </span><span class="kwd">throws</span><span class="pln"> </span><span class="typ">UnsupportedEncodingException</span><span class="pun">,</span><span class="pln"> </span><span class="typ">IOException</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br/><br/>   </span><span class="typ">String</span><span class="pln"> postUrl </span><span class="pun">=</span><span class="pln"> </span><span class="str"><span>"</span><a class="jive-link-external-small" href="https://test.payment.test/init/http">https://test.payment.test/init/http</a><span>"</span></span><span class="pun">;</span><span class="pln"><br/>   </span><span class="typ">HttpPost</span><span class="pln"> postRequest </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">HttpPost</span><span class="pun">(</span><span class="pln">postUrl</span><span class="pun">);</span><span class="pln"><br/>   </span><span class="typ">String</span><span class="pln"> id </span><span class="pun">=</span><span class="pln"> </span><span class="str">"99999999"</span><span class="pun">;</span><span class="pln"><br/>   </span><span class="typ">String</span><span class="pln"> password </span><span class="pun">=</span><span class="pln"> </span><span class="str">"99999999"</span><span class="pun">;</span><span class="pln"><br/>   </span><span class="typ">String</span><span class="pln"> action </span><span class="pun">=</span><span class="pln"> </span><span class="str">"4"</span><span class="pun">;</span><span class="pln"><br/>   </span><span class="typ">String</span><span class="pln"> amt </span><span class="pun">=</span><span class="pln"> </span><span class="str">"1.00"</span><span class="pun">;</span><span class="pln"><br/>   </span><span class="typ">String</span><span class="pln"> currencycode </span><span class="pun">=</span><span class="pln"> </span><span class="str">"978"</span><span class="pun">;</span><span class="pln"><br/>   </span><span class="typ">String</span><span class="pln"> langid </span><span class="pun">=</span><span class="pln"> </span><span class="str">"ENG"</span><span class="pun">;</span><span class="pln"><br/>   </span><span class="typ">String</span><span class="pln"> responseurl </span><span class="pun">=</span><span class="pln"> </span><span class="str"><span>"</span><a class="jive-link-external-small" href="http://myhost:8080/EMBOWorkshop/secure/response.xhtml">http://myhost:8080/EMBOWorkshop/secure/response.xhtml</a><span>"</span></span><span class="pun">;</span><span class="pln"><br/>   </span><span class="typ">String</span><span class="pln"> errorurl </span><span class="pun">=</span><span class="pln"> </span><span class="str"><span>"</span><a class="jive-link-external-small" href="http://myhost:8080/EMBOWorkshop/secure/error.xhtml">http://myhost:8080/EMBOWorkshop/secure/error.xhtml</a><span>"</span></span><span class="pun">;</span><span class="pln"><br/>  trackid </span><span class="pun">=</span><span class="pln"> </span><span class="str">"TRCK0001"</span><span class="pun">;</span><span class="pln"><br/>  udf1 </span><span class="pun">=</span><span class="pln"> </span><span class="str">"Descrizione"</span><span class="pun">;</span><span class="pln"><br/><br/>   </span><span class="typ">StringEntity</span><span class="pln"> postParamsEntity </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">StringEntity</span><span class="pun">(</span><span class="str">"id="</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> id<br/>   </span><span class="pun">+</span><span class="pln"> </span><span class="str">"&password="</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> password<br/>   </span><span class="pun">+</span><span class="pln"> </span><span class="str">"&action="</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> action<br/>   </span><span class="pun">+</span><span class="pln"> </span><span class="str">"&amt="</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> amt<br/>   </span><span class="pun">+</span><span class="pln"> </span><span class="str">"&currencycode="</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> currencycode<br/>   </span><span class="pun">+</span><span class="pln"> </span><span class="str">"&langid="</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> langid<br/>   </span><span class="pun">+</span><span class="pln"> </span><span class="str">"&responseurl="</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> responseurl<br/>   </span><span class="pun">+</span><span class="pln"> </span><span class="str">"&errorurl"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> errorurl<br/>   </span><span class="pun">+</span><span class="pln"> </span><span class="str">"&trackid"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> trackid<br/>   </span><span class="pun">+</span><span class="pln"> </span><span class="str">"&udf1"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> udf1</span><span class="pun">);</span><span class="pln"><br/>  postParamsEntity</span><span class="pun">.</span><span class="pln">setContentType</span><span class="pun">(</span><span class="str">"application/x-www-form-urlencoded"</span><span class="pun">);</span><span class="pln"><br/>  postRequest</span><span class="pun">.</span><span class="pln">setEntity</span><span class="pun">(</span><span class="pln">postParamsEntity</span><span class="pun">);</span><span class="pln"><br/>   </span><span class="typ">DefaultHttpClient</span><span class="pln"> httpClient </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">DefaultHttpClient</span><span class="pun">();</span><span class="pln"><br/>   </span><span class="com">// Execute the HTTP POST </span><span class="pln"><br/>   </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Executing HTTP Post...\n"</span><span class="pun">);</span><span class="pln"><br/>   </span><span class="typ">HttpResponse</span><span class="pln"> response </span><span class="pun">=</span><span class="pln"> httpClient</span><span class="pun">.</span><span class="pln">execute</span><span class="pun">(</span><span class="pln">postRequest</span><span class="pun">);</span><span class="pln"><br/>   </span><span class="com">// Check the HTTP status of the post.</span><span class="pln"><br/>   </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">response</span><span class="pun">.</span><span class="pln">getStatusLine</span><span class="pun">().</span><span class="pln">getStatusCode</span><span class="pun">()</span><span class="pln"> </span><span class="pun">!=</span><span class="pln"> </span><span class="lit">200</span><span class="pln"> </span><span class="pun">&&</span><span class="pln"> response</span><span class="pun">.</span><span class="pln">getStatusLine</span><span class="pun">().</span><span class="pln">getStatusCode</span><span class="pun">()</span><span class="pln"> </span><span class="pun">!=</span><span class="pln"> </span><span class="lit">201</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br/>   </span><span class="kwd">throw</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RuntimeException</span><span class="pun">(</span><span class="str">"Failed: HTTP error code: "</span><span class="pln"><br/>   </span><span class="pun">+</span><span class="pln"> response</span><span class="pun">.</span><span class="pln">getStatusLine</span><span class="pun">().</span><span class="pln">getStatusCode</span><span class="pun">());</span><span class="pln"><br/>   </span><span class="pun">}</span><span class="pln"><br/>   </span><span class="com">// Create a reader to read in the HTTP post results.</span><span class="pln"><br/>   </span><span class="typ">BufferedReader</span><span class="pln"> br </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">BufferedReader</span><span class="pun">(</span><span class="pln"><br/>   </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">InputStreamReader</span><span class="pun">((</span><span class="pln">response</span><span class="pun">.</span><span class="pln">getEntity</span><span class="pun">().</span><span class="pln">getContent</span><span class="pun">())));</span><span class="pln"><br/>   </span><span class="com">// Read in all of the post results into a String.</span><span class="pln"><br/>   </span><span class="typ">String</span><span class="pln"> output </span><span class="pun">=</span><span class="pln"> </span><span class="str">""</span><span class="pun">;</span><span class="pln"><br/>   </span><span class="typ">Boolean</span><span class="pln"> keepGoing </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln"><br/>   </span><span class="kwd">while</span><span class="pln"> </span><span class="pun">(</span><span class="pln">keepGoing</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br/>   </span><span class="typ">String</span><span class="pln"> currentLine </span><span class="pun">=</span><span class="pln"> br</span><span class="pun">.</span><span class="pln">readLine</span><span class="pun">();</span><span class="pln"><br/><br/>   </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">currentLine </span><span class="pun">==</span><span class="pln"> </span>
<span class="pln"></span><span class="kwd">null</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br/>  keepGoing </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln"><br/>   </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br/>  output </span><span class="pun">+=</span><span class="pln"> currentLine</span><span class="pun">;</span><span class="pln"><br/>   </span><span class="pun">}</span><span class="pln"><br/>   </span><span class="pun">}</span><span class="pln"><br/><br/>   </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Raw string result: \n"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> output</span><span class="pun">);</span><span class="pln"><br/><br/></span><span class="pun">}</span>

The previous code works fine. I receive the response. But now I don't know how to continue. How can redirect the user to another site, and when the payment is finished, receive the response? Another question is, the user authenticate himself to access to the application. When the user will redirect to an external site, the glassfish session permits to get the response from the other host without require a new authentication?

For the moment I don't know how to redirect the user, but I tried to write this method to obtain the last response:

<span class="pln">  </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> getResponse</span><span class="pun">(</span><span class="typ">HttpServletRequest</span><span class="pln"> request</span><span class="pun">,</span><span class="pln"> </span><span class="typ">HttpServletResponse</span><span class="pln"> response</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br/>  paymentId </span><span class="pun">=</span><span class="pln"> request</span><span class="pun">.</span><span class="pln">getParameter</span><span class="pun">(</span><span class="str">"paymentid"</span><span class="pun">);</span><span class="pln"><br/>  result </span><span class="pun">=</span><span class="pln"> request</span><span class="pun">.</span><span class="pln">getParameter</span><span class="pun">(</span><span class="str">"result"</span><span class="pun">);</span><span class="pln"><br/>  auth </span><span class="pun">=</span><span class="pln"> request</span><span class="pun">.</span><span class="pln">getParameter</span><span class="pun">(</span><span class="str">"auth"</span><span class="pun">);</span><span class="pln"><br/>   </span><span class="kwd">ref</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> request</span><span class="pun">.</span><span class="pln">getParameter</span><span class="pun">(</span><span class="str">"ref"</span><span class="pun">);</span><span class="pln"><br/>  traind </span><span class="pun">=</span><span class="pln"> request</span><span class="pun">.</span><span class="pln">getParameter</span><span class="pun">(</span><span class="str">"tranid"</span><span class="pun">);</span><span class="pln"><br/>  trackid </span><span class="pun">=</span><span class="pln"> request</span><span class="pun">.</span><span class="pln">getParameter</span><span class="pun">(</span><span class="str">"trackid"</span><span class="pun">);</span><span class="pln"><br/>  udf1 </span><span class="pun">=</span><span class="pln"> request</span><span class="pun">.</span><span class="pln">getParameter</span><span class="pun">(</span><span class="str">"udf1"</span><span class="pun">);</span><span class="pln"><br/>  responsecode </span><span class="pun">=</span><span class="pln"> request</span><span class="pun">.</span><span class="pln">getParameter</span><span class="pun">(</span><span class="str">"responsecode"</span><span class="pun">);</span><span class="pln"><br/><br/></span><span class="pun">}</span>

Is it correct?

Thanks

Answers

  • morellik
    morellik Member Posts: 19
    edited Nov 13, 2013 7:59AM

    Now I'm able to redirect the user to the other page:

    <span class="typ">ExternalContext</span><span class="pln"> externalContext </span><span class="pun">=</span><span class="pln"> </span><span class="typ">FacesContext</span><span class="pun">.</span><span class="pln">getCurrentInstance</span><span class="pun">().</span><span class="pln">getExternalContext</span><span class="pun">();</span><span class="pln"><br/>externalContext</span><span class="pun">.</span><span class="pln">redirect</span><span class="pun">(</span><span class="str"><span>"</span><a class="jive-link-external-small" href="http://stackoverflow.com/">http://stackoverflow.com</a><span>"</span></span><span class="pun">);</span>
    
    
    

    Now the problem is: once the user has finished to pay, the other site redirect the user to my site using one of the two JSF page I prepared for error or response like the following:

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                    template="./index.xhtml"
                    xmlns:h="http://java.sun.com/jsf/html"
                    xmlns:f="http://java.sun.com/jsf/core"
                    xmlns:rich="http://richfaces.org/rich"
                    xmlns:a4j="http://richfaces.org/a4j"
                    xmlns="http://www.w3.org/1999/xhtml"
                    xmlns:fn="http://java.sun.com/jsp/jstl/functions"
                    >
    
    
        <ui:define name="content">
            <center>
               An error occurred during  transaction
                #{participantBean.auth}<br />
                #{participantBean.responsecode}
            </center>
      </ui:define>
       
    </ui:composition>
    
    
    

    So, how can I get the GET parameters that the other host sent to me during the redirection?

This discussion has been closed.