Monday, July 30, 2018

Using Java and Pdfbox to create PDF with Arabic text

Hello,

I am writing this post to demonstrate how to create pdf file using pdfbox library, more importantly how to write in Arabic in it which is not very common topic people talk about.

Simply, the below represents the code to do that, and depending on your scenario you can dig more on the rest of the library, it is open source and very rich.

Prerequisites:
-pdfbox v.2.0.11
-ICU v.62
-arial.ttf

You can download pdfbox-app-2.0.11.jar which is the latest as of the time I am writing this post from here.

You can download ICU from here

You can download arial.ttf from here and put it in c:\data\ for the example to work

Go to netbeans or the IDE you are using and them to your Libraries and create the below desktop program.

package pdftester;

import com.ibm.icu.text.ArabicShaping;
import com.ibm.icu.text.ArabicShapingException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

public class PDFtester {

    public static void main(String[] args) {

        PDDocument doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);
        PDPageContentStream pageContentStream;
        try {
            pageContentStream = new PDPageContentStream(doc, page);
            pageContentStream.beginText();
            pageContentStream.setFont(PDType0Font.load(doc, new File("c:\\data\\arial.ttf")), 20);
            pageContentStream.newLineAtOffset(250, 750);
            String s = "سامر احمد";
            pageContentStream.showText(new StringBuilder(new ArabicShaping(ArabicShaping.LETTERS_SHAPE).shape(s)).reverse().toString());
            pageContentStream.endText();
            pageContentStream.close();
            doc.save("c:\\data\\test.pdf");
            doc.close();
        } catch (IOException ex) {
            Logger.getLogger(PDFtester.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ArabicShapingException ex) {
            Logger.getLogger(PDFtester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Monday, April 9, 2018

Rest API using python

In this post I would like to share my experience with Rest APIs using python as a consumer, this will be very useful and innovative in term of operation and automation.

Most of the time system (Storage, UNIX, Windows, Network) admins complain about the amount of work, many repetitive tasks are done in a tedious way, critical mistakes can happen due to doing many tasks at the same time. Apart from all that, recently the self service approach started to rise as a way to offload the tasks to the end user, budget control and reducing interactions. Also not to forget to mention the agility of the environment which will involve the swift creation and removal of environments.

Probably, all of what mentioned above is doable in UNIX and Windows servers, if not most of them. Now, but what about appliances, such as switches, storage, and any embedded systems. Recently, all new well supported systems started to come with enabled Rest APIs, to facilitate integration with other systems, reporting and monitoring.

Using Rest APIs of specific system will have few requirements:
  • Understanding HTTP requests
  • Understanding JSON/XML data
  • Programming language (Python) with request module installed
  • Documentation of how to use the system Rest API
Briefly, the HTTP requests are revolving around GET, POST, PUT, DELETE, UPDATE and some other types which we don't require usually in our Rest API operations. GET is used for list/retrieving objects, POST for creation of objects, PUT for updating attributes, UPDATE for updating the objects and intuitively DELETE for deleting objects.

Object here represents element to be Listed, Created or Deleted..etc. as per the above type of requests.

Security is important element in the Rest APIs that is why there are couple of ways to secure Restful resources. Github for example is using Basic Authentication, OAuth2 token and OAuth2 key/secret check here for more information.

Basic authentication is the usual used Rest API security method, which is basically Username and Password concatenated with ":" in the middle, and prepended by "Basic " passed in a base64 encrypted format.

Here is how you can demonstrate the use of base64 encoding (you don't need to do this in your code as HTTP protocol will do this automatically in the background) and how it will look like in the header parameters:

I am using python version 3.x

#!/usr/bin/python3.6

import base64
import sys

print (base64.b64encode("user:pass".encode()).decode())


The HTTP will have the below parameter in the request header:
KeyValue
AuthorizationBasic dXNlcjpwYXNz

You can use google chrome plugin called Postman to troubleshoot or debug your requests.

Now, starting to make GET requests (without authentication) is easy by following few steps in github..

#!/usr/bin/python3.6

import json
import requests
requests.packages.urllib3.disable_warnings()

response = requests.get('https://api.github.com/users', verify=False)
json_data = json.loads(response.text)
print(json_data)

The above code will get you the list of users in github in json format, you can use online json formatter to view it in a readable format.

Similarly, we can use this to interact with EMC VPLEX:

#!/usr/bin/python3.6

import requests, json

requests.packages.urllib3.disable_warnings()

url = "https://Cluster_IP/vplex/clusters/Cluster_ID/virtual-volumes/*"
payload = (UsernamePassword)
r = requests.get(url, auth = payload, verify = False) #make get request passing the credentials
data = json.loads(r.text) #read response as json and data will be a dictionary variable
print(data) #printing data
print(json.dumps(data)) #printing will be in a nice readable format

Using Rest API I developed interface for deleting VPLEX volumes, which takes less than a minute. This task used to take 15 to 30 minutes using CLI(which was the only available way to delete the volume). Therefore, the productivity significantly increased, and this way can help in automating provisioning and destroying Demo environments, which is the current trend in demonstrating new technologies.

Tuesday, May 30, 2017

adding bulk of users GUI interface using Java swing in windows

Now, continuing talking about providing GUI interface to systems operations.

We will dive more into Java and start using the jar file command-factory.jar I mentioned in my previous article to show how useful is to provide GUI interface whether it's for delegation, e.g. you want to delegate specific operation, give access to specific team to extract report(from system not database), enforce standards for the operations conventions/unifying passwords, integrate multiple services together..etc, the list is too long to be mentioned here.

To demonstrate the idea, I will use what I would like to call OAM Object-Administrational Mapping (I just invented that ^_^) which is equivelant to the ORM Object-Relational Mapping. So, I will use command-factory.jar to create small app as "helloworld" to demonstrate the idea of "extending administration capabilities using Java development".

Let us start, after googling for 10 minutes I found the commands to create and delete windows user(normal windows user not a domain user).

in windows cmd:

-net user USERNAME PASSWORD /add

e.g.

-net user testuser Pass_123 /add

to delete:

net user testuser /del

Keep in mind in windows this works in the external commands only(check windows internal vs. external commands), for internal commands it didn't work I guess you need to use "shell" flag somewhere, Windows Powershell or even write python script to do the job for you. Even though with the external commands, probably you will need to use the full executable path (just put the full path it will save you some head scratches).

in windows cmd:

where net

output:
C:\Windows\System32\net.exe

After becoming familiar with the command, go to your IDE netbeans, eclipse..etc. Here, I thought it is better to demonstrate with desktop app instead of web based as it is faster to be used, you know, web app needs application server.

We will follow MVC design pattern which will include Model(user class/entity), View (main console/java swing) and controller(java class to link model and view).

create Java desktop application and give it a name call it UserManager for example, and for the sake of testing we will create main class without GUI/swing. Once application is created add command-factory.jar to the project libraries.


Project includes:

-User.java

package entity;

public class User {
 
    private String name;
    private String password;

    public User() {
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" + "name=" + name + ", password=" + password + '}';
    }  
}

-Controller.java
package controller;

import UserSAO.UserSAO;
import entity.User;

public class Controller {

    private UserSAO userSAO;
    private User user;

    public Controller() {
        userSAO = new UserSAO();
    }

    public UserSAO getUserSAO() {
        return userSAO;
    }
}

-UserSAO.java

package UserSAO;

import commandfactory.CommandFactory;
import entity.User;
import java.util.ArrayList;
import java.util.List;

public class UserSAO {

    CommandFactory commandFactory;

    public CommandFactory getCommandFactory() {
        return commandFactory;
    }

    public void createUser(User user) {
        List<String> cmd = new ArrayList<>();
        cmd.add("C:\\Windows\\System32\\net.exe");
        cmd.add("user");
        cmd.add(user.getName());
        cmd.add(user.getPassword());
        cmd.add("/add");
        commandFactory = new CommandFactory();
        commandFactory.setCmdList(cmd);
        commandFactory.execCommand();
    }

    public void deleteUser(String name) {
        List<String> cmd = new ArrayList<>();
        cmd.add("C:\\Windows\\System32\\net.exe");
        cmd.add("user");
        cmd.add(name);
        cmd.add("/delete");
        commandFactory = new CommandFactory();
        commandFactory.setCmdList(cmd);
        commandFactory.execCommand();
    }
}

-Main.java

package usermanager;

import controller.Controller;
import entity.User;

public class Main {

    public static void main(String[] args) {

        User user = new User("testuser", "Pass_123");
        Controller controller = new Controller();
        controller.getUserSAO().createUser(user);
        if (controller.getUserSAO().getCommandFactory().getExitCode() == 0) {
            System.out.println(controller.getUserSAO().getCommandFactory().getStdOutPut());
        }
//        controller.getUserSAO().deleteUser("testuser");
//        if (controller.getUserSAO().getCommandFactory().getExitCode() == 0) {
//            System.out.println(controller.getUserSAO().getCommandFactory().getStdOutPut());
//        }
    }
}


The code above is to create a user with password (testuser/Pass_123). To delete a user, you can use the below..

-Main.java

package usermanager;

import controller.Controller;

public class Main {

    public static void main(String[] args) {

//        User user = new User("testuser", "Pass_123");
        Controller controller = new Controller();
//        controller.getUserSAO().createUser(user);
//        if (controller.getUserSAO().getCommandFactory().getExitCode() == 0) {
//            System.out.println(controller.getUserSAO().getCommandFactory().getStdOutPut());
//        }
        controller.getUserSAO().deleteUser("testuser");
        if (controller.getUserSAO().getCommandFactory().getExitCode() == 0) {
            System.out.println(controller.getUserSAO().getCommandFactory().getStdOutPut());
        }
    }
}

Now, I will replace the main class with java swing class and have it so basic to accept user names in TextArea component then create users based on that along with their passwords..

-create JFrame class call it MainFrame

-from swing palette just add by drag and drop two text areas and 2 labels and 1 button
-Declare as instance variable and initialize it in the constructor

Controller controller;
public MainFrame() {
controller = new Controller();
..omitted

it will look like the snapshot

In the actionevent of the Add command you will add the below code

  for (String s: userTextArea1.getText().split("\\n+")) {
            controller.getUserSAO().deleteUser(s);
            if (controller.getUserSAO().getCommandFactory().getExitCode() == 0)
            logArea.append(s+" user is deleted\n");
        }

on windows cmd:
-net user (to check users were created)

while in the action event of Delete command you will put the below code

for (String s: userTextArea1.getText().split("\\n+")) {
            controller.getUserSAO().deleteUser(s);
            if (controller.getUserSAO().getCommandFactory().getExitCode() == 0)
            logArea.append(s+" user is deleted\n");
        }


after creation
-net user (to check users)
after deletion
-net user



jar url https://github.com/samir82show/devops/blob/master/Usertest.zip
use java 8
Samir











Saturday, May 27, 2017

jar file to execute external system commands

Since I like to use command line in the system administration,I realized that having GUI interface is becoming highly required more and more, so, I decided to use Java in my applications whether they are desktop (swing) or web based.

In order to achieve this(execute external scripts/commands), java provides two ways, Runtime and ProcessBuilder.

I decided to use ProcessBuilder to make jar file which one you add it to your library will offer you executing external commands as well as getting the Standard Output, Standard Error and the exit status.

Download here.

Here is how to use it..
  • Add the jar command-factory.jar file to the project library
  • Declare and initialize the class e.g. 
    • CommandFactory processFactory = new CommandFactory();
  • Declare and initialize list of string e.g. 
    • List<String> cmd = new ArrayList<>();
    • cmd.add("python"); 
    • cmd.add("c:\\test.py");
  • Set the command to be executed e.g. 
    • processFactory.setCmdList(cmd);
  • Call execute method e.g. 
    • processFactory.execCommand();
  • You can check the exit status 
    • e.g. processFactory.getExitCode();
  • Standard output e.g. 
    • processFactory.getStdOutPut();
  • Standard error e.g. 
    • processFactory.getStdError();
  • Check the executed command e.g. 
    • processFactory.getCmdList();

Tuesday, December 20, 2016

Avamar automating image level backup

Good whatever time you are in,

In the Avamar as server backup solution, we take Guest level backup, Application (Oracle, SQL server, Exchange..etc.) backups or VM snapshot backup.

Now, for the guest level backup you can use "msiexec ..etc." command to install client and register it with avamar from the client side. But when it comes to VM snapshot backup, it has to be configured from Avamar side. This task is painful if you are migrating from old backup solution like networker to avamar and specially when number of VMs is more than 500.

So, I decided to write script to deploy the VMs accepting the required inputs, searching for the client in the vcenter, adding it and adding the client to the respective group.

The only type of VM clients you can't add is VM client with "spaces" inside the name.

Check and your feedback is appreciated.

Saturday, January 16, 2016

networker backup speed report

To assess the backup performance you need to know the duration and the backup rate, however this is not available in networker command line utilities, so I wrote a script that collect the data a client and date passed as arguments to the script then it will give you the backup details adding the duration of the backup in hours, seconds and the rate MB/s, all you have to do is to fill the backup server name inside the script "backup_server = BKP_SERVER_NAME".

Your feedback is appreciated.

Tuesday, December 29, 2015

VPLEX report

This script is written to report the VPLEX capacity distribution, as a prerequisites use python 3.3 or the minimum version supports "subprocess.check_output", install openpyxl module; then replace this with your credentials, ip and email details

USERNAME = 'UserName'
PASSWORD = 'PassWord'
VPLEX_IP = 'VPLEX_IP'
EMAIL_FROM = 'report@company.com'
EMAIL_TO = 'email@company.com'

your feedback is appreciated,