Sunday, June 27, 2021

GIT local server

 ref https://www.linux.com/training-tutorials/how-run-your-own-git-server/

Git is a versioning system developed by Linus Torvalds, that is used by millions of users around the globe. Companies like GitHub offer code hosting services based on Git. According to reports, GitHub, a code hosting site, is the world’s largest code hosting service. The company claims that there are 9.2M people collaborating right now across 21.8M repositories on GitHub. Big companies are now moving to GitHub. Even Google, the search engine giant, is shutting it’s own Google Code and moving to GitHub.

Run your own Git server

GitHub is a great service, however there are some limitations and restrictions, especially if you are an individual or a small player. One of the limitations of GitHub is that the free service doesn’t allow private hosting of the code. You have to pay a monthly fee of $7 to host 5 private repositories, and the expenses go up with more repos.

In cases like these or when you want more control, the best path is to run Git on your own server. Not only do you save costs, you also have more control over your server. In most cases a majority of advanced Linux users already have their own servers and pushing Git on those servers is like ‘free as in beer’.

In this tutorial we are going to talk about two methods of managing your code on your own server. One is running a bare, basic Git server and and the second one is via a GUI tool called GitLab. For this tutorial I used a fully patched Ubuntu 14.04 LTS server running on a VPS.

Install Git on your server

In this tutorial we are considering a use-case where we have a remote server and a local server and we will work between these machines. For the sake of simplicity we will call them remote-server and local-server.

First, install Git on both machines. You can install Git from the packages already available via the repos or your distros, or you can do it manually. In this article we will use the simpler method:

sudo apt-get install git-core

Then add a user for Git.

sudo useradd git
passwd git

In order to ease access to the server let’s set-up a password-less ssh login. First create ssh keys on your local machine:

ssh-keygen -t rsa

It will ask you to provide the location for storing the key, just hit Enter to use the default location. The second question will be to provide it with a pass phrase which will be needed to access the remote server. It generates two keys – a public key and a private key. Note down the location of the public key which you will need in the next step.

Now you have to copy these keys to the server so that the two machines can talk to each other. Run the following command on your local machine:

cat ~/.ssh/id_rsa.pub | ssh git@remote-server "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"

Now ssh into the server and create a project directory for Git. You can use the desired path for the repo.

git@server:~ $ mkdir -p /home/swapnil/project-1.git

Then change to this directory:

cd /home/swapnil/project-1.git

Then create an empty repo:

git init --bare
Initialized empty Git repository in /home/swapnil/project-1.git

We now need to create a Git repo on the local machine.

mkdir -p /home/swapnil/git/project

And change to this directory:

cd /home/swapnil/git/project

Now create the files that you need for the project in this directory. Stay in this directory and initiate git:

git init 
Initialized empty Git repository in /home/swapnil/git/project

Now add files to the repo:

git add .

Now every time you add a file or make changes you have to run the add command above. You also need to write a commit message with every change in a file. The commit message basically tells what changes were made.

git commit -m "message" -a
[master (root-commit) 57331ee] message
 2 files changed, 2 insertions(+)
 create mode 100644 GoT.txt
 create mode 100644 writing.txt

In this case I had a file called GoT (Game of Thrones review) and I made some changes, so when I ran the command it specified that changes were made to the file. In the above command ‘-a’ option means commits for all files in the repo. If you made changes to only one you can specify the name of that file instead of using ‘-a’.

An example:

git commit -m "message" GoT.txt
[master e517b10] message
 1 file changed, 1 insertion(+)

Until now we have been working on the local server. Now we have to push these changes to the server so the work is accessible over the Internet and you can collaborate with other team members.

git remote add origin ssh://git@remote-server/repo-<wbr< a="">>path-on-server..git

Now you can push or pull changes between the server and local machine using the ‘push’ or ‘pull’ option:

git push origin master

If there are other team members who want to work with the project they need to clone the repo on the server to their local machine:

git clone git@remote-server:/home/swapnil/project.git

Here /home/swapnil/project.git is the project path on the remote server, exchange the values for your own server.

Then change directory on the local machine (exchange project with the name of project on your server):

cd /project

Now they can edit files, write commit change messages and then push them to the server:

git commit -m 'corrections in GoT.txt story' -a
And then push changes:
git push origin master

I assume this is enough for a new user to get started with Git on their own servers. If you are looking for some GUI tools to manage changes on local machines, you can use GUI tools such as QGit or GitK for Linux.

QGit

Using GitLab

