Saturday, 2 May 2015

Apache HTTP Server

Apache HTTP server is a web server. A Web server serves static pages to the browser. It receives the request from browser and serves the response. You can enhance the Apaches abilities by installing other modules, so that it can serve dynamic webpages using various technologies such as PHP, CGI, Servlets etc., but the core of Apache is just a plain old HTTP server.

a. Web browser sends a request to the server for a web page.
         Ex: http://mydomain/pages/home.html

b. Web server looks for the document on the file system and retrieves documents from file system

c. Once the document is received, web server sends the data of the document to the browser.

A simple web server accepts connections from web browser, read the request from browser, Retrieve the static (or) dynamic content and return it to the browser. Close the connection with browser.

I hope with this you got some basic idea of web server. Follow the link, to know how to install Apache HTTP server on Ubuntu.

Prevoius                                                 Next                                                 Home

Ubuntu : Install SSL cerificate on Apache


Following post, explains, how to generate self-signed certificate for Apache. Before proceeding, install Apache using the link.

Step 1: Open shell Run the following commands, to enable SSL.
#a2enmod ssl

Step 2: Create a directory “/etc/apache2/ssl ”.
mkdir /etc/apache2/ssl 

Step 3: Run the following command to generate SSL certificate.
openssl req -newkey rsa:2048 -x509 -sha256 -days 365 -nodes -out /etc/apache2/ssl/apache.pem -keyout /etc/apache2/ssl/apache.key
Once you entered above command, it prompts for number of inputs to generate a certificate. This example will create a certificate valid for 365 days.
# openssl req -newkey rsa:2048 -x509 -sha256 -days 365 -nodes -out /etc/apache2/ssl/apache.pem -keyout /etc/apache2/ssl/apache.key
Generating a 2048 bit RSA private key
........+++
...................+++
writing new private key to '/etc/apache2/ssl/apache.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:IN
State or Province Name (full name) [Some-State]:Karnataka
Locality Name (eg, city) []:Bangalore
Organization Name (eg, company) [Internet Widgits Pty Ltd]:JavaTuorial
Organizational Unit Name (eg, section) []:SSl
Common Name (e.g. server FQDN or YOUR name) []:hariJavaTutorial
Email Address []:


Step 4: Configure Apache to use the Self-Signed Certificate.
Open file “/etc/apache2/sites-available/default-ssl.conf”. Edit the virtual host configuration files for sites which you would like to enable SSL on. For each virtual host, you must add the following stanza.

<IfModule mod_ssl.c>
        <VirtualHost 127.0.0.1:443>
                ServerAdmin webmaster@localhost
                DocumentRoot /var/www/html
                ErrorLog ${APACHE_LOG_DIR}/error.log
                CustomLog ${APACHE_LOG_DIR}/access.log combined
                SSLEngine on
                SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
                SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

                <FilesMatch "\.(cgi|shtml|phtml|php)$">
                                SSLOptions +StdEnvVars
                </FilesMatch>
                <Directory /usr/lib/cgi-bin>
                                SSLOptions +StdEnvVars
                </Directory>

                BrowserMatch "MSIE [2-6]" \
                                nokeepalive ssl-unclean-shutdown \
                                downgrade-1.0 force-response-1.0
                BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

        </VirtualHost>
</IfModule>


Step 5: Enable SSL site entries with following command.
a2ensite default-ssl

Step 6: Restart web server using following command.
#service apache2 restart

Step 7: Now you can open localhost using https.
Open browser, and type “https://localhost” in your browser’s URL text box. You will get “Apache2 Ubuntu Default Page”.


Note:
If you want to disable https, open “/etc/apache2/sites-enabled/default-ssl.conf” and update the variable SSLEngine as off.

Prevoius                                                 Next                                                 Home

Install Apche HTTP Server on Ubuntu


Step 1: Open terminal and enter the following command.
sudo apt-get install apache2

Step 2: Start web server.
/etc/init.d/apache2 start

Step 3: Open browser, and type “http://localhost” in your browser’s URL text box. You will get “Apache2 Ubuntu Default Page”.

Note:
Restart Apache 2 Server
# /etc/init.d/apache2 restart 

Stop Apache 2 Server
# /etc/init.d/apache2 stop


Prevoius                                                 Next                                                 Home

Thursday, 30 April 2015

Apache avro : Serializing and deserializing without code generation

In previous post, I explained, how to serialize and deserialize data using Apache avro. In previous post to perform serialization and deserialization, I generated Employee.java, by using “avro-tools” library. In this post, I am going to explain, how to serialize and deserialize the data without code generation.

