Implémentation d'un filtre personnalisé de transformation de cookie
Les développeurs d'applications peuvent implémenter un filtre personnalisé de transformation de cookie.
Pourquoi et quand exécuter cette tâche
Pour ce faire, ils doivent fournir une implémentation des interfaces
com.ibm.mashups.proxy.ext.CookieTransformer et com.ibm.mashups.proxy.ext.CookieTransformerFactory. La classe de fabrique crée et renvoie une instance de l'objet transformateur du cookie. L'exemple de code suivant utilise le filtre de transformation de cookie pour écrire le nom et la valeur d'un cookie donné dans un journal. L'exemple de code utilise System.err en tant qu'objet Writer.package test.sample;
import com.ibm.mashups.proxy.ext.CookieTransformer;
import com.ibm.mashups.proxy.ext.CookieTransformerFactory;
public class SampleCookieXformerFactory implements CookieTransformerFactory {
/**
* Produce and return a new CookieTransformer instance.
* @param url is the URL for which the cookie transformer is to be opened.
* @param the metadata that are associated with the transformer object.
* @return CookieTransformer the custom cookie transformation handler.
* Must not be null.
*/
@Override
public CookieTransformer newCookieTransformer(URL url,
Map<String, String> metadata) {
return new SampleCookieTransformer (url);
}
}
package test.sample;
import com.ibm.mashups.proxy.ext.CookieTransformer;
import com.ibm.mashups.proxy.ext.CookieTransformerFactory;
/**
* The custom cookie transformation handler logs ingoing and outgoing
* cookies, for which the cookie transformer handler was defined.
*
*/
public class SampleCookieTransformer implements CookieTransformer (
// sample code: use System.err as output stream.
private PrintStream getWriter () {
return System.err;
}
private URL theURL;
/**
* Initialize a sample cookie transformation handler.
* @param url the URL for which the outbound connection was opened.
**/
public SampleCookieTransformer (URL url) {
this.theURL = url;
}
/**
* Process an outgoing cookie.
* This method is called before the outbound HTTP connection is submitted
* to the remote host.
* The sample code writes the name and value of the cookie and the URL
* to which the connection was submitted to a PrintStream writer.
* @param cookie the cookie instance.
*/
@Override
public void transformOutbound(HttpCookie cookie) {
getWriter().println (Cookie "+cookie.getName() +" sent to URL
"+theURL+". value="+cookie.getValue());
}
/**
* Process an ingoing cookie.
* This method is called after an HTTP response header was read.
* The response header contains a Set-Cookie statement for a cookie
* to which this connection handler applies.
* The sample code writes the name and value of the affected cookie
* and the URL from where the cookie was set to a PrintStream writer.
* @param cookie the cookie instance.
*/
@Override
public void transformInbound(HttpCookie cookie) {
getWriter().println (Cookie "+cookie.getName() +" received from URL
"+theURL+". value="+cookie.getValue());
}
}Un gestionnaire personnalisé de transformation de cookie est une extension du service de connexions HTTP sortantes. Par conséquent, le code du gestionnaire d'extension doit être accessible pour le chargeur de classe de WebSphere® Application Server sur lequel HCL s'exécute. Par exemple, placez le code dans le répertoire suivant :- UNIX™Linux™ :
PortalServer_root/shared/app - IBM® i:
PortalServer_root/shared/app - z/OS® :
PortalServer_root/shared/app - Windows™ :
PortalServer_root\shared\app