Thursday, September 25, 2014

Adding MIME Types for Fonts in EC2 using Python



By Default EC2 does not include mime types for fonts except svg in  /etc/mime.types

So need to add them to the system

Below code example tells you how to do that in python

import mimetypes
..
mimetypes.init()
mimetypes.add_type('font/ttf', '.ttf')
mimetypes.add_type('font/opentype', '.otf')
mimetypes.add_type('application/font-woff', '.woff')
mimetypes.add_type('application/vnd.ms-fontobject', '.eot')

For more Information please visit https://docs.python.org/2/library/mimetypes.html

Thursday, September 11, 2014

CSV format from MySQL View



With the latest Technologies and Tools available its very easy to export data select from database to CSV format.

But most of them are not able to to configure as you want.

Here is a sample for MySql for Student Data from client_marks_view View


set @given_from:='2014-01-01';
set @given_to:='2014-06-01';

SELECT CONCAT(' Students Marks Summary for the period ',`given_from`(), ' - ',`given_to`()) AS `Students Marks Summary`
UNION ALL
SELECT '' AS ``
UNION ALL
SELECT CONCAT('"Student Name','",','"Total Attendance','",','"Total Marks','"') AS `CONCAT('"Student','",','"Resource Name','",','"Total Attendance','",','"Total Marks')`
UNION ALL
(SELECT CONCAT('"',ifnull(`a`.`studnet_name`,''),'","',ifnull(`a`.`total_attendance`,' '),'","$',round(ifnull(`a`.`total_marks`,0),2),'",') AS `CONCAT('"',
IFNULL(a.student_name, ""),
'","',
IFNULL(a.total_attendance," "),
'","',
IFNULL(a.total_marks, "")
ROUN` from `student_marks_view` `a`) ;


How to create a direct view from that

CREATE OR REPLACE VIEW students_view
AS

   SELECT CONCAT(' Student Marks Summary for the period ',`given_from`(), ' - ',`given_to`()), ',,,,,,'
     UNION ALL
   SELECT "", "" FROM DUAL
     UNION ALL
   SELECT CONCAT('"Student Name','",','"Total Attendance','",','"Total Marks'),'",,,,,,'
     UNION ALL
 
   (SELECT CONCAT('"',
IFNULL(a.student_name, ""),
'","',
IFNULL(a.total_attendance," "),
'","$',
ROUND(IFNULL(a.total_marks, 0),2),
'",'), ',,,,,,' 

FROM student_marks_view  AS a )

Wednesday, September 3, 2014

Basic Web Testing with Selenium WebDriver With Ruby 2.0.0



1. Install Node and Ruby on windows

D:\Selinium Tests\1>ruby -v
ruby 2.0.0p481 (2014-05-08) [x64-mingw32]

2. Install Selinium Web Driver 
gem install selenium-webdriver

