首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

liferay过滤器(2)

2012-07-25 
liferay过滤器(二)liferay的过滤器基本上都可以在portal-ext.properties文件里面进行设置是否使用(true or

liferay过滤器(二)

liferay的过滤器基本上都可以在portal-ext.properties文件里面进行设置是否使用(true or false)

?

1、GZipFilter过滤器

?

主要代码:

if (isCompress(request) && !isInclude(request) &&
???BrowserSnifferUtil.acceptsGzip(request) &&
???!isAlreadyFiltered(request)) {

???if (_log.isDebugEnabled()) {
????String completeURL = HttpUtil.getCompleteURL(request);

????_log.debug("Compressing " + completeURL);
???}

???request.setAttribute(SKIP_FILTER, Boolean.TRUE);

???GZipResponse gZipResponse = new GZipResponse(response);

???processFilter(GZipFilter.class, request, gZipResponse, filterChain);

???gZipResponse.finishResponse();
??}
??else {
???if (_log.isDebugEnabled()) {
????String completeURL = HttpUtil.getCompleteURL(request);

????_log.debug("Not compressing " + completeURL);
???}

???processFilter(
????GZipFilter.class, request, response, filterChain);
??}

?

主要功能:截获输出进行处理,写入到真正的HttpServletResponse中(在源代码中没有找到处理的代码,压缩处理代码参看:http://blog.csdn.net/hongweigg/article/details/6197445)

?

2、Header Filter过滤器

主要代码:

Enumeration<String> enu = _filterConfig.getInitParameterNames();

??while (enu.hasMoreElements()) {
???String name = enu.nextElement();

???if (name.equals(_URL_REGEX_PATTERN)) {
????continue;
???}

???String value = _filterConfig.getInitParameter(name);

???if (name.equals(_EXPIRES) && Validator.isNumber(value)) {
????int seconds = GetterUtil.getInteger(value);

????Calendar cal = new GregorianCalendar();

????cal.add(Calendar.SECOND, seconds);

????value = _dateFormat.format(cal.getTime());
???}

???// LEP-5895

???boolean addHeader = true;

???if (name.equalsIgnoreCase(HttpHeaders.CACHE_CONTROL)) {
????HttpSession session = request.getSession(false);

????if ((session == null) || session.isNew()) {
?????addHeader = false;
????}
???}

???if (addHeader) {
????response.addHeader(name, value);
???}
??}

??long lastModified = getLastModified(request);
??long ifModifiedSince = request.getDateHeader(
???HttpHeaders.IF_MODIFIED_SINCE);

??if (lastModified > 0) {
???response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);

???if (lastModified <= ifModifiedSince) {
????response.setDateHeader(
?????HttpHeaders.LAST_MODIFIED, ifModifiedSince);
????response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);

????return;
???}
??}
功能:添加页面过期时间的参数,根据filter config属性文件中的Expires

?

3、I18n 过滤器

主要代码:

String contextPath = PortalUtil.getPathContext();

??String requestURI = request.getRequestURI();

??if ((Validator.isNotNull(contextPath)) &&
???(requestURI.indexOf(contextPath) != -1)) {

???requestURI = requestURI.substring(contextPath.length());
??}

??requestURI = StringUtil.replace(
???requestURI, StringPool.DOUBLE_SLASH, StringPool.SLASH);

??String i18nLanguageId = requestURI.substring(1);

??int pos = requestURI.indexOf(StringPool.SLASH, 1);

??if (pos != -1) {
???i18nLanguageId = i18nLanguageId.substring(0, pos - 1);
??}

??if (_languageIds.contains(i18nLanguageId)) {
???return null;
??}

??String defaultLanguageId = LocaleUtil.toLanguageId(
???LocaleUtil.getDefault());

??String guestLanguageId = CookieKeys.getCookie(
???request, CookieKeys.GUEST_LANGUAGE_ID);

??if (Validator.isNull(guestLanguageId)) {
???guestLanguageId = defaultLanguageId;
??}

??if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 1) {
???if (!defaultLanguageId.equals(guestLanguageId)) {
????i18nLanguageId = guestLanguageId;
???}
???else {
????return null;
???}
??}
??else if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 2) {
???i18nLanguageId = guestLanguageId;
??}

??if (i18nLanguageId == null) {
???return null;
??}

??String i18nPath = StringPool.SLASH + i18nLanguageId;

??Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId);

??if (!LanguageUtil.isDuplicateLanguageCode(locale.getLanguage())) {
???i18nPath = StringPool.SLASH + locale.getLanguage();
??}
??else {
???Locale priorityLocale = LanguageUtil.getLocale(
????locale.getLanguage());

???if (locale.equals(priorityLocale)) {
????i18nPath = StringPool.SLASH + locale.getLanguage();
???}
??}

??String redirect = contextPath + i18nPath + requestURI;

??LayoutSet layoutSet = (LayoutSet)request.getAttribute(
???WebKeys.VIRTUAL_HOST_LAYOUT_SET);

??if ((layoutSet != null) &&
???(requestURI.startsWith(_PRIVATE_GROUP_SERVLET_MAPPING) ||
??? requestURI.startsWith(_PRIVATE_USER_SERVLET_MAPPING) ||
??? requestURI.startsWith(_PUBLIC_GROUP_SERVLET_MAPPING))) {

???int x = requestURI.indexOf(StringPool.SLASH, 1);

???if (x == -1) {

????// /web

????requestURI += StringPool.SLASH;

????x = requestURI.indexOf(StringPool.SLASH, 1);
???}

???int y = requestURI.indexOf(StringPool.SLASH, x + 1);

???if (y == -1) {

????// /web/alpha

????requestURI += StringPool.SLASH;

????y = requestURI.indexOf(StringPool.SLASH, x + 1);
???}

???String groupFriendlyURL = requestURI.substring(x, y);

???Group group = layoutSet.getGroup();

???if (group.getFriendlyURL().equals(groupFriendlyURL)) {
????redirect = contextPath + i18nPath + requestURI.substring(y);
???}
??}

??String queryString = request.getQueryString();

??if (Validator.isNotNull(queryString)) {
???redirect += StringPool.QUESTION + request.getQueryString();
??}

??return redirect;

?

主要功能:重新组装重定向URL(设置语言的ID)。

?

4、Language过滤器

主要代码:

HttpServletRequest request, StringServletResponse stringResponse) {

??String languageId = LanguageUtil.getLanguageId(request);
??Locale locale = LocaleUtil.fromLanguageId(languageId);

??String content = stringResponse.getString();

??StringBundler sb = new StringBundler();

??Matcher matcher = _pattern.matcher(content);

??int x = 0;

??while (matcher.find()) {
???int y = matcher.start(0);

???String key = matcher.group(1);

???sb.append(content.substring(x, y));
???sb.append(StringPool.APOSTROPHE);

???String value = null;

???if (_portletConfig != null) {
????value = UnicodeLanguageUtil.get(_portletConfig, locale, key);
???}
???else {
????value = UnicodeLanguageUtil.get(locale, key);
???}

???sb.append(value);
???sb.append(StringPool.APOSTROPHE);

???x = matcher.end(0);
??}

??sb.append(content.substring(x));

??return sb.toString();

功能:取得语言的id,根据id设置语言name,写到response中

?

5、Minifier过滤器

主要代码:

?

热点排行