Powered By Blogger

Monday, May 30, 2011

CIFS Alfresco

The logic behind the implementation of CIFS as written below

/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see . */
/**
*
*/
package org.alfresco.filesys.util;

import java.util.HashMap;
import java.util.Map;

import org.alfresco.jlan.util.Platform;
import org.alfresco.util.exec.RuntimeExec;
import org.alfresco.util.exec.RuntimeExec.ExecutionResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* CIFS Mounter Class
*
*

Mount/map a network drive taking care of platform differences.
*
* @author gkspencer
*/
public class CifsMounter {

// Debug logging

private static final Log logger = LogFactory.getLog( CifsMounter.class);

// Protocol type constants

public static final int Default = 0;
public static final int NetBIOS = 1;
public static final int NativeSMB = 2;
public static final int Win32NetBIOS = 3;

// Windows mount/unmount commands

private static final String WindowsMountCmd = "net use ${drive} \\\\${srvname}\\${sharename} ${password} /USER:${username}";
private static final String WindowsUnMountCmd = "net use ${drive} /d";

// Linux mount/unmount commands

private static final String LinuxMountSmbfsCmd = "mount -t smbfs //${srvname}/${sharename} ${mountpoint} -o username=${username},password=${password}";
private static final String LinuxMountCifsCmd = "mount -t cifs //${srvname}/${sharename} ${mountpoint} -o username=${username},password=${password}";
private static final String LinuxMountCifsNBCmd = "mount.cifs //${srvname}/${sharename} ${mountpoint} -o servern=${srvname},port=139,username=${username},password=${password}";
private static final String LinuxUnMountCmd = "umount ${mountpoint}";

// Mac OS X mount/unmount commands

private static final String MacOSXMountCmd = "mount_smbfs -U ${username} //${password}@${srvname}/${sharename} ${mountpoint}";
private static final String MacOSXUnMountCmd = "umount ${mountpoint}";

// Server name and share name

private String m_srvName;
private String m_shareName;

// Server address

private String m_srvAddr;

// Access details for remote share

private String m_userName;
private String m_password;

// Protocol to use for connection (non Windows platforms)

private int m_protocolType = Default;

// Port to connect on (non Windows platforms)

private int m_port;

/**
* Default constructor
*/
public CifsMounter()
{
}

/**
* Class constructor
*
* @param srvName String
* @param shareName String
* @param userName String
* @param password String
*/
public CifsMounter(String srvName, String shareName, String userName, String password)
{
setServerName( srvName);
setShareName( shareName);

setUserName( userName);
setPassword( password);
}

/**
* Mount a remote CIFS shared filesystem
*
* @param driveLetter String
* @param mountPoint String
* @exception CifsMountException
*/
public void mountFilesystem( String driveLetter, String mountPoint)
throws CifsMountException
{
// Create the command line launcher

RuntimeExec exec = new RuntimeExec();

// Create the command map and properties

Map commandMap = new HashMap( 1);
Map defProperties = new HashMap( 10);

// Determine the platform type and build the appropriate command line string

Platform.Type platform = Platform.isPlatformType();

switch ( platform)
{
// Windows

case WINDOWS:
commandMap.put( "Windows.*", WindowsMountCmd);
break;

// Linux

case LINUX:

// Check the protocol type of the CIFS server

if ( isProtocolType() == NativeSMB || isProtocolType() == Default)
{
// Use the CIFS VFS mounter to connect using native SMB

StringBuilder cmd = new StringBuilder( LinuxMountCifsCmd);
if ( getProtocolPort() != 0)
{
cmd.append( ",port=");
cmd.append( getProtocolPort());
}

// Set the command line

commandMap.put( "Linux", cmd.toString());
}
else
{
// Set the command line to use the CIFS VFS mounter to connect using NetBIOS

StringBuilder cmd = new StringBuilder( LinuxMountCifsNBCmd);

if ( getServerAddress() != null)
{
cmd.append( ",ip=");
cmd.append( getServerAddress());
}

// Set the command line

commandMap.put( "Linux", cmd.toString());
}
break;

// Mac OS X

case MACOSX:
commandMap.put( "Mac OS X", MacOSXMountCmd);
break;
}

// Set the command map

exec.setCommandMap( commandMap);

// Build the command line properties list

defProperties.put( "drive", driveLetter);
defProperties.put( "srvname", getServerName());
defProperties.put( "sharename", getShareName());
defProperties.put( "username", getUserName());
defProperties.put( "password", getPassword());
defProperties.put( "mountpoint", mountPoint);

exec.setDefaultProperties(defProperties);

// Get the command to be used on this platform

if ( logger.isDebugEnabled())
logger.debug( "Mount CIFS share, cmdLine=" + exec.getCommand());

// Run the command

ExecutionResult execRes = exec.execute();

if ( logger.isDebugEnabled())
logger.debug( "Mount result=" + execRes);

// Check if the command was successful

if ( execRes.getSuccess() == false)
throw new CifsMountException( execRes.getExitValue(), execRes.getStdOut(), execRes.getStdErr());
}

/**
* Unmount a remote CIFS shared filesystem
*
* @param driveLetter String
* @param mountPoint String
* @exception CifsMountException
*/
public void unmountFilesystem( String driveLetter, String mountPoint)
throws CifsMountException
{
// Create the command line launcher

RuntimeExec exec = new RuntimeExec();

// Create the command map and properties

Map commandMap = new HashMap( 1);
Map defProperties = new HashMap( 10);

// Determine the platform type and build the appropriate command line string

Platform.Type platform = Platform.isPlatformType();

switch ( platform)
{
// Windows

case WINDOWS:
commandMap.put( "Windows.*", WindowsUnMountCmd);
break;

// Linux

case LINUX:
commandMap.put( "Linux", LinuxUnMountCmd);
break;

// Mac OS X

case MACOSX:
commandMap.put( "Mac OS X", MacOSXUnMountCmd);
break;
}

// Set the command map

exec.setCommandMap( commandMap);

// Build the command line properties list

defProperties.put( "drive", driveLetter);
defProperties.put( "mountpoint", mountPoint);

exec.setDefaultProperties(defProperties);

// Get the command to be used on this platform

if ( logger.isDebugEnabled())
logger.debug( "UnMount CIFS share, cmdLine=" + exec.getCommand());

// Run the command

ExecutionResult execRes = exec.execute();

if ( logger.isDebugEnabled())
logger.debug( "UnMount result=" + execRes);

// Check if the command was successful

if ( execRes.getSuccess() == false)
throw new CifsMountException( execRes.getExitValue(), execRes.getStdOut(), execRes.getStdErr());
}

/**
* Return the server name
*
* @return String
*/
public final String getServerName()
{
return m_srvName;
}

/**
* Return hte server address
*
* @return String
*/
public final String getServerAddress()
{
return m_srvAddr;
}

/**
* Return the share name
*
* @return String
*/
public final String getShareName()
{
return m_shareName;
}

/**
* Return the user name
*
* @return String
*/
public final String getUserName()
{
return m_userName;
}

/**
* Return the password
*
* @return String
*/
public final String getPassword()
{
return m_password;
}

/**
* Return the protocol type
*
* @return int
*/
public final int isProtocolType()
{
return m_protocolType;
}

/**
* Return the protocol port
*
* @return int
*/
public final int getProtocolPort()
{
return m_port;
}

/**
* Set the server name
*
* @param name String
*/
public final void setServerName(String name)
{
m_srvName = name;
}

/**
* Set the server address
*
* @param srvAddr String
*/
public final void setServerAddress(String srvAddr)
{
m_srvAddr = srvAddr;
}

/**
* Set the share name
*
* @param name String
*/
public final void setShareName(String name)
{
m_shareName = name;
}

/**
* Set the user name
*
* @param user String
*/
public final void setUserName(String user)
{
m_userName = user;
}

/**
* Set the password
*
* @param password String
*/
public final void setPassword(String password)
{
m_password = password;
}

/**
* Set the protocol type to use
*
* @param proto int
*/
public final void setProtocolType(int proto)
{
m_protocolType = proto;
}

/**
* Set the port to use for the connection
*
* @param port int
*/
public final void setProtocolPort(int port)
{
m_port = port;
}

/**
* Return the CIFS mounter as a string
*
* @return String
*/
public String toString()
{
StringBuilder str = new StringBuilder();

str.append( "[\\\\");
str.append( getServerName());
str.append( "\\");
str.append( getShareName());
str.append( ",");
str.append( getUserName());
str.append( ",");
str.append( getPassword());

if ( isProtocolType() != Default)
{
str.append( " (");
if ( isProtocolType() == NetBIOS)
str.append ( "NetBIOS");
else if ( isProtocolType() == NativeSMB)
str.append( "NativeSMB");
else if ( isProtocolType() == Win32NetBIOS)
str.append( "Win32NetBIOS");

if ( getProtocolPort() != 0)
{
str.append( ",");
str.append( getProtocolPort());
}
str.append( ")");
}
str.append( "]");

return str.toString();
}
}

