Skip to main content

Featured

Adobe Experience Manager - Create an OSGI Configuration

 In this article, let's create an OSGi configuration, configure it and use it in AEM code. So now let's get started with the creation of an OSGi configuration. Technical details and Assumptions: All the following details are tested in AEM version 6.5.8, Java version 11.0.2 Creation of OSGi configuration: To create an OSGi configuration we need to create an ObjectClassDefinition. I have included a sample OCD configuration, which can be used as a reference to create one. The next step would be to create an interface and an implementation that can help fetch the OSGi configurations.  Interface: Implementation: Let's try to use the OSGi configuration created so far in Models/Servlets. For demonstration purposes, I used AEM Models here, but the same can be implemented in Servlets too. Now that we have created the OSGi configuration. Once building the code, we should be able to see the OSGi configuration in the web console (http://localhost:4502/system/console/configMgr) Configur

Implementation of Transmission Control Protocol (TCP) by File Transfer Program

This is java program implementing TCP by transferring a file between the client and sever by using the localhost a address where any address can be placed even remote addresses are supported with certain file permissions.

FileTransferServer.java:

 import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class FileTransferServer {
   
    public static void main(String[] args) throws Exception {

        //Initialize Sockets
        ServerSocket ssock = new ServerSocket(5000);
        Socket socket = ssock.accept();
       
        //The InetAddress specification
        InetAddress IA = InetAddress.getByName("localhost");
       
        //Specify the file
        File file = new File("images.png");
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
         
        //Get socket's output stream
        OutputStream os = socket.getOutputStream();
               
        //Read File Contents into contents array
        byte[] contents;
        long fileLength = file.length();
        long current = 0;
        
        long start = System.nanoTime();
        while(current!=fileLength){
            int size = 10000;
            if(fileLength - current >= size)
                current += size;   
            else{
                size = (int)(fileLength - current);
                current = fileLength;
            }
            contents = new byte[size];
            bis.read(contents, 0, size);
            os.write(contents);
            System.out.print("Sending file ...  /images.png\n "+(current*100)/fileLength+"% complete!");
        }  
       
        os.flush();
        //File transfer done. Close the socket connection!
        socket.close();
        ssock.close();
        System.out.println("File sent succesfully!");
    }
}

 FileTransferClient.java:

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;


public class FileTransferClient {
   
    public static void main(String[] args) throws Exception{
       
        //Initialize socket
        Socket socket = new Socket(InetAddress.getByName("localhost"), 5000);
        byte[] contents = new byte[10000];
       
        //Initialize the FileOutputStream to the output file's full path.
        FileOutputStream fos = new FileOutputStream("image.png");
    System.out.println("Saved as: /image.png");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        InputStream is = socket.getInputStream();
       
        //No of bytes read in one read() call
        int bytesRead = 0;
       
        while((bytesRead=is.read(contents))!=-1)
            bos.write(contents, 0, bytesRead);
       
        bos.flush();
        socket.close();
       
        System.out.println("File saved successfully!");
    }
}

Output:

Comments

Post a Comment

Popular Posts