Friday, December 30, 2016

Avoid SSL in Apache HTTP client

Avoid SSL in Apache HTTP client. 

Sometimes,  you need to do REST call from Apache HTTP client where the REST endpoint has https. HTTPS may block your request since you are not a trusted party. To avoid such situation you can use following code ( This code should be only used in development and QA environment, not for production).


try
 {
    DefaultHttpClient client = new DefaultHttpClient();
 HostnameVerifier hostnameVerifier=
org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; SchemeRegistry registry = new SchemeRegistry(); SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory(); socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier); registry.register(new Scheme("https", socketFactory, 443)); SingleClientConnManager mgr =
new SingleClientConnManager(client.getParams(), registry); DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams()); HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); HttpGet requestAPI = new HttpGet(url); HttpResponse responseAPI = httpClient.execute(requestAPI); HttpEntity entity = responseAPI.getEntity(); int responseCode = responseAPI.getStatusLine().getStatusCode(); } catch(Exception e) { }

How to get size of disc and size of RAM in JAVA

Following code can help you to find size of the disc and size of the RAM. 
import java.lang.management.*;
import java.io.*;

class Main
{
    public static void main(String... a)
    {
        long diskSize = new File("/").getTotalSpace();

        long maxMemory = Runtime.getRuntime().maxMemory();
        long memorySize = ((com.sun.management.OperatingSystemMXBean) 
ManagementFactory.getOperatingSystemMXBean())
.getTotalPhysicalMemorySize();
        System.out.println("Size of C:="+diskSize+" Bytes");


        System.out.println("RAM Size="+memorySize+" Bytes");
   }
}

Create and Build a Maven Project in Codenvy

Creating and building project with Maven in Codenvy is very simple. The new IDE interface provides some easy steps that helps you to build your POM file in a very easy manner.

Go to workspace and create project




Create a maven project
Give a name to project , description is optional.




Fill these entries as per your requirement. 
For demo purpose we are just leaving them blank.




After the project is created you can see this in your project explorer .




Build project




You can verify the build message in the console






Apache HttpClient example with Maven [JAVA]

Step one.
<dependencies>
 <dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.2</version>
</dependency>
  </dependencies>

Update pom.xml with following entry



Step two

Execute following code


import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

public class Main 
{

    public static void main(String[] args) 
    {
        try
        {
        String url = "http://wikipedia.org";

        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(url);


        request.addHeader("User-Agent", "USER_AGENT");
        HttpResponse response = client.execute(request);

            System.out.println("Response Code : "
                + response.getStatusLine().getStatusCode());
       }
       catch(Exception e)
       {
          System.out.println(e); 
       }
    }
}

Thursday, December 29, 2016

How to check if modules are enabled in php or not

To check if some particular modules such as PDO, XML, SQLLITE, FTP enabled in your PHP or not, we can use get_loaded_extensions() method.




<?php
echo "<pre>";
print_r(get_loaded_extensions());
?>



This will show you following modules are enabled


Array
(
    [0] => Core
    [1] => bcmath
    [2] => calendar
    [3] => ctype
    [4] => date
    [5] => filter
    [6] => hash
    [7] => iconv
    [8] => json
    [9] => mcrypt
    [10] => SPL
    [11] => pcre
    [12] => Reflection
    [13] => session
    [14] => standard
    [15] => mysqlnd
    [16] => tokenizer
    [17] => zip
    [18] => zlib
    [19] => libxml
    [20] => dom
    [21] => PDO
    [22] => bz2
    [23] => SimpleXML
    [24] => xml
    [25] => wddx
    [26] => xmlreader
    [27] => xmlwriter
    [28] => apache2handler
    [29] => openssl
    [30] => curl
    [31] => fileinfo
    [32] => gd
    [33] => gettext
    [34] => mbstring
    [35] => exif
    [36] => mysqli
    [37] => pdo_mysql
    [38] => pdo_sqlite
    [39] => Phar
    [40] => ftp
)


How to find apache version in PHP ?

apache_get_version method is used to fetch Apache version

<?php
$version 
apache_get_version();
echo 
"$version\n";?>

How to do a curl command in Javascript ?

To do a curl command in javascript you first need to understand what does curl do -

CURL

CURL is basically used to transfer (send or receive ) data to server. CURL supports various protcols but for web its primarily HTTP.

A sample example of doing curl is


$ curl -v http://google.com                                                                                              
* Rebuilt URL to: http://google.com/                                                                                                        
* Hostname was NOT found in DNS cache                                                                                                       
*   Trying 216.58.217.174...                                                                                                                
* Connected to google.com (216.58.217.174) port 80 (#0)                                                                                     
> GET / HTTP/1.1                                                                                                                            
> User-Agent: curl/7.38.0                                                                                                                   
> Host: google.com                                                                                                                          
> Accept: */*                                                                                                                               
>                                                                                                                                           
< HTTP/1.1 301 Moved Permanently                                                                                                            
< Location: http://www.google.com/                                                                                                          
< Content-Type: text/html; charset=UTF-8                                                                                                    
< Date: Thu, 29 Dec 2016 15:13:42 GMT                                                                                                       
< Expires: Sat, 28 Jan 2017 15:13:42 GMT

How you do in Javascript

Ajax is the best method to do the same in javascript. An ajax call does the same thing as curl. You can do HTTP requests over ajax and receive appropriate response.

For that matter you can use JQUERY as well.


$.ajax({
  accepts: {
    mycustomtype: 'application/x-some-custom-type'
  },
 
  // Instructions for how to deserialize a `mycustomtype`
  converters: {
    'text mycustomtype': function(result) {
      // Do Stuff
      return newresult;
    }
  },
 
  // Expect a `mycustomtype` back from server
  dataType: 'mycustomtype'
});