Research on UrlRewriteFilter in Liferay

Source: Internet
Author: User

Introduction:

You may not be familiar with UrlRewriteFilter. Especially those who have done SEO (Search Engine Optimization) know that it will rewrite a url to another URL. Why? Because the search engine looks at dynamic URLs very uncomfortable. What is dynamic URLs? Associate queryParameter, & associate parameters with such URLs, suchHttp: // 1.2.3.4: 3333/badurltest? A = 1 & B = 2 & c = 3This url. We will change it to a static url after rewriting, suchHttp: // 1.2.3.4: 3333/badurltest_1_2_3.htmlIn this case, the search engine looks more comfortable.


So let's take a look at how to rewrite the url in Liferay. Of course, it must be the same as the convention, and it will use filter to complete this work.



Debugging analysis:

Anyone familiar with urlrewrite knows that it generally uses urlrewrite. xml to complete the rewrite rule configuration. We liferay is no exception. It is in$ LIFERAY_TOMCAT_HOME/webapps/ROOT/WEB-INFDirectory:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115424125-0.png "title =" 2013-09-16_095244.png "alt =" 095431711.png"/>

Let's select the simplest one, such as http: // 172.29.175.236: 8080./Tunnel-webTo see if it can be rewritten as http: // 172.29.175.236: 8080/Api


Obviously, this URLRewriteFilter is called by InvokerFilterChain. We enter the doFilter () method: Because the URLRewriteFilter in the sourcecode we get. java cannot match UrlRewriterFilter in the jar package. the class matches, so we can only use jd-gui decompilation to compare it.


When we jumped off the unimportant line, we looked at rows 726th to 728.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/111542M49-1.png "title =" 2.png" alt = "100448493.png"/>

First, 726th rows will be usedProcessRequest (HttpServletRequest, HttpServletResponse)To get the rewrite url. Let's look at its implementation. First, it will remove the first and last blank characters under its trim () In line 68th:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/111542O60-2.png "title =" 3.png" alt = "100938898.png"/>

Trim ()After that, the url is still/Tunnel-web:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115423A0-3.png "title =" 4.png" alt = "101105952.png"/>


Then, it will be used in 84 rowsUTF-8FormatDecodeBecause we all know that the url we first entered has been url encoded.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115422416-4.png "title =" 5.png" alt = "101255709.png"/>

After the decode is completeOriginalUrlStill yes/Tunnel-web:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115424W5-5.png "title =" 6.png" alt = "101358524.png"/>


Then 95th to 98 rows from the requestUrlMediumContextPathPartially removed.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/111542D48-6.png "title =" 7.png" alt = "101517580.png"/>

As shown inContextPathIs "", so after 98 rows are executedOriginalUrlStill yes/Tunnel-web:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/111542E35-7.png "title =" 8.png" alt = "101735908.png"/>


Load the back of originalUrl from queryString on the line:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115425296-8.png "title =" 9.png" alt = "101933750.png"/>

Because we do not query strings, nothing is added.


Then, the 123rd line will get all url rewriting rules and save them in the VariableRulesFromUrlrewrite. xmlTo obtain the configuration information of all rules:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115421Q3-9.png "title =" 10.png" alt = "102215278.png"/>

Because at the top, weUrlrewrite. xmlSix rules are configured, so from the debugging information, hereRulesThe array variable has six elements.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115422622-10.png "title =" 11.png" alt = "102410301.png"/>


Then, the rows from 131-141 will traverse these rule sets in sequence and then call the rules to rewrite the request url. It should be noted that it is sequential execution matching and then overwritten, that is, the next rule uses the output of the previous rule as the input:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115421543-11.png "title =" 12.png" alt = "102633662.png"/>

The process of rewriting is in 135th rows.ExecuteMethod:


Important: details of the execute () method:

Let's take a look at our example because we have defined the following rules:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115421L9-12.png "title =" 13.png" alt = "102732364.png"/>

Therefore, when a requestUrlYes/Tunnel-webWhen the above 134th rows are executed, it starts fromUrlrewrite. xmlThe current rule matches the above definition ):

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115422625-13.png "title =" 14.png" alt = "103369166.png"/>


Then, the 135th lines will callRule.exe cute ()Method to specifically execute rewrite:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115425196-14.png "title =" 16.png" alt = "103906745.png"/>

It passesExecuteBase ()Method to obtainRuleExecutionOutputObject.MatcherObject. If it matches, replace the rewriting content of the matching Regular Expression in <from> () with the rewrite parameters $1, $2, and, $3. Finally, we can see through the debugging information that it calculates the final Matching target: 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115421247-15.png "title =" 17.png" alt = "104033663.png"/>

Here we can see that itsReplaceUrlContains the target to be matched, andRuleMatchedIsTrueIndicates that the url rewriting rule matches the current url.


WhileExecute ()Method will be called eventuallyRewriteRequest ()Method:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/111542E29-16.png "title =" 15.png" alt = "103731767.png"/>

We can see from this that because of ourTo typeSet the string"Permanent-redirect"According to the following rules:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/111542CJ-17.png "title =" 18.png" alt = "2017543826.png"/>

OurToTypeSet2.


ThereforeRewriteRequestLine 99-103.RewrittenRequestOfPermanentRedirectSetTrue.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11154252c-18.png "title =" 19.png" alt = "104849610.png"/>


Then go back to the UrlRewriter'sProcessRequest ()Method, because we haveExecute ()Return in MethodRewrittenUrl,ItsTargetYes/Api:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115426333-19.png "title =" 20.png" alt = "105655336.png"/>

Therefore, the target will be taken out in the 139th line and used as the finalFinalToUrl.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1115423613-20.png "title =" 21.png" alt = "105735395.png"/>



Summary:

Although this piece of debugging is not technically difficult, we can still sum up some valuable conclusions:

(1) Liferay Url rewriting is completed through UrlRewriteFilter, and this UrlRewriteFilter will be called by InvokerFilterChain.

(2) rewrite rules are defined in the $ LIFERAY_TOMCAT_HOME/webapps/ROOT/WEB-INF/urlrewrite. xml file.

(3) The rewriting process is as follows: first, it will request the url trim () and then decodes its UTF-8 into the original url, the contextPath section is removed and the query string section is added to the end to obtain the input url before rewriting. Then, it will. urlrewrite. all Rewrite Rules in xml are read and stored in the Rule array. traversing the array contains an application Rule for the previous url, Which is recursive, the output of a rule is the input of the next rule. The output url after the final rewriting is obtained only after all the rules are applied.

(4) When an application rewrite rule, it is divided into two parts. First, it will apply general rule replacement in the base class (RuleBase, convert the content of all requests that match the regular expression enclosed in brackets in <from> to the mode specified by the <to> rewrite parameter. Secondly, it reads the type attribute in <to> and determines how to post-process the rewritten url based on the value of the type attribute.

(5) There are 6 types of type attributes: redirect, permanent-redirect, temporary-redirect, pre-include, post-include, forward, we can customize it based on different needs.

This article from "parallel line cohesion" blog, please be sure to keep this source http://supercharles888.blog.51cto.com/609344/1297612

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.