Friday, May 27, 2011

Axis2 in Liferay portlet

Even though Liferay is coming up with the Axis1 jars we can have the system that works in the
portlet with Axis2.

The importan thing is along with the library depedndency jars we need to add the below jars
activation-1.1
antlr-2.7.7
axiom-api-1.2.10
axiom-dom-1.2.10
axiom-impl-1.2.10
axis2-adb-1.5.4
axis2-adb-codegen-1.5.4
axis2-ant-plugin-1.5.4
axis2-clustering-1.5.4
axis2-codegen-1.5.4
axis2-corba-1.5.4
axis2-fastinfoset-1.5.4
axis2-java2wsdl-1.5.4
axis2-jaxbri-1.5.4
axis2-jaxws-1.5.4
axis2-jibx-1.5.4
axis2-json-1.5.4
axis2-kernel-1.5.4
axis2-metadata-1.5.4
axis2-mtompolicy-1.5.4
axis2-saaj-1.5.4
axis2-soapmonitor-servlet-1.5.4
axis2-spring-1.5.4
axis2-transport-http-1.5.4
axis2-transport-local-1.5.4
axis2-xmlbeans-1.5.4
commons-codec-1.3
commons-fileupload-1.2
commons-httpclient-3.1
commons-io-1.4
commons-logging-1.1.1
geronimo-stax-api_1.0_spec-1.0.1
httpcore-4.0
log4j-1.2.15
mail-1.4
neethi-2.0.4
woden-api-1.0M8
woden-impl-dom-1.0M8
wsdl4j-1.6.2
wstx-asl-3.2.9
xmlbeans-2.3.0
XmlSchema-1.4.3


