[jboss-svn-commits] JBL Code SVN: r15657 - in labs/jbosslabs/trunk/portal-extensions/jbosswiki: forge-wiki/src/java/org/jboss/wiki/portlet and 3 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Oct 8 14:14:17 EDT 2007


Author: szimano
Date: 2007-10-08 14:14:17 -0400 (Mon, 08 Oct 2007)
New Revision: 15657

Modified:
   labs/jbosslabs/trunk/portal-extensions/jbosswiki/forge-wiki/src/java/org/jboss/wiki/attributesextractor/impl/PortletAttributesExctractor.java
   labs/jbosslabs/trunk/portal-extensions/jbosswiki/forge-wiki/src/java/org/jboss/wiki/portlet/WikiPortletMyFaces.java
   labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/project.xml
   labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/credentials/LiferayCredentials.java
   labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/management/EmailNotifierImpl.java
Log:
wiki attributes  NPE fix and email notifications returned

Modified: labs/jbosslabs/trunk/portal-extensions/jbosswiki/forge-wiki/src/java/org/jboss/wiki/attributesextractor/impl/PortletAttributesExctractor.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/jbosswiki/forge-wiki/src/java/org/jboss/wiki/attributesextractor/impl/PortletAttributesExctractor.java	2007-10-08 18:08:53 UTC (rev 15656)
+++ labs/jbosslabs/trunk/portal-extensions/jbosswiki/forge-wiki/src/java/org/jboss/wiki/attributesextractor/impl/PortletAttributesExctractor.java	2007-10-08 18:14:17 UTC (rev 15657)
@@ -40,8 +40,8 @@
 import org.jboss.wiki.credentials.Credentials;
 import org.jboss.wiki.exception.WikiUserPreferenceException;
 import org.jboss.wiki.portlet.PortletWikiSession;
+import org.jboss.wiki.portlet.WikiPortletMyFaces;
 