It is three-step process.
   1.   Create Employee object from the schema file.
   2.   Serialize the employee object
   3.   Deserialize the employee object.

employee.avsc
{"namespace": "tutorial.model",
 "type": "record",
 "name": "Employee",
 "fields": [
     {"name": "firstName", "type": "string"},
     {"name": "lastName", "type": "string"},
     {"name": "age",  "type": "int"},
     {"name": "id",  "type": "string"},
     {"name" : "company", "type" : "string"}
 ]
}

Following code is used to create an Employee object from schema file.

/* Read schema definition and create schema object */
Schema schema = new Schema.Parser().parse(new File(schemaFileName));

/* Use the schema and create an employee object */
GenericRecord employee = new GenericData.Record(schema);

/* Define employee */
employee.put("firstName", "Hari krishna");
employee.put("lastName", "Gurram");
employee.put("age", 27);
employee.put("id", "E432123");
employee.put("company", "ABCD");

Follow the steps to implement complete Application.
Step 1: Create Eclipse maven project “avro_tutorial”.

File -> New -> Other

Select Maven Project and press Next

Select the check box “Create a simple project (Skip archetype selection) and press Next.

Give Group Id and Artifact Id as “avro_tutorial” and press Finish.

Step 2: Open “pom.xml” and update dependencies for avro.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>avro_tutorial</groupId>
 <artifactId>avro_tutorial</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <properties>
  <avro_version>1.7.7</avro_version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.apache.avro</groupId>
   <artifactId>avro</artifactId>
   <version>${avro_version}</version>
  </dependency>
 </dependencies>
</project>


Step 3: Create “EmployeeUtil.java” under the package “tutorial.main”.
package tutorial.main;

import java.io.File;
import java.io.IOException;

import org.apache.avro.Schema;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;

public class EmployeeUtil {
 public static void serializeEmployee(GenericRecord emp,
   String serializedFileName, String schemaFileName)
   throws IOException {

  /* Read schema definition and create schema object */
  Schema schema = new Schema.Parser().parse(new File(schemaFileName));

  DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(
    schema);
  DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<GenericRecord>(
    datumWriter);

  dataFileWriter.create(schema, new File(serializedFileName));
  dataFileWriter.append(emp);
  dataFileWriter.close();
 }

 public static GenericRecord deSerializeEmployee(String serializedFileName,
   String schemaFileName) throws IOException {

  /* Read schema definition and create schema object */
  Schema schema = new Schema.Parser().parse(new File(schemaFileName));

  // De-serialize employee from disk
  File file = new File(serializedFileName);

  // Deserialize users from disk
  DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(
    schema);

  DataFileReader<GenericRecord> dataFileReader = new DataFileReader<GenericRecord>(
    file, datumReader);

  GenericRecord employee = null;
  while (dataFileReader.hasNext()) {
   /*
    * Reuse employee object by passing it to next(). This saves us from
    * allocating and garbage collecting many objects for files with
    * many items.
    */
   employee = dataFileReader.next(employee);
  }

  dataFileReader.close();
  return employee;
 }

 public static GenericRecord createEmployee(String schemaFileName)
   throws IOException {

  /* Read schema definition and create schema object */
  Schema schema = new Schema.Parser().parse(new File(schemaFileName));

  /* Use the schema and create an employee object */
  GenericRecord employee = new GenericData.Record(schema);

  /* Define employee */
  employee.put("firstName", "Hari krishna");
  employee.put("lastName", "Gurram");
  employee.put("age", 27);
  employee.put("id", "E432123");
  employee.put("company", "ABCD");

  return employee;
 }

}


Step 4: Create “Main.java”.

package tutorial.main;

import java.io.IOException;

import org.apache.avro.generic.GenericRecord;

public class Main {
 public static void main(String args[]) throws IOException {
  GenericRecord employee = EmployeeUtil.createEmployee("employee.avsc");

  EmployeeUtil.serializeEmployee(employee, "ser.out", "employee.avsc");

  GenericRecord employee1 = EmployeeUtil.deSerializeEmployee("ser.out", "employee.avsc");
  
  System.out.println(employee1);
 }
}

Run “Main.java”, you will get output like below.

{"firstName": "Hari krishna", "lastName": "Gurram", "age": 27, "id": "E432123", "company": "ABCD"}

Total project structure looks like below.
Prevoius                                                 Next                                                 Home