Liferay静态发布功能

来源:百度文库 编辑:神马文学网 时间:2024/04/29 00:39:14
这个是我发布的Liferay生成静态页面的功能代码,演示站点(demo):http://www.blueauk.com
step 1:首先建立一个folder放所有的html文件。本例子是htmls
step 2:增加一个类Tools.java 这个文件的功能是读取指定group中的layout,然后生成一个html文件放在htmls下面,并且保持目录结构不变。
step 3:修改FriendlyURLServlet,在这个文件中加入读取对应html文件的逻辑。
step 4:增加一个job监听(ServletContextListener),它的功能是定时启动产生html的startGen方法。。。(这个类可以自己来写)
下面是对应的类 和方法:
1package com.blueauk.util;
2
3import java.io.*;
4import java.net.*;
5import java.util.*;
6import javax.servlet.*;
7import javax.servlet.http.HttpServletRequest;
8
9import com.liferay.portal.kernel.util.StringMaker;
10import com.liferay.portal.model.Layout;
11import com.liferay.portal.service.LayoutLocalServiceUtil;
12import com.liferay.portal.theme.ThemeDisplay;
13import com.liferay.portal.util.PortalUtil;
14
15public class Tools {
16 final static Object lock = new Object();
17
18 public static void startGen() {
19       try {
20          String dir = (Tools.class.getClassLoader().getResource(""))
21                   .toString();
22          String endDir = dir.substring(6, dir.length() - 16);
23          File file = new File(endDir);
24          if (!file.exists()) {
25             file.mkdir();
26          }
27          List genHtmlURL = htmlAll();
28
29          for (int i = 0; i < genHtmlURL.size(); i++) {
30             String Url = (String) genHtmlURL.get(i);
31             //在这里windows和linux的系统路径是不一样的,要注意!
32             String filedir = htmlmkdir(endDir + Url);
33             //根据实际情况做适当修改
34             makeHtml("http://localhost:8080" + Url, "" + filedir
35                      + Url.substring(Url.lastIndexOf("/") + 1) + ".html");
36          }
37       } catch (Exception e) {
38
39       }
40 }
41 private static List _buildSiteMapURL(List layouts, int displayDepth,
42          int curDepth, ThemeDisplay themeDisplay, StringMaker sm)
43          throws Exception {
44       List urlall = new ArrayList();
45       for (int i = 0; i < layouts.size(); i++) {
46          Layout layout = (Layout) layouts.get(i);
47          String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
48          urlall.add(layoutURL);
49          if ((displayDepth == 0) || (displayDepth > curDepth)) {
50             _buildSiteMapURL(layout.getChildren(), displayDepth,
51                      curDepth + 1, themeDisplay, sm);
52          }
53       }
54       return urlall;
55 }
56
57 private static List htmlAll() {
58       try {
59          List rootLayoutsURL = null;
60          StringMaker sm = new StringMaker();
61          //Advertent this Group
62          rootLayoutsURL = LayoutLocalServiceUtil.getLayouts(14, false, 0);
63          ThemeDisplay themeDisplayURL = new ThemeDisplay();
64
65          return _buildSiteMapURL(rootLayoutsURL, 0, 1, themeDisplayURL, sm);
66       } catch (Exception e) {
67
68       }
69       return null;
70 }
71 private static void makeHtml(String page, String filePath) {
72       makeHtml(page, filePath, "UTF-8");
73 }
74 private static String htmlmkdir(String url) {
75       String bb = url.replace("/web/guest", "htmls");
76
77       String aaa[] = bb.split("/");
78
79       String dir = "";
80       for (int i = 0; i < aaa.length - 1; i++) {
81          dir += aaa;
82
83          File file = new File(dir);
84
85          if (!file.exists()) {
86             file.mkdir();
87          }
88          dir += "/";
89       }
90       return dir;
91 }
92
93 private static void makeHtml(String page, String filePath, String chartset) {
94       synchronized (lock) {
95          HttpURLConnection huc = null;
96          BufferedReader br = null;
97          BufferedWriter bw = null;
98          try {
99             huc = (HttpURLConnection) new URL(page).openConnection();
100             System.setProperty("sun.net.client.defaultConnectTimeout",
101                      "30000");
102             System
103                      .setProperty("sun.net.client.defaultReadTimeout",
104                               "30000");
105             huc.connect();
106             InputStream stream = huc.getInputStream();
107             bw = new BufferedWriter(new OutputStreamWriter(
108                      new FileOutputStream(filePath), chartset));
109             br = new BufferedReader(new InputStreamReader(stream, chartset));
110             String line;
111             while ((line = br.readLine()) != null) {
112                   if (line.trim().length() > 0) {
113                      bw.write(line);
114                      bw.newLine();
115                   }
116             }
117          } catch (Exception e) {
118             e.printStackTrace();
119          } finally {
120             try {
121                   br.close();
122                   bw.close();
123                   huc.disconnect();
124             } catch (Exception e) {
125                   e.printStackTrace();
126             }
127          }
128       }
129 }
130}
对FriendlyURLServlet的修改如下:line 99 行下面加入如下code
1 String sendRedirectUrl=friendlyURLPath+req.getPathInfo();
2
3       HttpSession ses = req.getSession();
4
5       if (ses.getAttribute("j_username") != null &&
6          ses.getAttribute("j_password") != null) {
7          System.out.println("you are login!");
8       }else{
9
10          if(sendRedirectUrl.substring(0, 11).equals("/web/guest/")){
11             String dir = (Tools.class.getClassLoader().getResource(""))
12             .toString();
13             //在这里windows和linux的系统路径是不一样的,要注意!
14             String endDir = dir.substring(6, dir.length() - 16);
15
16             File file=new File(endDir+"htmls/"+sendRedirectUrl.substring(11)+".html");
17             if(file.exists()){
18                   res.sendRedirect("/htmls/"+sendRedirectUrl.substring(11)+".html");
19                   return;
20             }
21
22          }
23
24       }
web.xml增加的监听部分。
1
2
3 com.blueauk.util.GeneratingListener
4