This was a pure command line solution for project owner and collaborator. It’s certainly not as easy as using GitHub. Unfortunately, while GitHub is the world’s largest code hosting service; its own software is not available for others to use. It’s not open source so you can’t grab the source code and compile your own GitHub. Unlike WordPress or Drupal you can’t download GitHub and run it on your own servers.

As usual in the open source world there is no end to the options. GitLab is a nifty project which does exactly that. It’s an open source project which allows users to run a project management system similar to GitHub on their own servers.

You can use GitLab to run a service similar to GitHub for your team members or your company. You can use GitLab to work on private projects before releasing them for public contributions.

GitLab employs the traditional Open Source business model. They have two products: free of cost open source software, which users can install on their own servers, and a hosted service similar to GitHub.

The downloadable version has two editions – the free of cost community edition and the paid enterprise edition. The enterprise edition is based on the community edition but comes with additional features targeted at enterprise customers. It’s more or less similar to what WordPress.org or WordPress.com offer.

The community edition is highly scalable and can support 25,000 users on a single server or cluster. Some of the features of GitLab include: Git repository management, code reviews, issue tracking, activity feeds, and wikis. It comes with GitLab CI for continuous integration and delivery.

Many VPS providers such as Digital Ocean offer GitLab droplets for users. If you want to run it on your own server, you can install it manually. GitLab offers an Omnibus package for different operating systems. Before we install GitLab, you may want to configure an SMTP email server so that GitLab can push emails as and when needed. They recommend Postfix. So, install Postfix on your server:

sudo apt-get install postfix

During installation of Postfix it will ask you some questions; don’t skip them. If you did miss it you can always re-configure it using this command:

sudo dpkg-reconfigure postfix

When you run this command choose “Internet Site” and provide the email ID for the domain which will be used by Gitlab.

In my case I provided it with:

This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 

Use Tab and create a username for postfix. The Next page will ask you to provide a destination for mail.

In the rest of the steps, use the default options. Once Postfix is installed and configured, let’s move on to install GitLab.

Download the packages using wget (replace the download link with the latest packages from here) :

wget https://downloads-packages.s3.amazonaws.com/ubuntu-14.04/gitlab_7.9.4-omnibus.1-1_amd64.deb

Then install the package:

sudo dpkg -i gitlab_7.9.4-omnibus.1-1_amd64.deb

Now it’s time to configure and start GitLabs.

sudo gitlab-ctl reconfigure

You now need to configure the domain name in the configuration file so you can access GitLab. Open the file.

nano /etc/gitlab/gitlab.rb

In this file edit the ‘external_url’ and give the server domain. Save the file and then open the newly created GitLab site from a web browser.

GitLab 1

By default it creates ‘root’ as the system admin and uses ‘5iveL!fe’ as the password. Log into the GitLab site and then change the password.

GitLab 2

Once the password is changed, log into the site and start managing your project.

GitLab manage project page

GitLab is overflowing with features and options. I will borrow popular lines from the movie, The Matrix: “Unfortunately, no one can be told what all GitLab can do. You have to try it for yourself.”

Thursday, March 11, 2021

REST File Upload

 https://www.w3spoint.com/jax-rs-file-upload

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
 
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
 
 
@Path("/file")
public class UploadFileService {
 @POST
 @Path("/upload")
 @Consumes(MediaType.MULTIPART_FORM_DATA)
 public Response uploadFile(
  @FormDataParam("file") InputStream uploadedInputStream,
  @FormDataParam("file") FormDataContentDisposition fileDetail) {
 
  String uploadedFileLocation = "D://" + fileDetail.getFileName();
  writeToFile(uploadedInputStream, uploadedFileLocation);
  String output = "File uploaded to : " + uploadedFileLocation;
  return Response.status(200).entity(output).build();
 }
 
 private void writeToFile(InputStream uploadedInputStream,
  String uploadedFileLocation) {
 
  try {
	OutputStream out = new FileOutputStream(new File(
			uploadedFileLocation));
	int read = 0;
	byte[] bytes = new byte[1024];
 
	out = new FileOutputStream(new File(uploadedFileLocation));
	while ((read = uploadedInputStream.read(bytes)) != -1) {
		out.write(bytes, 0, read);
	}
	out.flush();
	out.close();
  } catch (IOException e) {
	e.printStackTrace();
  }
 }
}

Saturday, July 30, 2016

JAVA REST with NETBEANS

http://ayazroomy-java.blogspot.in/2013/08/java-webservice-tutorial-part-11.html

Java WebService Tutorial - Part 11 (Writing Simple REST WebService using Netbeans & GlassFish)


