Modèle de programmation du service de connexions HTTP sortantes

Le service de connexions HTTP sortantes peut être utilisé dans le contexte d'un service de demande de servlet ou le contexte d'un service de demande de portlet. Voici quelques exemples de code.

L'exemple suivant indique comment obtenir le service de connexions HTTP sortantes dans le contexte d'un portlet :
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import com.ibm.portal.outbound.service.OutboundConnectionService;
import com.ibm.portal.outbound.service.OutboundConnectionServiceHome;
import com.ibm.portal.outbound.service.OutboundConnectionServiceException;

// obtain an Outbound HTTP connection service object (portlet context)

private OutboundConnectionService getService (PortletRequest p_request,
					      PortletResponse p_response) 
	throws  OutboundConnectionServiceException, NamingException
{

	Context ctx = new InitialContext();
	final OutboundConnectionServiceHome home = (OutboundConnectionServiceHome) 
           ctx.lookup(OutboundConnectionServiceHome.JNDI_NAME);
      final OutboundConnectionService service = 
        		home.getOutboundConnectionService(p_request, p_response); 
	return service;
}
Dans un autre scénario, le code qui appelle le service de connexions HTTP sortantes fait partie d'un servlet. Dans ce cas, la méthode getOutboundConnectionService () reçoit la variable de demande et de réponse de servlet. Consultez les exemples suivants :
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ibm.portal.outbound.service.OutboundConnectionService;
import com.ibm.portal.outbound.service.OutboundConnectionServiceHome;
import com.ibm.portal.outbound.service.OutboundConnectionServiceException;
…

// obtain an Outbound HTTP connection service object  (servlet context)
private OutboundConnectionService getService ( HttpServletRequest s_request,
					  HttpServletResponse s_response) 
	throws OutboundConnectionServiceException, NamingException
{

	Context ctx = new InitialContext();
	final OutboundConnectionServiceHome home = (OutboundConnectionServiceHome) 
           ctx.lookup(OutboundConnectionServiceHome.JNDI_NAME);
      final OutboundConnectionService service = 
        		home.getOutboundConnectionService(s_request, s_response); 
	return service;
}
Les applications utilisent la méthode createConnection() du service de connexion HTTP sortante pour ouvrir une connexion HTTP sortante. Le fragment de code suivant se connecte à une ressource d'URL via des connexions HTTP sortantes, lit le contenu de la page Web et l'écrit dans un flux de sortie sous forme de tableau d'octets. Consultez les exemples suivants :
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.ibm.portal.outbound.service.OutboundConnectionService;

...
/**
 * usage sample for GET requests using Outbound HTTP Connections
 * @param service the outbound HTTP connection service
 * @param theURL the remote URL
 * @param bos An object that receives the content of the GET request.
 * @return the HTTP status
 **/
private int doGet (OutboundConnectionService service, 
                   URL theURL,
                   ByteArrayOutputStream bos) 
    throws OutboundConnectionServiceException, IOException
{
    // obtain a connection object 
    HttpURLConnection connection = createConnection(theURL);
    try {
       // Submit the URL connection to the remote host.
       connection.connect();

       // read the returned content:
       InputStream is = (InputStream)connection.getContent();
       int byt = is.read();
       while (byt >= 0) {
          bos.write(byt);
          byt = is.read();
       }   
	 int status = connection.getStatus(); 
       return status;
    } finally {
       connection.disconnect();
       bos.close();
    }
}
Le fragment de code suivant se connecte à une ressource d'URL via des connexions HTTP sortantes et soumet une requête POST :
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.ibm.portal.outbound.service.OutboundConnectionService;

...
/**
 * usage sample for POST requests using Outbound HTTP Connections
 * @param service the outbound HTTP connection service
 * @param theURL the remote URL
 * @param postData the POST data.
 * @param bos An object that receives the content of the GET request.
 * @return the HTTP status
 **/
private int doPost (OutboundConnectionService service, 
                    URL theURL,
                    byte[] postData,
                    ByteArrayOutputStream bos) 
    throws OutboundConnectionServiceException, IOException
{
    // obtain a connection object 
    HttpURLConnection connection = createConnection(theURL);
    try {
       // write the POST data
       OutputStream os = connection.getOutputStream();
       os.write(postData, 0, postData.length);
       os.close();

       // Submit the URL connection to the remote host.
       connection.connect();	 

       // read the returned content:
       InputStream is = (InputStream)connection.getContent();
       int byt = is.read();
       while (byt >= 0) {
          bos.write(byt);
          byt = is.read();
       }   
	 int status = connection.getStatus(); 
       return status;
    } finally {
       connection.disconnect();
       bos.close();
    }
}
Le fragment de code ci-dessous obtient un service des connexions sortantes et demande le contenu de l'URL www.ibm.com :
ByteArrayOutputStrream bos = new ByteArrayOutputStream();

OutboundConnectionService ocs = getService (request, response);
int status = doGet(ocs, new URL("http.//www.ibm.com"), bos);
if (status >= 400) {
    System.err.println("Remote connection failed with HTTP status "+status);
} else {
    byte[] da = bos.toByteArray();
    String first = da.length > 10 ? new String(da,0,da.length) : new String(da);
    System.out.println("The content starts with "+first);
}