⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mdcinjectionfiltertest.html

📁 MINA+API 关于MINA的API
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<a name="257" href="#257">257</a>                                    String user) {<a name="258" href="#258">258</a>         InetSocketAddress remoteAddress = (InetSocketAddress) address;<a name="259" href="#259">259</a>         <strong class="jxr_keyword">for</strong> (LoggingEvent event : events) {<a name="260" href="#260">260</a>             <strong class="jxr_keyword">if</strong> (event.getMessage().equals(message) &amp;&amp;<a name="261" href="#261">261</a>                 event.getMDC(<span class="jxr_string">"remoteAddress"</span>).equals(remoteAddress.toString()) &amp;&amp;<a name="262" href="#262">262</a>                 event.getMDC(<span class="jxr_string">"remoteIp"</span>).equals(remoteAddress.getAddress().getHostAddress()) &amp;&amp;<a name="263" href="#263">263</a>                 event.getMDC(<span class="jxr_string">"remotePort"</span>).equals(remoteAddress.getPort()+<span class="jxr_string">""</span>) ) {<a name="264" href="#264">264</a>                 <strong class="jxr_keyword">if</strong> (user == <strong class="jxr_keyword">null</strong> &amp;&amp; event.getMDC(<span class="jxr_string">"user"</span>) == <strong class="jxr_keyword">null</strong>) {<a name="265" href="#265">265</a>                     <strong class="jxr_keyword">return</strong>;<a name="266" href="#266">266</a>                 }<a name="267" href="#267">267</a>                 <strong class="jxr_keyword">if</strong> (user != <strong class="jxr_keyword">null</strong> &amp;&amp; user.equals(event.getMDC(<span class="jxr_string">"user"</span>))) {<a name="268" href="#268">268</a>                     <strong class="jxr_keyword">return</strong>;<a name="269" href="#269">269</a>                 }<a name="270" href="#270">270</a>                 <strong class="jxr_keyword">return</strong>;<a name="271" href="#271">271</a>             }<a name="272" href="#272">272</a>         }<a name="273" href="#273">273</a>         fail(<span class="jxr_string">"No LoggingEvent found from ["</span> + remoteAddress +<span class="jxr_string">"] with message ["</span> + message + <span class="jxr_string">"]"</span>);<a name="274" href="#274">274</a>     }<a name="275" href="#275">275</a> <a name="276" href="#276">276</a>     <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">class</strong> <a href="../../../../../org/apache/mina/filter/logging/MdcInjectionFilterTest.html">SimpleIoHandler</a> <strong class="jxr_keyword">extends</strong> IoHandlerAdapter {<a name="277" href="#277">277</a> <a name="278" href="#278">278</a>         CountDownLatch sessionIdleLatch = <strong class="jxr_keyword">new</strong> CountDownLatch(2);<a name="279" href="#279">279</a>         CountDownLatch sessionClosedLatch = <strong class="jxr_keyword">new</strong> CountDownLatch(2);<a name="280" href="#280">280</a>         CountDownLatch messageSentLatch = <strong class="jxr_keyword">new</strong> CountDownLatch(2);<a name="281" href="#281">281</a> <a name="282" href="#282">282</a>         @Override<a name="283" href="#283">283</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> sessionCreated(IoSession session) <strong class="jxr_keyword">throws</strong> Exception {<a name="284" href="#284">284</a>             logger.info(<span class="jxr_string">"sessionCreated"</span>);<a name="285" href="#285">285</a>             session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 1);<a name="286" href="#286">286</a>         }<a name="287" href="#287">287</a> <a name="288" href="#288">288</a>         @Override<a name="289" href="#289">289</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> sessionOpened(IoSession session) <strong class="jxr_keyword">throws</strong> Exception {<a name="290" href="#290">290</a>             logger.info(<span class="jxr_string">"sessionOpened"</span>);<a name="291" href="#291">291</a>         }<a name="292" href="#292">292</a> <a name="293" href="#293">293</a>         @Override<a name="294" href="#294">294</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> sessionClosed(IoSession session) <strong class="jxr_keyword">throws</strong> Exception {<a name="295" href="#295">295</a>             logger.info(<span class="jxr_string">"sessionClosed"</span>);<a name="296" href="#296">296</a>             sessionClosedLatch.countDown();<a name="297" href="#297">297</a>         }<a name="298" href="#298">298</a> <a name="299" href="#299">299</a>         @Override<a name="300" href="#300">300</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> sessionIdle(IoSession session, IdleStatus status) <strong class="jxr_keyword">throws</strong> Exception {<a name="301" href="#301">301</a>             logger.info(<span class="jxr_string">"sessionIdle"</span>);<a name="302" href="#302">302</a>             sessionIdleLatch.countDown();<a name="303" href="#303">303</a>             session.close();<a name="304" href="#304">304</a>         }<a name="305" href="#305">305</a> <a name="306" href="#306">306</a>         @Override<a name="307" href="#307">307</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> exceptionCaught(IoSession session, Throwable cause) <strong class="jxr_keyword">throws</strong> Exception {<a name="308" href="#308">308</a>             logger.info(<span class="jxr_string">"exceptionCaught"</span>, cause);<a name="309" href="#309">309</a>         }<a name="310" href="#310">310</a> <a name="311" href="#311">311</a>         @Override<a name="312" href="#312">312</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> messageReceived(IoSession session, Object message) <strong class="jxr_keyword">throws</strong> Exception {<a name="313" href="#313">313</a>             logger.info(<span class="jxr_string">"messageReceived-1"</span>);<a name="314" href="#314">314</a>             <em class="jxr_comment">// adding a custom property to the context</em><a name="315" href="#315">315</a>             String user = <span class="jxr_string">"user-"</span> + message;<a name="316" href="#316">316</a>             MdcInjectionFilter.setProperty(session, <span class="jxr_string">"user"</span>, user);<a name="317" href="#317">317</a>             logger.info(<span class="jxr_string">"messageReceived-2"</span>);<a name="318" href="#318">318</a>             session.write(message);<a name="319" href="#319">319</a>             <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> RuntimeException(<span class="jxr_string">"just a test, forcing exceptionCaught"</span>);<a name="320" href="#320">320</a>         }<a name="321" href="#321">321</a> <a name="322" href="#322">322</a>         @Override<a name="323" href="#323">323</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> messageSent(IoSession session, Object message) <strong class="jxr_keyword">throws</strong> Exception {<a name="324" href="#324">324</a>             logger.info(<span class="jxr_string">"messageSent-1"</span>);<a name="325" href="#325">325</a>             MdcInjectionFilter.removeProperty(session, <span class="jxr_string">"user"</span>);<a name="326" href="#326">326</a>             logger.info(<span class="jxr_string">"messageSent-2"</span>);<a name="327" href="#327">327</a>             messageSentLatch.countDown();<a name="328" href="#328">328</a>         }<a name="329" href="#329">329</a>     }<a name="330" href="#330">330</a> <a name="331" href="#331">331</a>     <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">class</strong> <a href="../../../../../org/apache/mina/filter/logging/MdcInjectionFilterTest.html">DummyProtocolCodecFactory</a> implements ProtocolCodecFactory {<a name="332" href="#332">332</a> <a name="333" href="#333">333</a>         <strong class="jxr_keyword">public</strong> ProtocolEncoder getEncoder(IoSession session) <strong class="jxr_keyword">throws</strong> Exception {<a name="334" href="#334">334</a>             <strong class="jxr_keyword">return</strong> <strong class="jxr_keyword">new</strong> ProtocolEncoderAdapter() {<a name="335" href="#335">335</a>                 <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> encode(IoSession session, Object message, ProtocolEncoderOutput out) <strong class="jxr_keyword">throws</strong> Exception {<a name="336" href="#336">336</a>                     logger.info(<span class="jxr_string">"encode"</span>);<a name="337" href="#337">337</a>                     IoBuffer buffer = IoBuffer.allocate(4).putInt(123).flip();<a name="338" href="#338">338</a>                     out.write(buffer);<a name="339" href="#339">339</a>                 }<a name="340" href="#340">340</a>             };<a name="341" href="#341">341</a>         }<a name="342" href="#342">342</a> <a name="343" href="#343">343</a>         <strong class="jxr_keyword">public</strong> ProtocolDecoder getDecoder(IoSession session) <strong class="jxr_keyword">throws</strong> Exception {<a name="344" href="#344">344</a>             <strong class="jxr_keyword">return</strong> <strong class="jxr_keyword">new</strong> ProtocolDecoderAdapter() {<a name="345" href="#345">345</a>                 <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) <strong class="jxr_keyword">throws</strong> Exception {<a name="346" href="#346">346</a>                     <strong class="jxr_keyword">if</strong> (in.remaining() &gt;= 4) {<a name="347" href="#347">347</a>                         <strong class="jxr_keyword">int</strong> value = in.getInt();<a name="348" href="#348">348</a>                         logger.info(<span class="jxr_string">"decode"</span>);<a name="349" href="#349">349</a>                         out.write(value);<a name="350" href="#350">350</a>                     }<a name="351" href="#351">351</a>                 }<a name="352" href="#352">352</a>             };<a name="353" href="#353">353</a>         }<a name="354" href="#354">354</a>     }<a name="355" href="#355">355</a> <a name="356" href="#356">356</a>     <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">class</strong> <a href="../../../../../org/apache/mina/filter/logging/MdcInjectionFilterTest.html">MyAppender</a> <strong class="jxr_keyword">extends</strong> AppenderSkeleton {<a name="357" href="#357">357</a> <a name="358" href="#358">358</a>         List&lt;LoggingEvent&gt; events = Collections.synchronizedList(<strong class="jxr_keyword">new</strong> ArrayList&lt;LoggingEvent&gt;());<a name="359" href="#359">359</a> <a name="360" href="#360">360</a>         @Override<a name="361" href="#361">361</a>         <strong class="jxr_keyword">protected</strong> <strong class="jxr_keyword">void</strong> append(<strong class="jxr_keyword">final</strong> LoggingEvent loggingEvent) {<a name="362" href="#362">362</a>             loggingEvent.getMDCCopy();<a name="363" href="#363">363</a>             events.add(loggingEvent);<a name="364" href="#364">364</a>         }<a name="365" href="#365">365</a> <a name="366" href="#366">366</a>         @Override<a name="367" href="#367">367</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">boolean</strong> requiresLayout() {<a name="368" href="#368">368</a>             <strong class="jxr_keyword">return</strong> false;<a name="369" href="#369">369</a>         }<a name="370" href="#370">370</a> <a name="371" href="#371">371</a>         @Override<a name="372" href="#372">372</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> close() {<a name="373" href="#373">373</a>         }<a name="374" href="#374">374</a>     }<a name="375" href="#375">375</a> <a name="376" href="#376">376</a>     <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">class</strong> <a href="../../../../../org/apache/mina/filter/logging/MdcInjectionFilterTest.html">DummyIoFilter</a> <strong class="jxr_keyword">extends</strong> IoFilterAdapter {<a name="377" href="#377">377</a>         @Override<a name="378" href="#378">378</a>         <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> sessionOpened(NextFilter nextFilter, IoSession session) <strong class="jxr_keyword">throws</strong> Exception {<a name="379" href="#379">379</a>             logger.info(<span class="jxr_string">"DummyIoFilter.sessionOpened"</span>);<a name="380" href="#380">380</a>             nextFilter.sessionOpened(session);<a name="381" href="#381">381</a>         }<a name="382" href="#382">382</a>     }<a name="383" href="#383">383</a> <a name="384" href="#384">384</a> }</pre><hr/><div id="footer">This page was automatically generated by <a href="http://maven.apache.org/">Maven</a></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -