Article summary

Summary
Discusses how to create a custom File Transfer Protocol (FTP) service within Adobe Experience Manager.
Digital Marketing Solution(s) Adobe Experience Manager (Adobe CQ)
Audience
Developer (intermediate)
Required Skills
Java, JQuery, FTP, XML
Tested On Adobe Experience Manager 5.5, 5.6

Introduction

You can create an Adobe Experience Manager application that uses the File Transfer Protocol (FTP) to transfer a file from one server to another one. This article describes how to develop an Experience Manager application that uses an OSGi component to transfer an HTML file to another server. The OSGi component uses Apache Java libraries to transfer the HTML file. 

JavaMyFTP

Create an Experience Manager application folder structure

You can create an application folder structure that contains templates, components, and pages by using CRXDE Lite.

CQAppSetup

The following describes each application folder:

  • application name: contains all of the resources that an application uses. The resources can be templates, pages, components, and so on.
  • components: contains components that your application uses.
  • page: contains page components. A page component is a script such as a JSP file.
  • global: contains global components that your application uses
  • template: contains templates that you can base page components on.
  • src: contains source code that comprises an OSGi component (this development does not create an OSGi bundle using this folder or CRXDE. Rather Eclipse is used).
  • install: contains a compiled OSGi bundles container

To create an application folder structure:

  1. To view the welcome page, enter the URL http://[host name]:[port] into a web browser. For example, http://localhost:4502.
  2. Select CRXDE Lite.
  3. Right-click the apps folder (or the parent folder), select Create, Create Folder.
  4. Enter the folder name into the Create Folder dialog box. Enter ftpapp
  5. Repeat steps 1-4 for each folder specified in the previous illustration. 
  6. Click the Save All button.

Create a template

You can create a template by using CRXDE Lite. A template enables you to define a consistent style for the pages in your application. A template comprises of nodes that specify the page structure. For more information about templates, see Templates.

To create a template:

1. To view the welcome page, enter the URL http://[host name]:[port] into a web browser. For example, http://localhost:4502.

2. Select CRXDE Lite.

3. Right-click the template folder (within your application), select Create, Create
Template.

4. Enter the following information into the Create Template dialog box:

  • Label: The name of the template to create. Enter ftptemplate.
  • Title: The title that is assigned to the template
  • Description: The description that is assigned to the template
  • Resource Type: The component's path that is assigned to the template and copied to implementing pages. Enter ftpapp/components/page/ftptemplate.
  • Ranking: The order (ascending) in which this template will appear in relation to other templates. Setting this value to 1 ensures that the template appears first in the list.

5. Add a path to Allowed Paths. Click on the plus sign and enter the following value: /content(/.*)?.

6. Click Next for Allowed Parents.

7. Select OK on Allowed Children.

Create a render component that uses the template

Components are re-usable modules that implement specific application logic to render the content of your web site. You can think of a component as a collection of scripts (for example, JSPs, Java servlets, and so on) that completely realize a specific function. In order to realize this functionality, it is your responsibility as an AEM developer to create scripts that perform specific functionality. For more information about components, see Components.

By default, a component has at least one default script, identical to the name of the component.
To create a render component:

1. To view the CQ welcome page, enter the URL http://[host name]:[port] into a web browser. For example, http://localhost:4502.

2. Select CRXDE Lite.

3. Right-click /apps/ftpapp/components/page, then select
Create, Create Component.

4. Enter the following information into the Create Component dialog box:

  • Label: The name of the component to create. Enter ftptemplate.
  • Title: The title that is assigned to the component
  • Description: The description that is assigned to the template

5. Select Next for Advanced Component Settings and Allowed Parents.

6. Select OK on Allowed Children.

7. Open the ftptemplate.jps located at: /apps/ftpapp/components/page/ ftptemplate/ftptemplate.jsp.

8. Enter the following HTML code:

<html>
<head>
<title>Hello World !!!</title>
</head>
<body>
<h1>Hello FTP Application</h1>
<h2>This page will let a user upload a file to Adobe CQ that can sent using FTP</h2>
</body>
</html>

Create FTP application logic

Create a Java class named sendPDF that retrieves the HTML document and uses the FTP protocol to send it to another server. In this example, the HTML document is simply retrieved from the local file system. 

In the sendPDF class, create a method named doFTP that sends the HTML document that is retrieved by using the Java JCR API. This method uses an org.apache.commons.net.ftp.FTPClient to send the HTML document to another server. (See the doFTP application logic in the following Java class).

Note:

To use the FTP application logic shown here, ensure that you add the commons-net-3.1.jar file to your Java project’s class path. You can download this JAR file from the following URL: http://commons.apache.org/net/download_net.cgi.

The following Java code represents the sendPDF class.

package com.adobe.cq;


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

		
public class GetRepository { 
	
	
public void GetFile(String ftpFileName)
{
	
	try { 
		 File fileName = null;
         
                 //Get the HTML document from the local file system
		 fileName = new File("C:\\Adobe\\Page.html");
        
                 // Get the length of the file stream and create a byte array
                 FileInputStream inFs = new FileInputStream(fileName); 
                     
		//FTP the HTML file
		DoFTP(inFs, ftpFileName); 
		
		}
	catch(Exception e){
		e.printStackTrace();
		}
	} 



public  Boolean DoFTP(InputStream in, String fileName)
{
  Boolean didItUpload = Boolean.valueOf(false);
  try
  {
	 String server="[FTP Server]";
	 String user = "[FTP User]"; 
	 String password = "[FTP Password]"; 

         FTPClient ftp = new FTPClient();

         ftp.connect(server);
         ftp.login(user, password);

         ftp.setDataTimeout(60000);

         int reply = ftp.getReplyCode();

         if (FTPReply.isPositiveCompletion(reply)) {
             System.out.println("Connected Success");
         else
              ftp.disconnect();
    }

        Boolean tt = Boolean.valueOf(ftp.isConnected());
        ftp.enterLocalPassiveMode();
        ftp.setUseEPSVwithIPv4(true);

        boolean directoryExists = ftp.changeWorkingDirectory("/");
        directoryExists =      ftp.changeWorkingDirectory("/public_html/documents");

    ftp.setFileType(0, 0);
    ftp.setFileTransferMode(0);

    String upPath = "/public_html/documents/" + fileName;

    didItUpload = Boolean.valueOf(ftp.storeFile(upPath, in));

    ftp.disconnect();
    return didItUpload;
  }
  catch (Exception e)
  {
    e.printStackTrace();
  }
  return null;
} 

} 

Note:

Replace FTP Server, FTP User Name, and FTP Password with values applicable to your FTP server. Also export the Java project as a JAR file (this JAR file is also included in the OSGi package).

Create the OSGi bundle that contains the Java proxy classes

Create an OSGi bundle that contains jar files created earlier in this artilce. Once you upload the OSGi bundle to Experience Manager, you can call the getHTMLFile method defined in the SendPDF class. To create the OSGi bundle, you can use another Eclipse project. An OSGi bundle is essentially a collection of Java files and a MANIFEST.MF file.

To create an OSGi bundle that contains JAR files using Eclipse:

1. Start Eclipse

2. Select File, New, Other.

3. Under the Plug-in Development folder, choose Plug-in from Existing JAR Archives.

4. In the JAR selection dialog, click the Add external button, and browse to the JAR file that you just created. Also include the commons-net-3.1.jar in the OSGi bundle. This is required to use Apache FTP classes in the OSGi bunlde. 

5. Click Next.

6. In the Plug-in Project properties dialog, ensure that you check the checkbox for Analyze library contents and add dependencies.

7. Make sure that the Target Platform is the standard OSGi framework.

8. Ensure the checkboxes for Unzip the JAR archives into the project and Update references to the JAR files are both checked.

9. Click Next, and then Finish.

10. Click on the Runtime tab.

11. Make sure that the Exported Packages list is populated

12. Make sure these have been added under the Export-Package header in MANIFEST.MF. Remove the version information in the MANIFEST.MF file. Version numbers can cause conflicts when you upload the OSGi bundle.

13. Also make sure that the Import-Package header in MANIFEST.MF is also populated.

14. Save the project.

15. Build the OSGi bundle by right clicking the project in the left pane, choose Export, Plug-in Development, Deployable plug-ins and fragments and click Next.

16. Select a location for the export (C:\TEMP) and click Finish. (Ignore any error messages).

17. In C:\TEMP\plugins, you should now find the OSGi bundle with a name similar to ftpbundle_1.0.0.jar.

18. Login to Apache Felix Web Console at http://server:port/system/console/bundles (default admin user = admin with password= admin).

19. Sort the bundle list by Id and note the Id of the last bundle.

20. Click the Install/Update button.

21. Check the Start Bundle checkbox.

22. Browse to the bundle JAR file you just built. (C:\TEMP\plugins\ftpbundle_1.0.0.jar).

23. Click Install.

24. Click the Refresh Packages button.

25. Check the bundle with the highest Id.

26. Your new bundle should now be listed with the status Active.

27. If the status is not Active, check the error.log for exceptions. If you get “org.osgi.framework.BundleException: Unresolved constraint” errors, check the MANIFEST.MF for strict version requirements which might look as follows: javax.xml.namespace; version=”3.1.0”

28. If the version requirement causes problems, remove it so that the entry looks like this: javax.xml.namespace,.

29. If the entry is not required, remove it entirely.

30. Rebuild the bundle

31. Delete the previous bundle and deploy the new one.

 

*INFO* [FelixDispatchQueue] com.oracle.jdbc BundleEvent INSTALLED
*INFO* [FelixDispatchQueue] com.oracle.jdbc BundleEvent RESOLVED
*INFO* [FelixDispatchQueue] com.oracle.jdbc BundleEvent STARTED
*INFO* [FelixDispatchQueue] org.apache.felix.framework FrameworkEvent PACKAGES REFRESHED

Note:

You can view details, such as the Java packages that are part of the OSGi bundle.

Modify the JSP to call the methods specified in the OSGi bundle

Modify the FTPTemplate.jsp to call the GetFile method defined in the GetRepository class. The string parameter value is used as the name of the file is that sent to the other server. In this example, the file name is AdobeCQ.html.

The following code shows the modified JSP code to invoke operations of the OSGi bundle.

<%@include file="/libs/foundation/global.jsp"%>
<%@ page import="org.apache.sling.commons.json.io.*,com.adobe.cq.*" %><%


com.adobe.cq.GetRepository repo = new com.adobe.cq.GetRepository ();
repo.GetFile("AdobeCQ.html");
%>

<h2>The AdobeCQ HTML file was sent to another server using an OSGi component</h2>

To modify the ftptemplate.jsp file
1. To view the CQ welcome page, enter the URL: http://[host name]:[port] into a web browser. For example, http://localhost:4502.
2. Select CRXDE Lite.
3. Double-click apps/ftpapp/components/page/ftptemplate/ftptemplate.jsp.
4. Replace the JSP code with the new code shown in this section.
5. Click Save All.

Create a page that lets a user upload an HTML page

The final task that you perform in order to see a web page that lets a user upload an HTML file to Experience Manager is to create a site that contains a page that is based on the ftptemplate (the template created earlier in this development article).

To create a web page that invokes an operation in the OSGi bundle:

1. To view the CQ welcome page, enter the URL http://[host name]:[port] into a web browser. For example, http://localhost:4502.
2. Select Websites.
3. From the left hand pane, select Websites.
4. Select New, New Page.
5. Specify the title of the page in the Title field.
6. Specify the name of the page in the Name field.
7. Select templateJSON from the template list that appears. This value represents the template that is created in this development article. If you do not see it, then repeat the steps in this development article. For example, if you made a typing mistake when entering in path information when creating the template, the template will not show up in the New Page dialog box.
8. Open the new page that you created by double-clicking it in the right pane. The new page opens in a web browser. 

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy