<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dominikdorn.com &#187; Session</title>
	<atom:link href="http://dominikdorn.com/tag/session/feed/" rel="self" type="application/rss+xml" />
	<link>http://dominikdorn.com</link>
	<description>shit happens ;)</description>
	<lastBuildDate>Tue, 03 Jan 2012 19:47:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Access Quercus PHP-Session from Java Servlets/Filters/Beans &#8211; Part 1</title>
		<link>http://dominikdorn.com/2010/06/access-quercus-php-session-from-java-servlets-filters-beans/</link>
		<comments>http://dominikdorn.com/2010/06/access-quercus-php-session-from-java-servlets-filters-beans/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 16:06:15 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Quercus]]></category>
		<category><![CDATA[Filter]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[Session]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=302</guid>
		<description><![CDATA[This article shows how to access a PHP-Session from Java when running under Quercus - an implementation of PHP in Java which runs in any decent Servlet-Container. ]]></description>
			<content:encoded><![CDATA[<p>If you are integrating a PHP Application in your Java Environment, you&#8217;ll probably came across <a href="http://quercus.caucho.com">Quercus</a>, a <a href="http://quercus.caucho.com">Java Implementation of PHP</a> provided by <a href="http://www.caucho.com">Caucho Technologies</a>. </p>
<p>As you are moving more and more into the Servlet world, you&#8217;ll probably will be using Servlet-Filters and normal Servlets todo some tasks. </p>
<p>If you then want to access Variables in a PHP Session, here&#8217;s how to do it:</p>
<p>The Quercus PHP-Session is not stored the same way as the normal Servlet HTTP-Session is stored. Quercus uses an own Session Manager that is capable of doing all this stuff that you know from PHP, like e.g. migrating Session-IDs (to prevent session-hijacking etc.). </p>
<p>To access this Session Manager, you first have to get a hand on the <strong>QuercusContext</strong>. In the current release (4.0.7) there is no easy way to get a hand on it if you are not inside a <strong>PHPModule</strong> or a PHP-Page. Thus I simply copied <strong>com.caucho.quercus.servlet.QuercusServlet</strong> to <strong>com.caucho.quercus.servlet.DCQuercusServlet</strong> and added this line to the end of the method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> initImpl<span style="color: #009900;">&#40;</span>ServletConfig config<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> ServletException
  <span style="color: #009900;">&#123;</span>
....
      <span style="color: #006633;">config</span>.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;quercusContext&quot;</span>, quercus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>which <strong>exposes the QuercusContext as an Attribute in the ServletContext</strong>.</p>
<p>(Note: If you are running multiple <strong>QuercusServlet</strong>s in your WebApp &#8211; which you probably shouldn&#8217;t &#8211; this might will be a problem)<br />
(Note2: The package name com.caucho.quercus.servlet must be used, or else you will not have access to some package scoped properties used in the QuercusServlet)<br />
Ok, so now we have access to the <strong>QuercusContext</strong> from every <strong>Servlet</strong>, <strong>Filter</strong> or <strong>JSF-Bean</strong>, horray <img src='http://dominikdorn.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<p>But how to use it? </p>
<p>Lets take a look at the following filter and its methods helping me to work with the PHP-Session:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoginOutFilter <span style="color: #000000; font-weight: bold;">implements</span> Filter <span style="color: #009900;">&#123;</span>
&nbsp;
    QuercusContext context<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// we store the reference to the quercusContext in the filter</span>
    FilterConfig config<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// the reference to the config </span>
&nbsp;
... 
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span>FilterConfig filterConfig<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException <span style="color: #009900;">&#123;</span>
        config <span style="color: #339933;">=</span> filterConfig<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// looks up the QuercusContext the first time it is accessed</span>
    <span style="color: #666666; font-style: italic;">// Beware: will return null if no PHP page has been rendered yet and the quercusContext is not already set. </span>
    <span style="color: #000000; font-weight: bold;">private</span> QuercusContext getQuercusContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>context <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            context <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>QuercusContext<span style="color: #009900;">&#41;</span> config.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;quercusContext&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> context<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// find the PHP-Session Cookie in the array of Cookies provided by the browser</span>
    <span style="color: #000000; font-weight: bold;">public</span> Cookie findPHPSessionCookie<span style="color: #009900;">&#40;</span>Cookie<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> cookies<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>Cookie c <span style="color: #339933;">:</span> cookies<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PHPSESSID&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> c<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doFilter<span style="color: #009900;">&#40;</span>ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>someCondition<span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// clear a PHP-Session, e.g. when logging out a user</span>
                Cookie c <span style="color: #339933;">=</span> findPHPSessionCookie<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>HttpServletRequest<span style="color: #009900;">&#41;</span> servletRequest<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getCookies</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>c <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    QuercusSessionManager mng <span style="color: #339933;">=</span> getQuercusContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getQuercusSessionManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #003399;">String</span> phpSessionId <span style="color: #339933;">=</span> c.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    mng.<span style="color: #006633;">removeSession</span><span style="color: #009900;">&#40;</span>phpSessionId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>someOtherCondition<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        QuercusSessionManager mng <span style="color: #339933;">=</span> getQuercusContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getQuercusSessionManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                        SessionArrayValue val<span style="color: #339933;">;</span>
                        Cookie c <span style="color: #339933;">=</span> findPHPSessionCookie<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>HttpServletRequest<span style="color: #009900;">&#41;</span> servletRequest<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getCookies</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #003399;">String</span> phpSessionId<span style="color: #339933;">;</span>
                        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>c <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// we found a session cookie</span>
                            phpSessionId <span style="color: #339933;">=</span> c.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            val <span style="color: #339933;">=</span> mng.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, phpSessionId, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>val <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
                            <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// but the session does not exist on the server, so create it</span>
                                val <span style="color: #339933;">=</span> mng.<span style="color: #006633;">createSession</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, phpSessionId, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #009900;">&#125;</span>
&nbsp;
                        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span>
                        <span style="color: #009900;">&#123;</span>
          <span style="color: #666666; font-style: italic;">// we haven't found a php-session cookie, so create a new session</span>
                            val <span style="color: #339933;">=</span> mng.<span style="color: #006633;">createSession</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>,<span style="color: #000066; font-weight: bold;">null</span>,<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            phpSessionId <span style="color: #339933;">=</span> val.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #666666; font-style: italic;">// and store the appropriate cookie</span>
                            Cookie newCookie <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Cookie<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PHPSESSID&quot;</span>, phpSessionId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>HttpServletResponse<span style="color: #009900;">&#41;</span>servletResponse<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addCookie</span><span style="color: #009900;">&#40;</span>newCookie<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
     <span style="color: #666666; font-style: italic;">// storing data in the session</span>
                      <span style="color: #003399;">String</span> username <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;myUsername&quot;</span><span style="color: #339933;">;</span>
                        val.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ludata_suserid&quot;</span>, username<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        val.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ludata_nusernr&quot;</span>, someService.<span style="color: #006633;">getUserId</span><span style="color: #009900;">&#40;</span>username<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        val.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ludata_nstatusnr&quot;</span>, someService.<span style="color: #006633;">getUserStatus</span><span style="color: #009900;">&#40;</span>username<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        val.<span style="color: #006633;">addUse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This is required for saving, else an IllegalStateException will be thrown</span>
                        mng.<span style="color: #006633;">saveSession</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, val<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The above code shows how to <strong>erase a session</strong> and how to <strong>add values to a session</strong>. To <strong>get values from a session</strong>, simply use val.get(..). </p>
<p><strong>Attention: This currently only works if the page after the filter is not a Quercus Page itself (e.g. a Servlet or a JSF Page). I&#8217;m working on a solution for this</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/06/access-quercus-php-session-from-java-servlets-filters-beans/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

