Spring File Down-loader

File download is very common to any type of web application. Let’s see how this done in spring framework and java. This is pretty easy and you can add/remove code segments as you want. The given code segments are written as I thought and comments will give you an idea about each code.

Let’s go then, Please follow below simple steps to create your own file down-loader;

Step 1:

Create the spring controller.

  @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
  public void getUserProfilePage(@RequestParam("fileName") String fileName,
  HttpServletRequest request, HttpServletResponse response) {
    FileDownloadManager downloadManager = new FileDownloadManager();
    try {
      downloadManager.downloadFile(fileName, response);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

Step 2:

Create a java Class “DownloadManager” and create below method.

public void downloadFile(String fileName, HttpServletResponse response) throws IOException {
    // reads input file from an absolute path
    try {
      //get fixed file path from the property file of the project
      String filePath = CommonUtil.getValueFromFile("application", "file.upload.location") + fileName;
      File downloadFile = new File(filePath);
      FileInputStream inStream = new FileInputStream(downloadFile);

      // gets MIME type of the file
      String mimeType = new MimetypesFileTypeMap().getContentType(downloadFile);
      if (mimeType == null) {
        // set to binary type if MIME mapping not found
        mimeType = "application/octet-stream";
      } else if (StringUtils.equals(FilenameUtils.getExtension(filePath), "pdf")
          || StringUtils.equals(FilenameUtils.getExtension(filePath), "PDF")) {
        mimeType = "application/pdf";
      }
      System.out.println("MIME type: " + mimeType);

      // modifies response
      response.setContentType(mimeType);
      response.setHeader("Content-Disposition", "inline; filename=" + downloadFile.getName());
      response.setContentLength((int) downloadFile.length());

      // if you want to forces download you can use below
      // String headerKey = "Content-Disposition";
      // String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
      // response.setHeader(headerKey, headerValue);

      // obtains response's output stream
      OutputStream outStream = response.getOutputStream();

      byte[] buffer = new byte[4096];
      int bytesRead = -1;

      while ((bytesRead = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, bytesRead);
      }

      inStream.close();
      outStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

For mime type; you can use different java libraries like MimeMagic, Tika or use URLConnection. But I have used simple MimetypesFileTypeMap which is available form java 1.7 as I know.

Now you can enjoy the file down-loader by accessing the spring controller via browser. Please contact me for further information.

Generating QR Code

Step 01:

Add below to your maven repository.

<!-- https://mvnrepository.com/artifact/net.glxn/qrgen -->
<dependency>
    <groupId>net.glxn</groupId>
    <artifactId>qrgen</artifactId>
    <version>1.4</version>
</dependency>

Step 02:

Add below java util class in to your project and use it as you want. Please note this util class is coded as I wanted, you can amend the code as you want; anyway I added comments to describe the code for you.

package lk.pwc.cult.fund.common.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

//importing libraries
import net.glxn.qrgen.QRCode;
import net.glxn.qrgen.image.ImageType;

public class QRCodeUtil {

  //pass a string generated code to embed in the qr code
  public String retrieveQRCodeImageName(String code) {

    try {
      //generating the qr code in png format and get to Byte Array Output Stream
      ByteArrayOutputStream out = QRCode.from(code).to(ImageType.PNG).stream();

      //get file path from the property file to save the created qr code png
      String filePath = CommonUtil.getValueFromFile("application", "qrcode.file.location");
      //generate a random string to save the qr coded png file
      String fileName = UUID.randomUUID().toString() + ".png";
      String imagePath = filePath + fileName;

      //this set of code uses to create the specific file path if it's not exist
      File theDir = new File(filePath);
      if (!theDir.exists()) {
        try {
          theDir.mkdirs();
          System.out.println("DIR created");
        } catch (SecurityException se) {
          se.printStackTrace();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      FileOutputStream fout = new FileOutputStream(new File(imagePath));

      //Use file output stream to write the qr code png image to a file
      fout.write(out.toByteArray());
      fout.flush();
      fout.close();

      return imagePath;
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      return null;
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }

  }

}

MongoDB

MongoDB is arise in the mid-2000s under NoSQL community. Dwight Merriman and Eliot Horowitz, who had encountered development and scalability issues with traditional relational database approaches while building Web applications at DoubleClick, an Internet advertising company that is now owned by Google Inc. According to Merriman, the name of the database was derived from the word humongous to represent the idea of supporting large amounts of data. Merriman and Horowitz helped form 10Gen Inc. in 2007 to commercialize MongoDB and related software. The company was renamed MongoDB Inc. in 2013. The database was released to open source in 2009 and is available under the terms of the Free Software Foundation’s GNU AGPL Version 3.0 commercial license.

MongoDB allow to use collections and documents instead of using tables and rows as in relational databases. Documents comprise key-value pairs. Collections contain sets of documents and functions just like database schema in relational databases.

Below are few characteristics of the MongoDB:

  • Written in: C++
  • Main point: Retains some friendly properties of SQL. (Query, index)
  • License: AGPL (Drivers: Apache)
  • Protocol: Custom, binary (BSON)
  • Master/slave replication (auto failover with replica sets)
  • Sharding built-in
  • Queries are javascript expressions
  • Run arbitrary javascript functions server-side
  • Better update-in-place
  • Uses memory mapped files for data storage
  • Performance over features
  • Text search integrated
  • GridFS to store big data + metadata
  • Has geospatial indexing
  • Data center aware

Install MongoDB

MongoDB 3 is copied to 10.161.121.8 server (D:\SHARED\LRIS_RESOURCES). Get a copy of the file(mongodb-win32-x86_64-2008plus-ssl-3.0.7-signed) and install it in your pc.

Note: install in the D:\mongodb folder for continue this guide.

Configure MongoDB

create two folders “data” and “log” in the D:\mongodb folder. Again create mongo.config file and copy paste below text.

##store data here
dbpath=D:\mongodb\data
##all output go here
logpath=D:\mongodb\log\mongo.log
##log read and write operations
diaglog=3

 

Run Mongo DB

Now you will have the below folder structure in path D:\mongodb

Capture3

run command prompt with administrator privileges, use cd command to navigate D:\mongodb folder. Then type below command to run the mongo service.

mongod --config D:\mongodb\mongo.config

 

Install Mongo DB as a windows service

You can add mongo DB service as a windows service using below command. Before executing below command make sure to use cd command to navigate D:\mongodb\bin folder.

"D:\mongodb\bin\mongod.exe" --config "D:\mongodb\mongo.config" --install

 

Access Mongo DB console

open command prompt and direct to D:\mongodb\bin folder using “cd” command. Execute below command to start the console.

mongo.exe

output:

Capture

Creating a snapshot

open command prompt and direct to D:\mongodb\bin folder using “cd” command.

mongodump --db &lt;db_name&gt;

output:

Capture1.png

There are tons of option you can use with mongodump in here.

Restore a backup

open command prompt and direct to D:\mongodb\bin folder using “cd” command.

mongorestore --db &lt;db_name&gt; &lt;dump_location&gt;

output:
drop database
Capture
restore the dump file(bson)
Capture21

There are tons of option you can use with mongorestore in here.

Latest MongoDB manual 3.0 is here.

 

MongoDB client

For people who doesn’t like command prompt can use different client software to access the server. MongoChef is one of the well known software which is support for latest MongoDB versions.

You can get a copy of the software from the 10.161.121.8 server (D:\SHARED\LRIS_RESOURCES).

 

Note : For people who are interested feel free to access MongoDB open test instance in 10.161.121.11 server. The service is running on port 27017. 

 

Application Hosting Using Docker

Docker_container_engine_logo

What is Docker ?

https://www.docker.com

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

In a way, Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.

And importantly, Docker is open source. This means that anyone can contribute to Docker and extend it to meet their own needs if they need additional features that aren’t available out of the box.

Docker Architecture

Docker consist with containers and images, We can run images on different containers. for more architectural information please refer this link.

docker_architecture

The Docker daemon
The Docker daemon runs on a host machine. The user uses the Docker client to interact with the daemon.

The Docker client
The Docker client, in the form of the docker binary, is the primary user interface to Docker. It accepts commands and configuration flags from the user and communicates with a Docker daemon. One client can even communicate with multiple unrelated daemons.

Inside Docker
To understand Docker’s internals, you need to know about images, registries, and containers.

Docker images
A Docker image is a read-only template with instructions for creating a Docker container. For example, an image might contain an Ubuntu operating system with Apache web server and your web application installed. You can build or update images from scratch or download and use images created by others. An image may be based on, or may extend, one or more other images. A docker image is described in text file called a Dockerfile, which has a simple, well-defined syntax. For more details about images, see How does a Docker image work?.

Docker images are the build component of Docker.

Docker containers
A Docker container is a runnable instance of a Docker image. You can run, start, stop, move, or delete a container using Docker API or CLI commands. When you run a container, you can provide configuration metadata such as networking information or environment variables. Each container is an isolated and secure application platform, but can be given access to resources running in a different host or container, as well as persistent storage or databases. For more details about containers, see How does a container work?.

Docker containers are the run component of Docker.

Docker registries
A docker registry is a library of images. A registry can be public or private, and can be on the same server as the Docker daemon or Docker client, or on a totally separate server. For more details about registries, see How does a Docker registry work?

Docker registries are the distribution component of Docker.

Docker services
A Docker service allows a swarm of Docker nodes to work together, running a defined number of instances of a replica task, which is itself a Docker image. You can specify the number of concurrent replica tasks to run, and the swarm manager ensures that the load is spread evenly across the worker nodes. To the consumer, the Docker service appears to be a single application. Docker Engine supports swarm mode in Docker 1.12 and higher.

Docker services are the scalability component of Docker.

Install Docker

Install on Windows – https://docs.docker.com/engine/installation/windows/

Install on Ubuntu – https://docs.docker.com/engine/installation/linux/ubuntulinux/

Note : Enable virtualization on your laptop/computer before continue docker. You can enable this option by accessing the boot menu of your computer.

How to use Docker in our projects?

Eg : Host ui-web / service-web / mysql (host two war files which is depends on one another with mysql server using docker engine. ui-web is the angular app, service-web is the spring boot application, mysql is the database server. angular app calls services on spring boot application and spring boot application access mysql database server)

Step 01 – Add Dockerfile to the angular application/ui-web

Add below code list in a file. File name should be exactly Dockerfile (no extension).

FROM jboss/wildfly
ADD ui-web.war /opt/jboss/wildfly/standalone/deployments/

Step 02 – Add Dockerfile to the Spring boot application/service-web

Add below code list in a file. File name should be exactly Dockerfile (no extension).

FROM jboss/wildfly
ADD service-web.war /opt/jboss/wildfly/standalone/deployments/

Step 03 – Create a yml file

Add below code list in a file. File name could be differ. extension shoule be .yml

db:
 image: orchardup/mysql
 environment:
 MYSQL_USER: &lt;db username&gt;
 MYSQL_PASSWORD: &lt;db password&gt;
 MYSQL_DATABASE: &lt;db name&gt;
 ports:
 - "3306"

services-web:
 build: &lt;path to the docker file eg: service/. (service is a directory, dot represent docker file)&gt;
 links:
 - db:mysql #(since service web access mysql server this is the link to access mysql image)
 ports:
 - "8080"

ui-web:
 build: ui-web/.
 links:
 - services-web:services-web #(since ui-web access service-web controller, this is the link)
 ports:
 - "8080"

Step 04 – Execute yaml file

&lt;yaml file name&gt; up

Now we have created all the images we want. You can list down created images using below command.

docker images

Result :
docker_images

Step 05 – Start mysql Server

docker run -e MYSQL_ROOT_PASSWORD=admin -p 3306:3306 --name mysql -d orchardup/mysql:latest

-e –> Set an environment variable (can be used multiple times)
-name –> Assign a name to the container
-d –> detache mode enable to run process background
orchardum/mysql –> image name
:latest –> tag name

Note: you can use any type of mysql client to access the database server to create databases or run scripts.
For more information, please refer this link.

Step 06 – Start service-web/spring boot application

docker run -p 8080:8080 --link mysql:mysql &lt;service web image name&gt;

Note : link should be set to mysql container as above.

Step 07 – Start ui-web/Angular application

docker run -p 8080:8080 --link service-web:service-web &lt;ui web image name&gt;

Note : link should be set to mysql container as above.

Note: After step 07, you may be able to access the system as you expected.

Note:

You can list your exising containers using below command.

docker ps -a

Result :
docker_all_containers

You can list your running container using below command.

docker ps

Result:
docker_active_containers

Using Volume with Docker

Docker container is volatile, data will be deleted if container is deleted. So we need volume to store container data.

A data volume is a specially-designated directory within one or more containers that bypasses the Union File System. Data volumes provide several useful features for persistent or shared data:

Volumes are initialized when a container is created. If the container’s base image contains data at the specified mount point, that existing data is copied into the new volume upon volume initialization. (Note that this does not apply when mounting a host directory.)

Data volumes can be shared and reused among containers.
Changes to a data volume are made directly.
Changes to a data volume will not be included when you update an image.
Data volumes persist even if the container itself is deleted.
Data volumes are designed to persist data, independent of the container’s life cycle. Docker therefore never automatically deletes volumes when you remove a container, nor will it “garbage collect” volumes that are no longer referenced by a container.
For more information, please refer this link.

Create Volume for Container Mysql

Use below command to create a volume for mysql container,

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin -v <volume name>:<location for the backup> --name mysql orchardup/mysql:latest

eg:

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin -v dbbackup:/var/lib/mysql --name mysql orchardup/mysql:latest

Note: when you create a volume, even though you deleted the container; you can create another container using an existing volume using above command. Then the container will be injected from the existing volume(stored data in the volume, in this scenario mysql database backup).

You can list your existing volumes under /var/lib/docker/volumes

Other Useful Commands for Docker

To delete containers,

docker rm -f <comntainer name>

To delete Images,

docker rmi -f <image name>

Note: You can use -f to force delete the container or image.

WildFly/Jboss Server Configuration

WildFly is the open source series of the jboss server. Latest WildFly version is 9.0.

If you are planning to use WildFly, below configuration will be very useful.

when you add the server double click the server and do the below configurations.

Publishing: choose “Automatically publish after a build event”. I like to change the publishing interval to 1 second too.
Application Reload Behavior: check the “Customize application reload …” checkbox and edit the regex pattern to \.jar$|\.class$
Then,

When enabled option Automatically publish when resource change then changes inside *.html, *.xhtml files are immediately reflected as soon as you refresh the browser.
To make hot deployment work for *.jsp files too, then you should inside ${wildfly-home}/standalone/configuration/standalone.xml make following change:

replace with:

Add Google Code Style Templates

Add Google Style for Java

Download xml file using this url–> eclipse-java-google-style

Instructions,

  • Open eclipse->window->preference->Java->Code Style->Formatter.
  • Import the downloaded xml file.
  • Press Apply and Ok.

How to Use,

  • You can use Ctrl+Shift+F short cut key in your java code.

Tips,

You can use auto formatting action on save (Open eclipse->window->preference->Java->Editor->Save Actions->check Perform the selected actions on save->check Format source code)

 

Add Google Style for JavaScript

Download xml file using this url–>google-style-guide-javascript-eclipse

Instructions,

  • Open eclipse->window->preference->JavaScript->Code Style->Formatter.
  • Import the downloaded xml file.
  • Press Apply and Ok.

How to Use,

  • You can use Ctrl+Shift+F short cut key in your java script code.

Tips,

You can use auto formatting action on save (Open eclipse->window->preference->JavaScript->Editor->Save Actions->check Perform the selected actions on save->check Format source code)

 

Add Google Style for HTML

Instructions,

  • Open eclipse->window->preference->Web->HTML Files->Editor.
  • Enter 140 as Line width
  • Press Apply and Ok.

 

Eclipse Short Cut Keys

1. Manage Files and Projects

Ctrl+N Create new project using the Wizard
Ctrl+Alt+N Create new project, file, class, etc.
Alt+F, then . Open project, file, etc.
Ctrl+Shift+R Open Resource (file, folder or project)
Alt+Enter Show and access file properties
Ctrl+S Save current file
Ctrl+Shift+S Save all files
Ctrl+W Close current file
Ctrl+Shift+W Close all files
F5 Refresh content of selected element with local file system

minus2. Editor Window

Focus/ cursor must be in Editor Window for these to work.
F12 Jump to Editor Window
Ctrl+Page Down/Ctrl+Page Up Switch to next editor / switch to previous editor
Ctrl+M Maximize or un-maximize current Editor Window (also works for other Windows)
Ctrl+F6/Ctrl+Shift+F6 Show list of open Editors. Similar to ctrl+e but switches immediately upon release of ctrl
Alt+Arrow Left/Alt+Arrow Right Go to previous / go to next Editor Window
Ctrl+F10 Show view menu (features available on left vertical bar: breakpoints, bookmarks, line numbers, …)
Ctrl+F10, then n Show or hide line numbers

minus3. Navigate in Editor

Home/End Jump to beginning / jump to end of indention. Press home twice to jump to beginning of line
Ctrl+Home/End Jump to beginning / jump to end of source
Ctrl+Arrow Right/Arrow Left Jump one word to the left / one word to the right
Ctrl+Shift+Arrow Down/Arrow Up Jump to previous / jump to next method
Ctrl+L Jump to Line Number. To hide/show line numbers, press ctrl+F10 and select ‘Show Line Numbers’
Ctrl+Q Jump to last location edited
Ctrl+./Ctrl+, Jump to next / jump to previous compiler syntax warning or error
Ctrl+Shift+P With a bracket selected: jump to the matching closing or opening bracket
Ctrl+[+]/Ctrl+ on numeric keyboard Collapse / Expand current method or class
Ctrl+[/]/Ctrl+* on numeric keyboard Collapse / Expand all methods or classes
Ctrl+Arrow Down/Ctrl+Arrow Up Scroll Editor without changing cursor position
Alt+Page Up/Alt+Page Down Next Sub-Tab / Previous Sub-Tab

minus4. Select Text

Shift+Arrow Right/Arrow Left Expand selection by one character to the left / to the right
Ctrl+Shift+Arrow Right/Arrow Left Expand selection to next / previous word
Shift+Arrow Down/Arrow Up Expand selection by one line down / one line up
Shift+End/Home Expand selection to end / to beginning of line
Ctrl+A Select all
Alt+Shift+Arrow Up Expand selection to current element (e.g. current one-line expression or content within brackets)
Alt+Shift+Arrow Left/Arrow Right Expand selection to next / previous element
Alt+Shift+Arrow Down Reduce previously expanded selection by one step

minus5. Edit Text

Ctrl+C/Ctrl+X/Ctrl+V Cut, copy and paste
Ctrl+Z Undo last action
Ctrl+Y Redo last (undone) action
Ctrl+D Delete Line
Alt+Arrow Up/Arrow Down Move current line or selection up or down
Ctrl+Alt+Arrow Up/Ctrl+Alt+Arrow Down/ Duplicate current line or selection up or down
Ctrl+Delete Delete next word
Ctrl+Backspace Delete previous word
Shift+Enter Enter line below current line
Shift+Ctrl+Enter Enter line above current line
Insert Switch between insert and overwrite mode
Shift+Ctrl+Y Change selection to all lower case
Shift+Ctrl+X Change selection to all upper case

minus6. Search and Replace

Ctrl+F Open find and replace dialog
Ctrl+K/Ctrl+Shift+K Find previous / find next occurrence of search term (close find window first)
Ctrl+H Search Workspace (Java Search, Task Search, and File Search)

minus7. Indentions and Comments

Tab/Shift+Tab Increase / decrease indent of selected text
Ctrl+I Correct indention of selected text or of current line
Ctrl+Shift+F Autoformat all code in Editor using code formatter
Ctrl+/ Comment / uncomment line or selection ( adds ‘//’ )
Ctrl+Shift+/ Add Block Comment around selection ( adds ‘/… */’ )
Ctrl+Shift+\ Remove Block Comment
Alt+Shift+J Add Element Comment ( adds ‘/** … */’)

minus8. Editing Source Code

Ctrl+Space Opens Content Assist (e.g. show available methods or field names)
Ctrl+1 Open Quick Fix and Quick Assist
Alt+/ Propose word completion (after typing at least one letter). Repeatedly press alt+/ until reaching correct name
Ctrl+Shift+Insert Deactivate or activate Smart Insert Mode (automatic indention, automatic brackets, etc.)

minus9. Code Information

Ctrl+O Show code outline / structure
F2 Open class, method, or variable information (tooltip text)
F3 Open Declaration: Jump to Declaration of selected class, method, or parameter
F4 Open Type Hierarchy window for selected item
Ctrl+T Show / open Quick Type Hierarchy for selected item
Ctrl+Shift+T Open Type in Hierarchy
Ctrl+Alt+H Open Call Hierarchy
Ctrl+Shift+U Find occurrences of expression in current file
Ctrl+move over method Open Declaration or Implementation

minus10. Refactoring

Alt+Shift+R Rename selected element and all references
Alt+Shift+V Move selected element to other class or file (With complete method or class selected)
Alt+Shift+C Change method signature (with method name selected)
Alt+Shift+M Extract selection to method
Alt+Shift+L Extract local variable: Create and assigns a variable from a selected expression
Alt+Shift+I Inline selected local variables, methods, or constants if possible (replaces variable with its declarations/ assignment and puts it directly into the statements)

minus11. Run and Debug

Ctrl+F11 Save and launch application (run)
F11 Debug
F5 Step Into function
F6 Next step (line by line)
F7 Step out
F8 Skip to next Breakpoint

minus12. The Rest

Ctrl+F7/Ctrl+Shift+F7 Switch forward / backward between views (panels). Useful for switching back and forth between Package Explorer and Editor.
Ctrl+F8/Ctrl+Shift+F8 Switch forward / backward between perspectives
Ctrl+P Print
F1 Open Eclipse Help
Shift+F10 Show Context Menu right click with mouse

NSSM – Windows Services

NSSM
We always need to add windows services in our deployments(client site and in office). “NSSM” is the easiest way to add windows services.

Download the NSSM file to your hard disk (no need to install / use this link to download – nssm-2.24).

Open command promt and go to the NSSM folder using “cd” command.

Use the command “nssm install” to open the nssm window.

Now you can add the executable and create windows services.

install_application

For More information – http://nssm.cc/

Install JAVA in Linux

download .bin file from oracle site.


chmod a+x [bin file name].bin

./[bin file name].bin

mv jdk1.6.0_30 java-6-oracle

sudo mkdir /usr/lib/jvm

sudo mv java-6-oracle /usr/lib/jvm

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/java-6-oracle/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/java-6-oracle/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/java-6-oracle/bin/javaws" 1

sudo chmod a+x /usr/bin/java
sudo chmod a+x /usr/bin/javac
sudo chmod a+x /usr/bin/javaws
sudo chown -R java6admin:java6admin /usr/lib/jvm/java-6-oracle