If no web driver installed you will get bellow error on windows
C:/Ruby200-x64/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- selenium-webdriver (LoadError)
        from C:/Ruby200-x64/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from 1.rb:2:in `<main>'


3. Install Chrome & IEDriverServer Driver
Copy the chromedriver.exe to  C:/Ruby200-x64/bin or any classpath location
Copy the IEDriverServer.exe to   C:/Ruby200-x64/bin or any classpath location

4. Install HTTP server locally and create a folder name ruby under htdocs
5. Copy All the *.rb and *.html files to above folder in (4)
6. Execute each ruby file as 
>ruby xxx.rb

Download the example file from Examples

Wednesday, August 27, 2014

Using Cookie as RewriteRule variable in Apache ReverseProxy



If have a cookie named myServerName, and want to check for its availability and use its content for building a URL

Code:
RewriteCond %{REQUEST_URI} ^/
RewriteCond %{HTTP_COOKIE} ^.*myServerName=([^;]+)
RewriteRule /(.*) http://%1.test.com.au/$1 [P]


Re Write to get the content from AWS S3 Bucket RewriteEngine On RewriteCond %{HTTP_COOKIE} ^.*mycookie.*$ [NC] RequestHeader set Host 's3-ap-southeast-2.amazonaws.com' RewriteRule (.*)/files/(.*) http://s3-ap-southeast-2.amazonaws.com/mybucket/$2 [P] ProxyPassReverse /files/ http://s3-ap-southeast-2.amazonaws.com/mybucket/ #If no Cookies Found RewriteRule (.*)/files/(.*) http://s3-ap-southeast-2.amazonaws.com/mybucket//error_url/error.html [P] ProxyPassReverse /files/ http://s3-ap-southeast-2.amazonaws.com/mybucket/

Monday, August 4, 2014

Easy Table Sorting and Searching with JQuery Plugings



<link rel="stylesheet" href="........../themes/blue/style.css" />

<script type="text/javascript" src="...../jquery.tablesorter.min.js"></script>
<script type="text/javascript" src=".../jquery.searcher.min.js"></script>


            <input id="tablesearchinput" />
            <table id="student-list" class="tablesorter">
                <thead>
                    <tr>
                        <th class="id">Student Id</th>
                        <th class="name">Student Name</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>

$("#student-list").tablesorter();
$("#student-list").searcher({
inputSelector: "#tablesearchinput"
});


Thursday, June 26, 2014

Improved data source for Spring




Why and what about C3p0

c3p0 is an easy-to-use library for making traditional JDBC drivers "enterprise-ready" by augmenting them with functionality defined by the jdbc3 spec and the optional extensions to jdbc2. In particular, c3p0 provides several useful services:

Classes which adapt traditional DriverManager-based JDBC drivers to the newer javax.sql.DataSource scheme for acquiring database Connections.
Transparent pooling of Connection and PreparedStatements behind DataSources which can "wrap" around traditional drivers or arbitrary unpooled DataSources.
The library tries hard to get the details right:

c3p0 DataSources are both Referenceable and Serializable, and are thus suitable for binding to a wide-variety of JNDI-based naming services.
Statement and ResultSets are carefully cleaned up when pooled Connections and Statements are checked in, to prevent resource- exhaustion when clients use the lazy but common resource-management strategy of only cleaning up their Connections....
The library adopts the approach defined by the JDBC 2 and 3 specification (even where these conflict with the library author's preferences). DataSources are written in the JavaBean style, offering all the required and most of the optional properties (as well as some non-standard ones), and no-arg constructors. All JDBC-defined internal interfaces are implemented (ConnectionPoolDataSource, PooledConnection, ConnectionEvent-generating Connections, etc.) You can mix c3p0 classes with compliant third-party implementations (although not all c3p0 features will work with external implementations).
c3p0 now fully supports the JDBC4 specification.

c3p0 hopes to provide DataSource implementations more than suitable for use by high-volume "J2EE enterprise applications".


Change to maven
                  <dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>


Change to Spring config where data source is created

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="${LIBRARY_JDBC_CONNECTION_STRING}" />
<property name="user" value="${LIBRARY_DATASOURCE_USER_NAME}" />
<property name="password" value="${LIBRARY_DATASOURCE_USER_PASSWORD}" />

<property name="idleConnectionTestPeriod" value="${C3P0_POOL_IDLE_CONNECTION_TEST_PERIOD}"/> 
<property name="preferredTestQuery" value="select 1"/>

<!-- performance improvements and configuration so that you don't get above error from: http://javatech.org/2007/11/c3p0-connectionpool-configuration-rules-of-thumb/ -->
<property name="acquireIncrement" value="${C3P0_POOL_ACQUIRE_INCREMENT}"/>
<property name="maxIdleTime" value="${C3P0_POOL_MAX_IDLE_TIME}"/>
<property name="maxIdleTimeExcessConnections" value="${C3P0_POOL_MAX_IDLE_TIME_EXCESS_CONNECTIONS}"/>
<property name="maxPoolSize" value="${C3P0_POOL_MAX_POOL_SIZE}"/>
<property name="minPoolSize" value="${C3P0_POOL_MIN_POOL_SIZE}"/>
<property name="numHelperThreads" value="${C3P0_POOL_NUM_HELPER_THREADS}"/>
<property name="unreturnedConnectionTimeout" value="${C3P0_POOL_UNRETURNED_CONNECTION_TIMEOUT}"/>  
</bean>

Example  values

C3P0_POOL_ACQUIRE_INCREMENT : 1 
C3P0_POOL_IDLE_CONNECTION_TEST_PERIOD : 300 
C3P0_POOL_MAX_IDLE_TIME : 3600 
C3P0_POOL_MAX_IDLE_TIME_EXCESS_CONNECTIONS : 2 
C3P0_POOL_MAX_POOL_SIZE : 4 
C3P0_POOL_MIN_POOL_SIZE : 2 
C3P0_POOL_NUM_HELPER_THREADS :2 
C3P0_POOL_UNRETURNED_CONNECTION_TIMEOUT : 3600

References
http://www.mchange.com/projects/c3p0/


Thursday, June 19, 2014

Best way to Identifying integer value from String



Best way to Identifying integer value from String


if(stringVariable.trim().matches("^\\d*$")){
// true this is a integer
}

To Decimal number like 1.23

if(("1.23".matches("^\\d+\\.\\d{2}$")){
// this will be truw
}