In this part we will see how to write a Simple REST Style Web Service using NetBeans and GlassFish Server Server and we will test the service.

Requirements:

1. NetBeans ID
2. GlassFish Server.

Usually NetBeans comes along with the GlassFish Server and also in build support for generating REST Services using the Jersey Framework.
So , once we have installed the NetBeans ID and GlassFish we can create our first webservice now.

Note : This is a quick guide of creating REST using Jersey Framework, we are not going in detail about the annotations we are using in the REST Webservice , we will discuss the basic annotation right now ,in later part we will cover in detail about each annotations with respective example.

Step 1:

Open NetBeans ID.

File-->New ---> New Project --> Java Web-->Web Application and click the "Next" Button.

Give a name to the Project say "MyFirstREST" (I am using this name here for this example) and click the "Next" Button.

Select the Server as "GlassFish Server" and select the J2EE Version as "Java EE 5" click the "Finish" Button.
So , now our project folders are created .














Step 2:

Creating Package.

Right click on the Project Node "MyFirstREST" and select :

New ---> Java Package and give name as "com.test" (You can provide your own name for this example i am using this name)
Now we have a package where all our service class will be kept.

Step 3:

Writing the REST Business Logic.

 There are couple of annotations i liked to discussed here, these are the very basic and commonly used annotations.

@Path  -- Mention the Path from which you want to access a REST Service either class level or method level.

@GET -- Performs HTTP get Operation useful for getting info read only.

@Produces - This Produces the Respective output in different format such as XML,JSON,TEXT,HTML etc to the client.

Step 4:

Implementing the Jersey Framework & Writing our First REST Service 

NetBeans generally comes along with the Jersey Framework if you don't have the Framework install , manually you should have to download it and add the jars to the libraries.

But i am not going to discuss about that , i assume you all are smart guys , and you have NetBeans with J2EE Supported version , so basically you will have Jersey supported framework install in the NetBeans Itself.

So , We don't need actually write any code from implementing this frame work in web.xml, NetBeans automatically does for us., that's the advantage of using NetBeans.

So now this can achieve by following these steps :

Right click on the "com.test"(Package i am using for this example, you can use your own)ans select 

New--->Other--->WebService-->RESTful WebServices from Patterns.

















Select patterns as "Simple Root Resource" and Press the "Next" Button















Give path Name as"MyPath" and class Name as"MypathResource" and select the MIME Type as "text/plain". and importantly select the check box for Jersey framework and click the "finish" Button.



















Step 5:

Develop the code.

By default a class name called "MypathResource" will be created with some methods and instance variable define in it.Delete the code and replace with the following code.

package com.test;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
@Path("Mypath")
public class MypathResource {
    @GET
    @Produces("text/plain")
    public String getText() {
        return "My First RESTful Servivce....";
    }
}

This is the very basic and simplest RESTful Service , it will accept the Path "MyPath" in the URL and invoke the method getText which in turn return a Response as Text with the wording "My First RESTful Servivce..."
 
Step 6:

Clean & Build , Deploy to check the service.

Right Click on the Project Node "MyFirstREST" and select Clean and build this will clean the directory and compiles the Java files and  creates a WAR Archive.

Once , the Clean and build is finish successfully again RightClick on the ProjectNode "MyFirstREST" and select "Deploy".

Once the Deployment is done we can check the service by:
Right click on the REST Service created under folder called "Restful Web Service" and  select "Test Resource Uri"

















or  we can directly access using the URL

Note : The Port Number will differ based upon your Server Configuration Setting.

Result :

















Main Components :

How all these is happening ?

The main Gate Keeper or the main Servlet for this is specified in the web.xml which performs the Jersey Mechanism , Please refer the image below for detail explanation of Jersey Servlet define in web.xml .
























That's all for the day, In the Next Section we will be seeing different useful annotations and how to produce different form of Outputs.


REST Annotations & Classes:

Hi, in this Section we will be discussing the different types of Annotations and some of the mostly used classes, provided by REST which can we use in our code.

First off, the most important Points we need to remember is:

REST is Web Service which performs all its Operation based upon the HTTP Methods. So it provides the following annotations.

@ GET
@POST
@PUT
@DELETE
@HEAD

If any one similar with the HTTP methods, they same they behave also here.

Some of the mostly used Annotations:

@Produces - This one we already discuss in our previous Sections, Any way it is used to produce a response to the User in based upon different MIME Types.( ex: text/html )

@Consumes - It is used to define what type of MIME or Inputs it will accepts from the Client .ex:  forms--URL--encoded.

