The application of dynamic Agent in web and JDBC Development (Web article)

Source: Internet
Author: User

Web case

There is currently a 2005, STRUTS1-based Web project A, whose validation part relies on Master SSO (Single Sign-on). When requesting site A, the user will be forced to take the SSO authentication, after authentication, the master will automatically forward the request to the a site, and in the request header to save the login user ID of the new attribute Sm_user, and a site based on the user ID to provide the corresponding service. Since the project is a saved item, there are a lot of residual test code like the following.

String user_id = Request.getheader ("Xx_user"), if (user_id = = null) {    user_id = "my_hard_coded_user_id";} UserProfile userprofile = new Bizdao (). GetUserProfile (USER_ID);

The root cause is that it is not possible to have SSO docking in the production environment at the time of local testing, and only after submitting the code to the public dev server, UAT server or PROD server can you enjoy the Xx_user data provided by SSO as a station, so the programmer cannot get the header data. The local environment is hard-coded directly and simply rudely. In a small team of only a few people, this kind of processing may seem to be indifferent, but the number of people who have handled it quite a lot, many are accustomed to use their own ID for testing, so in the historical version of SVN, the hard-coded ID from a to B, change to C, to D ...

Problem analysis

Everyone chooses to use their own ID or someone else's ID according to their preferences, so is there a way to unify the interface once and for all? Probably the most easy to think of is httpservletrequest.setheader, unfortunately HttpServletRequest does not have such an API, why? The personal guess may be that the request originates from the client, and the server side should maintain the original, pure, and non-toxic nature of the requests, while HttpServletResponse (http://docs.oracle.com/javaee/6/api/javax/ servlet/http/httpservletresponse.html) is under the control of the application, so programmers can arbitrarily trample on it, SetHeader, AddHeader and GetHeader, Getheaders, Getheadernames. Since there is no API available, go directly to the packaging agent, with Google's help we can find a ready-made solution to the header in the request to re-customize, focusing on the implementation of the Httpservletrequestwrapper class.

http://vangjee.wordpress.com/2009/02/25/how-to-modify-request-headers-in-a-j2ee-web-application/

In fact, this program was found after I implemented the dynamic agent scheme, the two ideas are almost the same, are the original request for the packaging agent, re-implementation of the GetHeader method.

Solution Solutions

No nonsense, just on the code!

private static class Requestinvocationhandler implements Invocationhandler {private HttpServletRequest wrappedrequest;    Public Requestinvocationhandler (HttpServletRequest r) {wrappedrequest = R;    } public static String Dummydata = "my_hard_coded_id"; public object invoke (object proxy, Method method, object[] args) throws Throwable {if ("GetHeader". Equals (METHOD.G        Etname ()) && args.length = = 1 && "Sm_user". Equals (Args[0]) {return dummydata;    } return Method.invoke (Wrappedrequest, args);    } public static HttpServletRequest Createrequestwapper (HttpServletRequest R) {if (Null! = R.getparameter ("U")) {    Dummydata = R.getparameter ("U"); } return (HttpServletRequest) (Proxy.newproxyinstance (HttpServletRequest.class.getClassLoader (), New Clas    S[] {Httpservletrequest.class}, new Requestinvocationhandler (R)); }}

The above code implements the proxy for the request object, and all calls to the request object need to go through the Invoke method. In the Invoke method, we can make finer-grained control of different method signatures. For example, the problem mentioned in the Web case can be customized specifically for the GetHeader method, and there is no such data in the request header, but we can "build" the data abruptly. In addition to "build" the test data, but also can be carried in the request of the parameters "U" Dynamic Data modification, so that the user Switching function.

Now that the problem with the proxy class has been solved, here's how to implant the proxy object. When and where it can be understood as a cut-in time, In fact, the initial is to implant the proxy object in the Org.apache.struts.action.RequestProcessor, but in the use of the implementation has found that the disadvantage is that the non-struts request can not use proxy objects, such as direct access to the JSP file or other servlet The service path that is provided. At this point it is natural to think of filter, and the use of filter is a pain, adding a brand-new filter bar, a bit of damage to the overall design of the feeling, and put into other filter, it looks nondescript. But at least for testing purposes, these two options can be compromised by one of the following.

public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, Servletexceptionrequest = Requestinvocationhandler.createrequestwapper ((httpservletrequest) request); Chain.dofilter (request, response);}

The overall workflow is as follows, the user makes the request first, and then the requests object is replaced with the proxy object in the filter, and the replacement request is passed into the Dofilter method, and then either servlet, struts, or JSP. All of them use our custom request object.

           +---------+         +---------+           |         |         |         | Request   |         | +-----> | Servlet |+--------> |  Filter |         | | |           +----+  |         |  Struts |           | Request |         |         | Response  |      is |         |         | <--------+ | Wrapped           |    | | | Here | <-----+         |           | | | | | +---------+         +---------+
Postscript

Of course, if you want to modify the request object more than this method, such as the Orthodox way is to define a httpservletrequestwrapper to redefine the request, the specific case please refer to HTTP// vangjee.wordpress.com/2009/02/25/how-to-modify-request-headers-in-a-j2ee-web-application/

Application of dynamic Proxy in web and JDBC Development (Web article)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.