The class will be as below

package com.test;

import java.io.IOException;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import sample.axisversion.ExceptionException;
import sample.axisversion.VersionStub;
import sample.axisversion.VersionStub.GetVersionResponse;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

/**
* Portlet implementation class NewPortlet
*/
public class NewPortlet extends GenericPortlet {

public void init() {
viewJSP = getInitParameter("view-jsp");
}

public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {

System.out.println("**********View method of NewPortlet ******************8");

VersionStub versionStub = new VersionStub();

GetVersionResponse response;
try {
response = versionStub.getVersion();
System.out.println("**************Return::::"+response.get_return());



} catch (ExceptionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

include(viewJSP, renderRequest, renderResponse);
}

protected void include(
String path, RenderRequest renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {

PortletRequestDispatcher portletRequestDispatcher =
getPortletContext().getRequestDispatcher(path);

if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}

protected String viewJSP;

private static Log _log = LogFactoryUtil.getLog(NewPortlet.class);

}

Thursday, May 26, 2011

Steps to use the WebService client in your Liferay portlet

1. Use the seperate tomcat instance to generate the stub classes from the
url
2. Create the portlet in your sdk
3. The lib folder of the portlet should have below jars
axis.jar
commons-discovery-0.2.jar
jaxrpc.jar
saaj.jar
wsdl4j-1.5.1.jar
4. Inside your portlet class we can write the logic to consume the service

package com.cignex;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.UserSoap;
import com.liferay.portal.service.http.UserServiceSoap;
import com.liferay.portal.service.http.UserServiceSoapServiceLocator;

import java.io.IOException;
import java.net.URL;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

/**
* Portlet implementation class WebServiceConsumePortlet
*/
public class WebServiceConsumePortlet extends GenericPortlet {

public void init() {
editJSP = getInitParameter("edit-jsp");
helpJSP = getInitParameter("help-jsp");
viewJSP = getInitParameter("view-jsp");
}

public void doEdit(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {

include(editJSP, renderRequest, renderResponse);
}

public void doHelp(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {

include(helpJSP, renderRequest, renderResponse);
}

public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {

System.out.println("Inside the doView mode************ service");


try {

System.out.println("service locator*************8");
UserServiceSoapServiceLocator locatorUser = new UserServiceSoapServiceLocator();
UserServiceSoap soapUser = locatorUser.getPortal_UserService(new URL("http://test:test@127.0.0.1:8080/tunnel-web/secure/axis/Portal_UserService"));

//http://abc:abc@127.0.0.1:8080/tunnel-web/secure/axis/Portal_UserService
System.out.println("Calling from portlet");
if(null!=soapUser){
System.out.println("Inside soapUser not null*********");
UserSoap soapUserModel = soapUser.getUserById(10134);
System.out.println(" getEmailAddress:" + soapUserModel.getEmailAddress());

}

} catch (Exception e) {
e.printStackTrace();
}


include(viewJSP, renderRequest, renderResponse);
}

protected void include(
String path, RenderRequest renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {

PortletRequestDispatcher portletRequestDispatcher =
getPortletContext().getRequestDispatcher(path);

if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}

protected String editJSP;
protected String helpJSP;
protected String viewJSP;

private static Log _log = LogFactoryUtil.getLog(WebServiceConsumePortlet.class);

}

Tuesday, May 24, 2011

Using Liferay WebServices

The wsdl url we can get it from
http://127.0.0.1:8080/tunnel-web/secure/axis/Portal_UserService?wsdl

by using the above url we can generate the skeleton classes in the eclipse.


The client code to access the url is as below


/**
*
*/
package com.cignex.test;

import java.net.URL;

import com.liferay.portal.model.UserSoap;
import com.liferay.portal.service.http.UserServiceSoap;
import com.liferay.portal.service.http.UserServiceSoapServiceLocator;

/**
* @author basanagowda.patil
*
*/
public class LiferayUserServiceClient {

/**
* @param args
*/
public static void main(String[] args) {
try {
UserServiceSoapServiceLocator locatorUser = new UserServiceSoapServiceLocator();
UserServiceSoap soapUser = locatorUser.getPortal_UserService(new URL("http://test:test@127.0.0.1:8080/tunnel-web/secure/axis/Portal_UserService"));

//http://abc:abc@127.0.0.1:8080/tunnel-web/secure/axis/Portal_UserService

if(null!=soapUser){
System.out.println("Inside soapUser not null*********");
UserSoap soapUserModel = soapUser.getUserById(10134);
System.out.println(" getEmailAddress:" + soapUserModel.getEmailAddress());

}

} catch (Exception e) {
e.printStackTrace();
}
}

}

Monday, May 23, 2011

Adding servlet in Liferay portlet

In the Web.xml you should have the below code mapping



servlettest-portlet

index.html
index.htm
index.jsp
default.html
default.htm
default.jsp



testServlet
com.liferay.portal.kernel.servlet.PortalDelegateServlet

servlet-class
com.aonehewit.servlet.TestServlet


sub-context
testServlet

0





testServlet
/testServlet/*





http://java.sun.com/portlet_2_0

/WEB-INF/tld/liferay-portlet.tld






In the class you should have the below code

/**
*
*/
package com.aonehewit.servlet;

import com.liferay.portal.kernel.servlet.PortalDelegatorServlet;
import com.liferay.portal.kernel.util.JavaConstants;
import com.liferay.portal.kernel.util.WebKeys;

import java.io.IOException;

import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author basanagowda.patil
*
*/
public class TestServlet extends HttpServlet {




@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
System.out.println("Inside the init() method****************8 of TestServlet");
}




@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
System.out.println("Inside the service******************TestServlet");
super.service(arg0, arg1);
}




@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("Inside the doGet method**********************888");
super.doGet(req, resp);
}



@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);

System.out.println("Inside the Do post me thod:::::::::::::::::::");
}


}


The url to access the servlet will be as below

http://localhost:8080/delegate/testServlet

Note : delegate is not a portlet it is another servlet which is used to invoke our servlet.

Thursday, May 5, 2011

Run Action flow in Alfresco

1. Run actionwizard
2. actions.jsp
3. When we click on setvalues and add then the seperate page
will be opened.
4. From the page it will register the value to the corresonding executer
class.
5. On the click of finish in The RunActionwizard ,finishImpl
will find the corresponding bean handler and its executeIpl()
will be called. that will be responsible for performing the
operations