@Context - It like the ServletContext in Servlet , it is the Jersey Framework context. It can used in many cases such as to get Header Parameters, Query parameters, Form Parameters etc.

Accessing Parameters in REST :

REST provides the following ways, the Param can be represented.

1.@PathParam
2.@QueryParam
3.@FormParam
4.@MatrixParam

1.@PathParam:

This annotation is used to get the Parameter specified in the URI using {} either from class level or Method Level.

Ex :CLASS LEVEL

@Path("/MyPath/{username}")
class MyPathResource
{
@GET
@Produces("text/plain")
public String getText(@PathParam("username"}String username)
{
return "UserName:"+username;
}
}

Access from URL :

Ayaz - Here taken as value for "username" and map to the Methods getText.Since getText does not have any path Associated with it the Jersey will automatically invokes this method.

Note: If there are more than one method specifies in the class with no path Annotation, then class will compile fine but the deployment will be fail.

EX: METHOD LEVEL

@Path("/MyPath/{username}")
class MyPathResource
{
@GET
@Produces("text/plain")
public String getText(@PathParam("username"}String username)
{
return "UserName:"+username;
}
@Path("/getText1/{text}")
@GET
@Produces("text/plain")
public String getText1(@PathParam("text"}String text)
{
return "Enter text:"+text;
}
}
Access from URL :

http://localhost:8080/MyPath/Ayaz  ---> Gives Output as UserName : Ayaz

http://localhost:8080/MyPath/getText1/HelloWorld  ---> Gives output as Enter text : HelloWorld

Note: If no value is pass for name  error will be thrown at runtime.


2. @Queryparam:

Queryparam is used to access the name and value pair enter in the URL using "?" .
It can be defined at class level and Method level; mostly it will be used in Method level. We will discuss Method level only.

Ex:

Here , name and age are the two Query parameters.

Ex:

    @Path("/MyMethod")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String Query(@QueryParam("name")String name ,
    @QueryParam("age")String  age )
    {
        return "Query Parameters"+" "+"Name:"+name+" "+"Age:"+age;
    }
Note: If no value is pass for name  error will be thrown at runtime.

3. @FormParam :

Form param is used to obtain values of the Form elements.

Ex:

  @Path("/posted")
    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String  putJson(@FormParam("name")String name ) {    
        return name;
    }

Here, name is the name of the text field or some other field declared inside the Form tag. 
Note: If no value is pass for name null will be return.

4. @MatrixParam :

The Matrix Param is used to accept values in name & value pair unlike Query Parameter it does not need any questionmark to begin with nor any and (&)sign to specify multiple values. Multiple values can be represented using semicolon(;)

Ex:
 @Path("/MyMethod")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String  putMatrix(@MatrixParam("name")String name,@MatrixParam("age")int age)
    {
       return "Name:"+name+"Age:"+age ;
    }

Note: If no value is pass for name and age null will be return.


Useful Classes:
  •  MediaType 
  •  Response
  • JSONObject & JSONArray

1. MediaType :

This class can be used to represent the MIME Types in the form of Constants.

Ex:

@Produces (MediaType.TEXT_PLAIN)
 Equivalent to 
@Produces ("text/plain")


2. Response:

The Response class one of the widely used , it can be used to return response as text,images,files etc.Instead of being returning as String we should return the Response Object and let Jersey to do the remaining writing to the output stream and other stuffs.

Writing text Response

Ex:

    @Path("/matrix")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Response putMatrix(@MatrixParam("name")String name,@MatrixParam("age")int age)
    {
       return Response.ok("Name:"+name+"Age:"+age).build();  
    }
 The Response.ok method takes the String as an Entity and builds it adds to the Output as response.


  Writing Image Response:

   @GET
    @Path("/getData")
    @Produces("image/jpg")
    public Response getData()
    {
        File f=new File("G:\\Icons\\en.jpg");
        Response.ResponseBuilder builder=Response.ok((Object)f);
        builder.header("Content-Disposition","attachment; filename=\"file_from_server.jpg\"");
                return builder.build();
    }
   
 3.JSONObject  & JSONArray :

The JSON Object class is used to create a list of JSON Objects. 

Ex:

        JSONObject jSONObject=new JSONObject();
        jSONObject.put("FirstName", "Jack");
        jSONObject.put("LastName", "Sparrow");
        jSONObject.put("Address", "America");
        System.out.println(jSONObject);

    Output :
    {"FirstName":"Jack","LastName":"Sparrow","Address":"America"}
   
    The JSON Array class is used to create an Array of JSON Objects.