-
 /**
  * PortletAttributesExctractor.java
  * 
@@ -78,11 +78,9 @@
 	private String userDefaultLang;
 
 	private String userDefaultPage;
-	
 
 	public PortletAttributesExctractor(Object request, WikiEngine wikiEngine) {
-		
-		
+
 		if (request instanceof PortletRequest) {
 			PortletRequest rReq = (PortletRequest) request;
 
@@ -101,17 +99,16 @@
 
 			wikiSession = new PortletWikiSession(rReq.getPortletSession());
 
-			credentials = wikiEngine.getCredentialsInstance(
-					request);
+			credentials = wikiEngine.getCredentialsInstance(request);
 
 			secure = rReq.isSecure();
 
 			page = rReq.getParameter("page");
 
 			language = rReq.getParameter("language");
-			
-			log.debug("Version = "+rReq.getParameter("version"));
 
+			log.debug("Version = " + rReq.getParameter("version"));
+
 			pageVersion = (rReq.getParameter("version") != null) ? Integer
 					.valueOf(rReq.getParameter("version")) : null;
 
@@ -164,15 +161,23 @@
 
 			String jsfUrl = portletURL.toString();
 
-			log.debug("ViewID: "
-					+ FacesContext.getCurrentInstance().getViewRoot()
-							.getViewId());
+			FacesContext fctx = FacesContext.getCurrentInstance();
+
+			if (fctx.getViewRoot() != null) {
+				log.debug("ViewID: " + fctx.getViewRoot().getViewId());
+			} else {
+				log.debug("ViewID not available");
+			}
+
 			try {
 				jsfUrl += "&"
 						+ MyFacesGenericPortlet.VIEW_ID
 						+ "="
-						+ URLEncoder.encode(FacesContext.getCurrentInstance()
-								.getViewRoot().getViewId(), "UTF-8");
+						+ URLEncoder
+								.encode((fctx.getViewRoot() != null) ? fctx
+										.getViewRoot().getViewId()
+										: WikiPortletMyFaces.DEFAULT_VIEW_LINK,
+										"UTF-8");
 			} catch (UnsupportedEncodingException e) {
 				log.error(e);
 			}
@@ -234,7 +239,7 @@
 				rReq.getPreferences().setValue(USER_LANG, lang);
 
 				rReq.getPreferences().store();
-				
+
 				this.userDefaultLang = lang;
 			} catch (Exception e) {
 				throw new WikiUserPreferenceException(e);
@@ -253,7 +258,7 @@
 
 			try {
 				rReq.getPreferences().setValue(USER_PAGE, page);
-				
+
 				rReq.getPreferences().store();
 
 				this.userDefaultPage = page;

Modified: labs/jbosslabs/trunk/portal-extensions/jbosswiki/forge-wiki/src/java/org/jboss/wiki/portlet/WikiPortletMyFaces.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/jbosswiki/forge-wiki/src/java/org/jboss/wiki/portlet/WikiPortletMyFaces.java	2007-10-08 18:08:53 UTC (rev 15656)
+++ labs/jbosslabs/trunk/portal-extensions/jbosswiki/forge-wiki/src/java/org/jboss/wiki/portlet/WikiPortletMyFaces.java	2007-10-08 18:14:17 UTC (rev 15657)
@@ -36,6 +36,7 @@
 import javax.portlet.PortletException;
 import javax.portlet.RenderRequest;
 import javax.portlet.RenderResponse;
+import javax.portlet.UnavailableException;
 
 import org.apache.commons.fileupload.FileItem;
 import org.apache.commons.fileupload.FileUploadException;
@@ -62,6 +63,8 @@
 			.getLogger(WikiPortletMyFaces.class);
 
 	private static final String EDIT_VIEW = "/views/wiki_preferences.xhtml";
+	
+	public static String DEFAULT_VIEW_LINK;
 
 	protected void facesRender(RenderRequest request, RenderResponse response)
 			throws PortletException, java.io.IOException {
@@ -96,9 +99,9 @@
 
 		for (String key : (Set<String>) paramMap.keySet()) {
 			if (key.matches("var[123456789]\\d*")) {
+				FacesContext facesContext = facesContext(request, response);
 
-				Map map = FacesContext.getCurrentInstance()
-						.getExternalContext().getApplicationMap();
+				Map map = facesContext.getExternalContext().getApplicationMap();
 				Injector injector = (Injector) map
 						.get(Injector.class.getName());
 				injector.getInstance(AttributesExtractor.class)
@@ -284,4 +287,11 @@
 
 	}
 
+	@Override
+	public void init() throws PortletException, UnavailableException {
+		DEFAULT_VIEW_LINK = getInitParameter(DEFAULT_VIEW);
+		
+		super.init();
+	}
+
 }

Modified: labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/project.xml
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/project.xml	2007-10-08 18:08:53 UTC (rev 15656)
+++ labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/project.xml	2007-10-08 18:14:17 UTC (rev 15657)
@@ -202,5 +202,12 @@
             <jar>liferay-portal-kernel.jar</jar>
         </dependency>
         
+        <dependency>
+	        <groupId>jboss</groupId>
+	        <artifactId>jboss-mail</artifactId>
+	        <version>1.0</version>
+	        <jar>mail.jar</jar>
+	    </dependency>
+        
 	</dependencies>
 </project>

Modified: labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/credentials/LiferayCredentials.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/credentials/LiferayCredentials.java	2007-10-08 18:08:53 UTC (rev 15656)
+++ labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/credentials/LiferayCredentials.java	2007-10-08 18:14:17 UTC (rev 15657)
@@ -6,7 +6,9 @@
 import java.util.Set;
 import java.util.TreeSet;
 
+import javax.portlet.PortletRequest;
 import javax.portlet.RenderRequest;
+import javax.servlet.http.HttpServletRequest;
 
 import org.apache.log4j.Logger;
 
@@ -14,11 +16,21 @@
 import com.liferay.portal.SystemException;
 import com.liferay.portal.model.EmailAddress;
 import com.liferay.portal.model.Role;
+import com.liferay.portal.model.User;
 import com.liferay.portal.service.EmailAddressServiceUtil;
 import com.liferay.portal.service.RoleLocalServiceUtil;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.liferay.portal.util.PortalUtil;
+import com.liferay.portal.util.WebKeys;
+import com.liferay.portlet.ActionRequestImpl;
+import com.liferay.portlet.RenderRequestImpl;
 
 public class LiferayCredentials extends Credentials {
 
+	public static final String COMPANY_ID = "COMPANY_ID";
+
+	public static final String USER_ID = "USER_ID";
+
 	private boolean isLogged = false;
 
 	private boolean isAdmin = false;
@@ -34,20 +46,37 @@
 
 	private static final String LIFERAY_ADMIN = "Administrator";
 
-	private static final String LIFERAY_EMAIL = "emailAddressId";
-
 	@Override
 	public Set<String> getAllRoles() {
-		// List results = RoleLocalServiceUtil
-		// .search(0, null, null, null, null, 0);
-		//
-		// Set<String> allRoles = new TreeSet<String>();
-		//
-		// for (Object o : results) {
-		// allRoles.add(((Role) o).getName());
-		// }
-		// return allRoles;
-		return Collections.singleton(LIFERAY_ADMIN);
+
+		Long companyIdObj = (Long) getHttpServletRequest(rreq).getAttribute(
+				COMPANY_ID);
+		long companyId = 0;
+
+		if (companyIdObj != null) {
+			companyId = companyIdObj.longValue();
+		}
+
+		try {
+			List results = RoleLocalServiceUtil.search(companyId, null, null,
+					null, 0, Integer.MAX_VALUE);
+
+			Set<String> allRoles = new TreeSet<String>();
+
+			for (Object o : results) {
+				allRoles.add(((Role) o).getName());
+			}
+			return allRoles;
+
+		} catch (SystemException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+		return null;
+
+		
+		// return Collections.singleton(LIFERAY_ADMIN);
 	}
 
 	@Override
@@ -95,34 +124,67 @@
 	}
 
 	private String getEmailLiferay(RenderRequest req) {
+		try {
+			User u = getUser();
 
-		if (req.getParameter(LIFERAY_EMAIL) != null) {
-			Long emailAddressId = Long.parseLong(req
-					.getParameter(LIFERAY_EMAIL));
+			return u.getEmailAddress();
 
-			EmailAddress emailAddress = null;
+		} catch (PortalException e1) {
+			// TODO Auto-generated catch block
+			e1.printStackTrace();
+		} catch (SystemException e1) {
+			// TODO Auto-generated catch block
+			e1.printStackTrace();
+		}
 
-			if (emailAddressId > 0) {
-				try {
+		return null;
+	}
 
-					emailAddress = EmailAddressServiceUtil
-							.getEmailAddress(emailAddressId);
-				} catch (PortalException e) {
-					// TODO Auto-generated catch block
-					e.printStackTrace();
-				} catch (SystemException e) {
-					// TODO Auto-generated catch block
-					e.printStackTrace();
-				} catch (RemoteException e) {
-					// TODO Auto-generated catch block
-					e.printStackTrace();
-				}
+	private static HttpServletRequest getHttpServletRequest(PortletRequest req) {
+		if (req instanceof ActionRequestImpl) {
+			ActionRequestImpl reqImpl = (ActionRequestImpl) req;
+
+			return reqImpl.getHttpServletRequest();
+		} else if (req instanceof RenderRequestImpl) {
+			RenderRequestImpl reqImpl = (RenderRequestImpl) req;
+
+			return reqImpl.getHttpServletRequest();
+		} else {
+			throw new RuntimeException(
+					"Unable to get the HTTP servlet request from "
+							+ req.getClass().getName());
+		}
+	}
+
+	private User getUser() throws PortalException, SystemException {
+		HttpServletRequest req = getHttpServletRequest(rreq);
+
+		long userId = (Long) req.getAttribute(USER_ID);
+
+		if (userId <= 0) {
+
+			// Portlet WARs may have the correct remote user and not have the
+			// correct user id because the user id is saved in the session
+			// and may not be accessible by the portlet WAR's session. This
+			// behavior is inconsistent across different application servers.
+
+			String remoteUser = req.getRemoteUser();
+
+			if (remoteUser == null) {
+				return null;
 			}
 
-			return emailAddress.getAddress();
+			userId = Long.parseLong(remoteUser);
 		}
 
-		return null;
+		User user = (User) req.getAttribute(WebKeys.USER);
+
+		if (user == null) {
+			user = UserLocalServiceUtil.getUserById(userId);
+
+			req.setAttribute(WebKeys.USER, user);
+		}
+
+		return user;
 	}
-
 }

Modified: labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/management/EmailNotifierImpl.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/management/EmailNotifierImpl.java	2007-10-08 18:08:53 UTC (rev 15656)
+++ labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/management/EmailNotifierImpl.java	2007-10-08 18:14:17 UTC (rev 15657)
@@ -1,58 +1,75 @@
 package org.jboss.wiki.management;
 
+import java.util.Date;
+
+import javax.mail.Address;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.Message.RecipientType;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.rmi.PortableRemoteObject;
+
+import org.apache.log4j.Logger;
+
 public class EmailNotifierImpl implements EMailNotifier {
 
+	private final static Logger log = Logger.getLogger(EmailNotifierImpl.class);
+
 	public synchronized void sendNotification(NotificationMsg notifications) {
 
-//		try {
-//			Session session = (Session) PortableRemoteObject.narrow(
-//					new InitialContext().lookup("java:Mail"), Session.class);
-//
-//			log.debug("Got email notification msg");
-//
-//			for (String email : notifications.getEmails()) {
-//				log.debug("MDB for: " + email);
-//
-//				StringBuffer buffer = null;
-//				Address[] to = null;
-//				MimeMessage m = new MimeMessage(session);
-//
-//				if (email != null) {
-//					m
-//							.setFrom(new InternetAddress(notifications
-//									.getFromEmail()));
-//					to = new InternetAddress[] { new InternetAddress(email) };
-//					m.setRecipients(RecipientType.TO, to);
-//					m.setSubject("[JBossWiki] Page change notification: "
-//							+ notifications.getPageName());
-//
-//					m.setSentDate(new Date());
-//					buffer = new StringBuffer();
-//
-//					buffer
-//							.append(
-//									"This message is generated automatically. Please do not reply. To remove yourself from the watch list please go to ")
-//							.append(notifications.getPageLink()).append(
-//									"<br />\n");
-//					buffer.append(notifications.getPageContent());
-//
-//					m.setContent(buffer.toString(),
-//							"text/html; charset=\"UTF-8\"");
-//					Transport.send(m);
-//					log.debug("Notification send to: "+email);
-//				}
-//			}
-//		} catch (ClassCastException e) {
-//			log.error(e);
-//		} catch (NamingException e) {
-//			log.error(e);
-//		} catch (AddressException e) {
-//			log.error(e);
-//		} catch (MessagingException e) {
-//			log.error(e);
-//		}
-	
+		try {
+			Session session = (Session) PortableRemoteObject.narrow(
+					new InitialContext().lookup("java:Mail"), Session.class);
+
+			log.debug("Got email notification msg");
+
+			for (String email : notifications.getEmails()) {
+				log.debug("MDB for: " + email);
+
+				StringBuffer buffer = null;
+				Address[] to = null;
+				MimeMessage m = new MimeMessage(session);
+
+				if (email != null) {
+					m
+							.setFrom(new InternetAddress(notifications
+									.getFromEmail()));
+					to = new InternetAddress[] { new InternetAddress(email) };
+					m.setRecipients(RecipientType.TO, to);
+					m.setSubject("[JBossWiki] Page change notification: "
+							+ notifications.getPageName());
+
+					m.setSentDate(new Date());
+					buffer = new StringBuffer();
+
+					buffer
+							.append(
+									"This message is generated automatically. Please do not reply. To remove yourself from the watch list please go to ")
+							.append(notifications.getPageLink()).append(
+									"<br />\n");
+					buffer.append(notifications.getPageContent());
+
+					m.setContent(buffer.toString(),
+							"text/html; charset=\"UTF-8\"");
+					Transport.send(m);
+					log.debug("Notification send to: " + email);
+				}
+			}
+		} catch (ClassCastException e) {
+			log.error(e);
+		} catch (NamingException e) {
+			log.error(e);
+		} catch (AddressException e) {
+			log.error(e);
+		} catch (MessagingException e) {
+			log.error(e);
+		}
+
 	}
 
-
 }




More information about the jboss-svn-commits mailing list