Ex:     

       JSONObject jSONObject=new JSONObject();
        jSONObject.put("FirstName", "Jack");
        jSONObject.put("LastName", "Sparrow");
        jSONObject.put("Address", "America");
        JSONArray array=new JSONArray();
        array.put(jSONObject);
        System.out.println(array);

Output:  [{"FirstName":"Jack","LastName":"Sparrow","Address":"America"}]

Even though Jersey with JAXB, support automatic conversion or implementation of JSON and XML from simple Java Bean with setter/getters, It is good to know how to do it manually.

In the Next Part we will see how to produce XML and JSON output manually, and with Automatic Implementation Provided by Jersey along with JAXB.

Producing XML and JSON:

In the previous section we have seen how to access Parameters from URL and what are useful classes we can use in our REST Services. In this section we will see how to Produce different Responses such as XML and JSON in REST.

So far in our all example, we have used GET and access directly from the Browser URL.What if some one wants to POST data , in this case i need to used POST Method, let see the following example here.

    @POST
    @Path("/Postme")
    @Produces("text/plain")
    public Response getData(@FormParam("user")String user)
    {
        return Response.ok(user).build();
    }

I cannot go directly in to the Browser and hit the URL with the Path .../Postme it will give me an error saying Method not allowed, we cannot directly do that, either we need a form to Post these changes and we need to write a client class to access this service. How to write a Client to access REST Service will be cover in later Parts. Now we will see how to do this by using a Html Form Submission.

Ex:

<form method="post" action="/....../Postme">
User :<input type="text" name="user">
<input type="submit">
</form>

When the submit button is pressed the "/Postme"Path 's respective Method get's invoked and it takes the parameter "user" which is a textfield inside the form tag ,and return the Response as the value entered in the html textfield.

As , for Producing JSON and XML we can use get or post it depend upon the criteria, now for our understanding , i am using GET to Produce  JSON and XML Data and display it to the User.

1. XML :

XML can be produce in two ways , either using the JAXB if you don't know JAXB , no need to Panic it just an API Provided by J2EE for quickly forming XML ,Parsing XML and Producing XML.

It takes some little Annotations that's it it wont take much time to understand the basics of JAXB.
Jersey , automatically does the XML /JSON Conversion, while returning response.

Another way to produce the XML is by returning as String in the Response, it is not the best way , but it can also produce XML Output.

EX:
    @Path("/check/{username}")
    @GET
    @Produces("text/xml")
    public String getText(@PathParam("username")String username) {
     return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>   <username>"+username+"</username></root>";
    }

This code will Produce the output as String in the XML Format.

 If you access from URL:  ....../check/John

Output :<root><username>John</username></root>

Using JAXB Style:

Create a Class called "Student".

package test;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement // This does everthing for Creating XML .
public class student {
private int rollno;
 private String name;
  public int getRollno() {
        return rollno;
    }
    public void setRollno(int rollno) {
        this.rollno = rollno;
    }
public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }  
}

Inside the REST Service Class:

    @Path("/getXML")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public student getXML()
    {
        student s=new student();
        s.setName("Ayaz");
        s.setRollno(1440);
        return s;
    }
Deploy the Service & Access this Method ....../getXML  will return the following Output:

XML Output :
<student><name>Ayaz</name><rollno>1440</rollno></student>

Note : Just By changing MediaType.APPLICATION_XML to
MediaType.APPLICATION_JSON in the @Produces Annotation of the Method getXML will give the following JSON Output 

JSON Output :
{"rollno":1440,"name":"Ayaz"}

2. JSON:

We have already seen the how to produce JSON output using JAXB, Now we will look at how we can do it manually using the JSON classes provided by the Jackson framework which comes along with the Jersey.
It can be done by specifying the following in the web.xml file:
          <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
              </init-param>
This enables the Jackson framework which comes along with the Jersey.Jackson used to provide the libraries to use for accessing and producing JSON.

Ex:
    @Path(“/Mydata”)
    @GET
    @Produces("application/json")
    public Response getData() throws JSONException
    {
        JSONObject jSONObject=new JSONObject();
        JSONArray array=new JSONArray();
        
       for(int i=0;i<5;i++)
       {
        jSONObject.put("name"+i, i);
          
       }
        array.put(jSONObject);
        String h=array.toString();
        Response r=null;
        r=Response.ok(h).build();
        return r;
    }

Output:
[{"name0":0,"name1":1,"name2":2,"name3":3,"name4":4}]

That's all folks , in the next section we will see how to write a REST client.