tag:blogger.com,1999:blog-6733059079708915632024-03-21T17:40:05.921-07:00Dev TriggerUnknownnoreply@blogger.comBlogger51125tag:blogger.com,1999:blog-673305907970891563.post-80407827887572850112021-11-04T01:06:00.003-07:002021-11-04T01:10:51.108-07:00AWS Secret Manager sample code with golang aws-sdk-go-v2 SDK<p>AWS Secret Manager provides sample code. It is very good to use for quick start. </p><p>But, I see often, they provide sample code with old version of SDK. </p><p>In this time, I program with Golang. and the aws-sdk-go is old version sdk. </p><p>As soon as I pushe the code, I know, it will be alerted by source scanning software. </p><p>I had to modify the sample code. as below</p><p> </p>
<pre class="pretty_print">package aws
// Use this code snippet in your app.
// If you need more information about configurations or implementing the sample code, visit the AWS docs:
// https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/setting-up.html
import (
"context"
"encoding/base64"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
"github.com/aws/smithy-go"
)
func getSecret() {
secretName := "arn:aws:secretsmanager:eu-central-1:.... put your secretmanager ARN"
region := "eu-central-1"
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(region),
)
if err != nil {
// handle error
}
//Create a Secrets Manager client
svc := secretsmanager.NewFromConfig(cfg)
input := &secretsmanager.GetSecretValueInput{
SecretId: aws.String(secretName),
VersionStage: aws.String("AWSCURRENT"), // VersionStage defaults to AWSCURRENT if unspecified
}
// In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
// See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
result, err := svc.GetSecretValue(context.TODO(), input)
if err != nil {
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
code := apiErr.ErrorCode()
message := apiErr.ErrorMessage()
// handle error code
fmt.Println("error code: " + code + " message : " + message)
return
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
// Decrypts secret using the associated KMS CMK.
// Depending on whether the secret is a string or binary, one of these fields will be populated.
var secretString, decodedBinarySecret string
if result.SecretString != nil {
secretString = *result.SecretString
} else {
decodedBinarySecretBytes := make([]byte, base64.StdEncoding.DecodedLen(len(result.SecretBinary)))
len, err := base64.StdEncoding.Decode(decodedBinarySecretBytes, result.SecretBinary)
if err != nil {
fmt.Println("Base64 Decode Error:", err)
return
}
decodedBinarySecret = string(decodedBinarySecretBytes[:len])
}
// Your code goes here.
fmt.Println(secretString)
fmt.Println(decodedBinarySecret)
}
</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-21819767604598608732018-12-21T05:10:00.002-08:002018-12-21T05:17:16.465-08:00maven with NTLM Proxy ServerWhile I am migrating my project to Java 11, I was facing many issues. I was not sure if the 3rd Party Framesworks work fine. I had a doubt.
So I tested several Frameworks. So far, Eclipslink and Jmokit was not running fine on Java11. I have reported to Jmokit.
Eclipselink has apparently fixed something in the latest code, but the latest version is not released yet. It is laid in Snapshot repository. I am using Company Nexus Repository which I am not allow to change the configuration for adding Snapshot Repository. To access the Repository, I had to configure Proxy in maven setting.xml. But, I was not able to access the Repository yet. I realized the Proxy Server is required NTML based Authentication. Maven doesn't support NTLM Proxy Authentication. It required another way to solve. <br/>
<br/>
I found out that either use CNTLM or wagon-http-lightweight. CNTLM would be good for various reson. If you use several application behind Proxy, like a VirtualBox, Git, Yum, SVN, ...
But, I just need for a maven. so I have chosen wagon-http-lightweight. <br/>
<br/>
It is fairly easy to use this. You can download the file from <a href="https://mvnrepository.com/artifact/org.apache.maven.wagon/wagon-http-lightweight/2.2">https://mvnrepository.com/artifact/org.apache.maven.wagon/wagon-http-lightweight/2.2</a> This is just 15 Kbytes. locate the dowonloaded file under M2_HOME/lib/ext<br/>
Now, You need to configure the maven xml files. You can use the password as plaintext in the configuration. But, I recommend to use encrypted password for the security. maven will recognize automatically when the password starts with "{" and will decrypt it before authenticate with Proxy Server. I will create a master password and user password by maven. masterpassword will be used for decrypting userpassword with in maven. userpassword is your NTLM userpassword<br/>
<br/>
USER_HOME/.m2/settings-security.xml
<br/>
<pre class="pretty_print">
mvn -emp masterpassword
{SPg1nt21S2MHuw0Hy8MJaEF7Gc7dK25UWGDYKHupNCw=}
<settingsSecurity>
<master>{SPg1nt21S2MHuw0Hy8MJaEF7Gc7dK25UWGDYKHupNCw=}</master>
</settingsSecurity>
</pre><br/>
<br/>
Set up your proxy in USER_HOME/.m2/setting.xml<br/>
<br/>
<pre class="pretty_print">
mvn -ep userpassword
{7ut6v4FFiJMHtwsmYrsmLMcPoDBGmbz/kgcQ6Vks+/0=}
<proxies>
<proxy>
<id>internet-proxy</id>
<active>true</active>
<protocol>http</protocol>
<host>###ProxyHost###</host>
<username>###Username###</username>
<password>{7ut6v4FFiJMHtwsmYrsmLMcPoDBGmbz/kgcQ6Vks+/0=}</password>
<port>###Proxy Port###</port>
<nonProxyHosts>localhost|127.0.0.1</nonProxyHosts>
</proxy>
</proxies> </pre>
<br/>
<br/>
PS. Security is good. but it makes develpers crazy. I hope there is a simple way to secure and easy to devlop without configure extra. it takes so much time to configure and get it.
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-54337111852745421552017-08-10T08:25:00.001-07:002017-08-10T08:25:16.396-07:00Curl Command example with client Key to https Server
To communicate with https Server by curl command, if a client private key is required, it is not simple like as i thought.
my_client.12 is my client key and I need to convert to the other format.
Before starting, check your key.
<br/>
<pre class="pretty_print">
openssl pkcs12 -info -in my_client.12
</pre>
<br/>
<br/>
<br/>
Check the Server sertificate
<br/>
<pre class="pretty_print">
openssl s_client -showcerts -connect www.domain.com:443
</pre>
<br/>
<br/>
If you see that all right. you will be able to communicate with the https server which requires client key.
<br/>
<br/>
Convert the client key
<br/>
<pre class="pretty_print">
openssl pkcs12 -in my_client.12 -out client.pem -clcerts -nokeys
openssl pkcs12 -in my_client.12 -out key.pem -nocerts
openssl rsa -in key.pem -out newkey.pem
</pre>
<br/>
<br/>
And now finally, you can communicate with the server.
<br/>
<pre class="pretty_print">
curl -v -G -k --key newkey.pem --cert ./client.pem:password https://www.domain.com/path
</pre>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-44590922193013383592017-08-10T08:00:00.000-07:002017-08-10T08:00:21.586-07:00Download Files from AWS S3 Bucket via SQS message<br/>
<br/>
Our Infrastructure put a log file in S3 bucket. I need to get only this new Log file from the S3 bucket and parse it put into DB.
The Log parse Servers are clustered which might be inturruped while downloading and might download again which another cluster server is currently parsing the log file.
<br/>
There is a solution. When new files are created, the S3 Bucket will send a message to a Simple Queue Service(SQS).
The Log Parsing Servers will pull the messages every minute.
If a message already pulled, SQS will hide a message in determined period until the delete action. The delete action will be called from parsing server after downloading.
<br/>
To run the code, you will need a python3 or higher and boto3(Amazon Web Services (AWS) SDK for Python)
<br/>
<pre class="pretty_print">
yum install epel-release
yum install python34-pip
pip3 install boto3
</pre>
<br/>
<br/>
<br/>
There is a python3 code sample.
<br/>
<pre class="pretty_print">
import boto3
import json
import traceback
import os
import logging, sys
import tarfile
#logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
# Get the service resource
sqs = boto3.resource('sqs', region_name='eu-central-1')
s3_client = boto3.client('s3', region_name='eu-central-1')
# Get the queue
queue = sqs.get_queue_by_name(QueueName='Your SQS Queue Name')
for message in queue.receive_messages(MaxNumberOfMessages=10):
try:
if message is not None:
# Parsing event message from s3 bucket
s3 = json.loads(message.body)['Records'][0]['s3']
bucket = s3['bucket']['name']
key = s3['object']['key']
logging.debug('bucket :'+bucket)
logging.debug('key :'+key)
# Get filename and directory from key
filename = os.path.basename(key)
directory = '/your_prefix_dir/' + os.path.dirname(key)
logging.debug('filename :'+filename)
logging.debug('directory :'+directory)
# Create Directory if it is not exist
if not os.path.exists(directory):
os.makedirs(directory)
# Download Log File
s3_client.download_file(bucket, key, directory + filename)
logging.debug('Download completed')
# Extract tar.gz File
tar = tarfile.open(directory + filename, "r:gz")
tar.extractall(directory)
tar.close()
logging.debug('extract file completed')
# Remove tar.gz File
os.remove(directory + filename)
# Remove SQS message
message.delete()
except ValueError:
# SQS is dedicated for S3 event message. If there is wrong message from other service, leave message body and remove the message
logging.error('Message format is not valid. Delete message :' + message.body)
message.delete()
except Exception:
logging.error(traceback.format_exc())
else:
logging.info('finish')
</pre>
<br/>
<br/>
If you see message from the S3 bucket like below command. It is ready to pull the log file by the python script.
Watch out, the SQS hide the message default 20 second. it won't be visiable.
<pre class="pretty_print">
aws sqs receive-message --queue-url https://sqs.eu-central-1.amazonaws.com/your account number/queueName --max-number-of-messages 1 --region eu-central-1
</pre>
<br/>
you can excute the python script like below 20 seconds later. flock is for avoiding concurrent excution.
<pre class="pretty_print">
flock -n /home/your Dir/lock/s3-copy.lock -c "/usr/bin/python3 /your_prefix_dir/s3-copy.py"
</pre>
<br/>
The log file are downloaded and the tar.gz files are extracted.
<br/>
<br/>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-90266042409722345192017-08-03T09:20:00.000-07:002017-08-03T09:20:05.249-07:00Generate Unique Code for Coupons or Vouchers in javaPlease see the
Maven Dependency
<pre class="prettyprint">
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
</pre>
<br />
<br />
<br />
<pre class="prettyprint">import java.math.BigInteger;
public class Coupon {
private byte numberOfChar;
private BigInteger code;
private BigInteger crc;
public byte getNumberOfChar() {
return numberOfChar;
}
public void setNumberOfChar(byte numberOfChar) {
this.numberOfChar = numberOfChar;
}
public BigInteger getCode() {
return code;
}
public void setCode(BigInteger code) {
this.code = code;
}
public BigInteger getCrc() {
return crc;
}
public void setCrc(BigInteger crc) {
this.crc = crc;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Coupon)) {
return false;
}
Coupon c = (Coupon) o;
if (code.equals(c.code)) {
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 5;
hash = 89 * hash + (code != null ? code.hashCode() : 0);
hash = 89 * hash + (crc != null ? crc.hashCode() : 0);
return hash;
}
}
</pre>
<br />
<br />
<br />
<br />
<pre class="prettyprint">public class CouponGenerateError extends Exception {
public CouponGenerateError(String message) {
super(message);
}
private static final long serialVersionUID = -8232285755941178115L;
}
</pre>
<br />
<br />
<br />
<br />
<pre class="prettyprint">import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.LinkedHashSet;
import java.util.Set;
public class CouponGenerator {
private SecureRandom random = new SecureRandom();
/**
* Represent of number of CRC chracter. If it is 1. One Character of CRC code will be generated and attached next to
* coupon code
*/
static int CRC_SIZE = 1;
/**
* CRC Generator should never be changed unless you like to invalidate all previously published coupons. It is a key
* value to prove if the CRC value is right or not
*/
static int CRC_GENERATOR = 31;
/**
* @param numberOfChar
* is define how long is the Voucher code. Voucher code consists of coupon code and CRC code
* @param numberOfCoupon
* is define how many Vouchers must be generated
* @return
* @throws CouponGenerateError
*/
public Set<Coupon> getCouponSet(byte numberOfChar, long numberOfCoupon) throws CouponGenerateError {
isValidNumberOfChar(numberOfChar);
Set<Coupon> couponSet = new LinkedHashSet<Coupon>();
while (numberOfCoupon > couponSet.size()) {
couponSet.add(generateCoupon(numberOfChar));
}
return couponSet;
}
/**
* @param numberOfChar
* It must be bigger than 2 and smaller than 12. If the parameter is 2, it will have 32 cases of coupon
* code. It is meaning less to generate. <br/>
* If the parameter is bigger than 12. Internal long type variable can not calculate it. It will make a
* byte overflow and return garbage value. Because, Long is 8 byte which is 64 bits. Each coupon chracter
* takes 5 bit. 13 Chracters will take 65 bits
* @throws CouponGenerateError
*/
private void isValidNumberOfChar(byte numberOfChar) throws CouponGenerateError {
if (numberOfChar < 3 || numberOfChar > 12) {
throw new CouponGenerateError(
"Invalid numberOfChar for Coupon chracters. It must be bigger than 2 and smaller than 12");
}
}
/**
* @param numberOfChar
* length of Alphanumeric code value is defined by numberOfChar
* @param numberOfChar
* must be bigger than 3 or equal and smaller than 12 or equal chracters
* @return
* @throws CouponGenerateError
*/
public Coupon getCoupon(byte numberOfChar) throws CouponGenerateError {
isValidNumberOfChar(numberOfChar);
return generateCoupon(numberOfChar);
}
private Coupon generateCoupon(byte numberOfChar) throws CouponGenerateError {
Coupon coupon = new Coupon();
coupon.setNumberOfChar(numberOfChar);
// Create 5 random bits per character. 5 bits represent Base32 Encoding
coupon.setCode(new BigInteger((numberOfChar - CRC_SIZE) * 5, random));
coupon.setCrc(CouponUtil.calculateCrc(coupon));
return coupon;
}
}
</pre>
<br />
<br />
<br />
<pre class="prettyprint">import java.math.BigInteger;
import org.apache.commons.lang.StringUtils;
public class CouponUtil {
/**
* @param coupon
*
* 7. Base 32 Encoding with Extended Hex Alphabet
*
* The following description of base 32 is derived from [7]. This
* encoding may be referred to as "base32hex". This encoding should not
* be regarded as the same as the "base32" encoding and should not be
* referred to as only "base32". This encoding is used by, e.g.,
* NextSECure3 (NSEC3) [10].
*
* One property with this alphabet, which the base64 and base32
* alphabets lack, is that encoded data maintains its sort order when
* the encoded data is compared bit-wise.
*
* This encoding is identical to the previous one, except for the
* alphabet. The new alphabet is found in Table 4.
*
* Table 4: The "Extended Hex" Base 32 Alphabet
*
* Value Encoding Value Encoding Value Encoding Value Encoding
* 0 0 9 9 18 I 27 R
* 1 1 10 A 19 J 28 S
* 2 2 11 B 20 K 29 T
* 3 3 12 C 21 L 30 U
* 4 4 13 D 22 M 31 V
* 5 5 14 E 23 N
* 6 6 15 F 24 O (pad) =
* 7 7 16 G 25 P
* 8 8 17 H 26 Q
*
* @return
*/
public static String getVoucherString(Coupon coupon) {
byte numberOfChar = coupon.getNumberOfChar();
String code = coupon.getCode().toString(32);
if (!isCodeSizeRight(numberOfChar, code)) {
code = leftPadding0(numberOfChar, code);
}
// Debug purpose
// System.out.println("code : " + code);
// System.out.println("bit code : " + coupon.getCode().toString(2));
// System.out.println("CRC : " + coupon.getCrc().toString(32));
return code + coupon.getCrc().toString(32);
}
private static String leftPadding0(byte numberOfChar, String code) {
return StringUtils.repeat("0", numberOfChar - CouponGenerator.CRC_SIZE).substring(
code.length() % numberOfChar)
+ code;
}
private static boolean isCodeSizeRight(byte numberOfChar, String code) {
return code.length() % (numberOfChar - CouponGenerator.CRC_SIZE) == 0;
}
/**
* It validate the CRC value from the coupon.
*
* @param coupon
* @return
*/
static boolean isValidCrc(Coupon coupon) {
if (coupon.getCrc().equals(calculateCrc(coupon))) {
return true;
} else {
return false;
}
}
/**
* It cancluate CRC value and return it. coupon parameter must be set code value to calculate CRC value.
*
* @param coupon
* @return
*/
static BigInteger calculateCrc(Coupon coupon) {
BigInteger code = coupon.getCode();
String crcValue = String.valueOf(code.longValue() % CouponGenerator.CRC_GENERATOR);
// Debug purpose
// System.out.println("code.toString(2) : " + code.toString(2));
// System.out.println("code.longValue() : " + code.longValue());
// System.out.println("crcValue : " + crcValue);
return new BigInteger(crcValue, 10);
}
}
</pre>
<br />
<br />
<br />
<br />
<pre class="prettyprint">import java.math.BigInteger;
import java.util.Set;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class CouponGeneratorTest {
static CouponGenerator couponGenerator = new CouponGenerator();
static Set<Coupon> couponSet;
@BeforeClass
public static void init() throws CouponGenerateError {
couponSet = couponGenerator.getCouponSet((byte) 10, 1000);
}
/**
* Test if the CouponGenerator generate requested number of coupons
*
* @throws Exception
*/
@Test
public void testCouponCount() throws Exception {
Assert.assertEquals("Coupon creation didn't match the requested number of coupon", 1000, couponSet.size());
}
/**
* Test if the CodeGenerator generate exact size of vaucher code chracter size.
*
* @throws Exception
*/
@Test
public void testCouponCharacterSize() throws Exception {
for (Coupon coupon : couponSet) {
// System.out.println(CouponUtil.getVoucherString(coupon));
Assert.assertEquals("Coupon Characters didn't match the requested number of character", 10, CouponUtil
.getVoucherString(coupon).length());
}
}
/**
* Test if the generated CRC value passes validation check. <br/>
* If it fails, one of generation or validation is wrong
*
* @throws Exception
*/
@Test
public void testCrcCreation() throws Exception {
for (Coupon coupon : couponSet) {
Assert.assertTrue("Checksum value is not valid", CouponUtil.isValidCrc(coupon));
}
}
/**
* Test if the given coupon passes CRC validation check.
*
* @throws Exception
*/
@Test
public void testCrcValidation() throws Exception {
Coupon coupon = new Coupon();
coupon.setCode(new BigInteger("54321", 32));
// CRC value is 1. ex ) 54321 (32 radix) % 31 = 5377089 (10 radix) % 31 = 15 (F)
coupon.setCrc(new BigInteger("f", 32));
Assert.assertTrue("CRC value value is not valid", CouponUtil.isValidCrc(coupon));
}
/**
* Test if the given coupon fails CRC validation check.
*
* @throws Exception
*/
@Test
public void testCrcValidation2() throws Exception {
Coupon coupon = new Coupon();
coupon.setCode(new BigInteger("54321", 32));
// CRC value is 1. ex ) 54321 (32 radix) % 31 = 5377089 (10 radix) % 31 = 15 (F)
coupon.setCrc(new BigInteger("2", 32));
Assert.assertFalse("CRC value must not be valid", CouponUtil.isValidCrc(coupon));
}
/**
* Test if the CouponGenerator accept requesting less than 3 chracters or bigger than 12 chracters. In this case, it
* must throw Exception
*/
@Test
public void testCouponMinusCharacterSize() {
try {
couponGenerator.getCoupon((byte) 2);
Assert.fail("small charater size is not allowed");
} catch (CouponGenerateError e) {
Assert.assertTrue(true);
}
try {
couponGenerator.getCoupon((byte) -6);
Assert.fail("Minus charater size is not allowed");
} catch (CouponGenerateError e) {
Assert.assertTrue(true);
}
try {
couponGenerator.getCoupon((byte) 13);
Assert.fail("Bigger than 12 charater size is not allowed");
} catch (CouponGenerateError e) {
Assert.assertTrue(true);
}
}
}
</pre>
<br />
<br />Unknownnoreply@blogger.com3tag:blogger.com,1999:blog-673305907970891563.post-31345721756101619852016-02-16T07:23:00.001-08:002016-02-16T07:23:42.455-08:00Image Resizing by java, Change JPEG image qualityI have seen sample code from others to resize image and change jpeg quality by java. It was pretty good. but, just I didn't like one thing that most of them didn't close the stream of file reading or writing.
So, The image files were not able to removed or rewrited. I guess probably, it will cause to create jomby process which is never die unless restart JVM. Thease kind of things must be avoided to make a stable application.
It will not be visible for now. but, It will capture the application one day and make difficult to find out why application is not stable.
<br />
<br />
Add Maven Dependendy to resize image
<br />
<pre class="prettyprint"><dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
</dependency>
</pre>
<br />
<br />
<pre class="prettyprint">import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.FileImageOutputStream;
import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Method;
import org.imgscalr.Scalr.Mode;
...
public class ImageUtil {
public static void downsizeJpg(Path sourcePath, Path targetPath, int targetWidth) throws IOException {
try (InputStream isSrc = new FileInputStream(sourcePath.toFile())) {
// Don't Use "ImageIO.read(File file)". It will not close the stream
// Use Stream to close the resource
BufferedImage sourceImage = ImageIO.read(isSrc);
double width = sourceImage.getWidth();
double height = sourceImage.getHeight();
double ratio = width / height;
int trgHeight = (int) (targetWidth / ratio);
// If taget width is smaller than source width, start to downsize
if (targetWidth < width) {
BufferedImage targetImage = Scalr.resize(sourceImage, Method.QUALITY, Mode.FIT_EXACT, targetWidth,
trgHeight);
saveImage(targetImage, targetPath, 0.7f);
// prevent upsizing. just copy the source image to target image
} else {
saveImage(sourceImage, targetPath, 0.7f);
}
}
}
private static void saveImage(RenderedImage image, Path targetPath, float quality) throws FileNotFoundException,
IOException {
// set compression level
JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(quality);
final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
try (FileImageOutputStream fios = new FileImageOutputStream(targetPath.toFile())) {
// specifies where the jpg image has to be written
writer.setOutput(fios);
// save the image
writer.write(null, new IIOImage(image, null, null), jpegParams);
writer.dispose();
}
}
}
</pre>
<br />
quality can be fromo 0f to 1f. 1 is the highst quality and file size get big. 0 is lowest quality and file size get small.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-70884850587223965832015-05-28T08:59:00.001-07:002015-05-28T08:59:19.425-07:00Print current JVM's Java System Property
It print out all System Properties on console.
<pre class="prettyprint">
package com.devtrigger;
public class SystemProperty {
public static void main(String... args) {
System.getProperties().list(System.out);
}
}
</pre>
<br/><br/>
Then you pick up one property which you need and use it.<br/>
<pre class="prettyprint">
package com.devtrigger;
public class SystemProperty {
public static void main(String... args) {
System.out.println(System.getProperty("user.home"));
System.out.println(System.getProperty("file.separator"));
...
// create file object which is under user's home directory
File file = new File (System.getProperty("user.home") + System.getProperty("file.separator") + "setting.xml");
}
}
</pre>
<br/><br/>
Referred from : <a href="https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html">System Properties</a>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-23680749335864036112015-05-20T08:44:00.000-07:002015-05-20T08:44:11.682-07:00a Java Class which scans ports in an IP
I had to run port scanner software for security check reason, before deliver the product. I tried to get one utilillity from internet. but, the Websites were blocked in my company to download.
so, I just made a Java Class which can do the port scanning. it takes around 20 min to run throught all the ports. If I increase Thread size and shorten the timeout, it will take less then 5 min.
<br><br>
Fornow, I have a little problem. When I let it run quickly, Firewall or security software get activated and deny to response if the port is opened.
So, I had to let it run slowly to find all the opened port.<br>
<br>
<pre class="prettyprint">
package com.devtrigger;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
class PortScanner {
public static void main(final String... args) throws InterruptedException, ExecutionException {
final ExecutorService es = Executors.newFixedThreadPool(15);
final String ip = "127.0.0.1";
final int timeout = 200;
final List<Future<ScanResult>> futures = new ArrayList<>();
for (int port = 1; port <= 65535; port++) {
futures.add(portIsOpen(es, ip, port, timeout));
}
es.awaitTermination(200L, TimeUnit.MILLISECONDS);
int openPorts = 0;
for (final Future<ScanResult> f : futures) {
if (f.get().isOpen()) {
openPorts++;
System.out.println(f.get().getPort());
}
}
System.out.println("There are " + openPorts + " open ports on host " + ip + " (probed with a timeout of "
+ timeout + "ms)");
}
public static Future<ScanResult> portIsOpen(final ExecutorService es, final String ip, final int port,
final int timeout) {
return es.submit(new Callable<ScanResult>() {
@Override
public ScanResult call() {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), timeout);
socket.close();
return new ScanResult(port, true);
} catch (Exception ex) {
return new ScanResult(port, false);
}
}
});
}
public static class ScanResult {
private int port;
private boolean isOpen;
public ScanResult(int port, boolean isOpen) {
super();
this.port = port;
this.isOpen = isOpen;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public boolean isOpen() {
return isOpen;
}
public void setOpen(boolean isOpen) {
this.isOpen = isOpen;
}
}
}
</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-56821720919776948592015-02-06T01:16:00.001-08:002015-02-06T01:16:17.754-08:00How to create and run Apache JMeter Test Scripts from a Java program
<pre clas="prettyprint">
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myportal</groupId>
<artifactId>loadtest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Zug Portal Load Test Tool</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_http</artifactId>
<version>2.11</version>
</dependency>
</dependencies>
</project>
</pre>
<pre clas="prettyprint">
package myportal.loadtest;
import java.net.URL;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.SetupThreadGroup;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
public class SampleJmeter {
public static void main(String[] args) {
// Engine
StandardJMeterEngine jm = new StandardJMeterEngine();
URL property = SampleJmeter.class.getClassLoader().getResource("jmeter.properties");
// jmeter.properties
JMeterUtils.loadJMeterProperties(property.getPath());
HashTree hashTree = new HashTree();
// HTTP Sampler
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain("www.google.com");
httpSampler.setPort(80);
httpSampler.setPath("/");
httpSampler.setMethod("GET");
// Loop Controller
TestElement loopCtrl = new LoopController();
((LoopController) loopCtrl).setLoops(1);
((LoopController) loopCtrl).addTestElement(httpSampler);
((LoopController) loopCtrl).setFirst(true);
// Thread Group
SetupThreadGroup threadGroup = new SetupThreadGroup();
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController((LoopController) loopCtrl);
// Test plan
TestPlan testPlan = new TestPlan("MY TEST PLAN");
hashTree.add("testPlan", testPlan);
hashTree.add("loopCtrl", loopCtrl);
hashTree.add("threadGroup", threadGroup);
hashTree.add("httpSampler", httpSampler);
jm.configure(hashTree);
jm.run();
}
}
</pre>
meter.properties file is from the JMeter installation /bin directory.
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-41143112835908962722014-05-05T03:36:00.003-07:002014-05-05T03:36:47.044-07:00Android infrared (IR) transmitter code sample for Kitkat and Jelly Bean
There are many remote controller application in Google play store. But, I was not able to find an app that I can put my own IR pattern.
For some reason, I need to transmit my own pattern.<br/>
<br/>
So, I made a remote controller application. Now I can reach my purpose.
It was easy to make for Kitkat OS. but, I wanted to make it run for Jelly Bean OS.
Despite of Android API officially offer IR API from Kitkat(4.4). apparently, I see few Samsung Android device has IR transmitter which is running Jelly Bean OS(4.2).
<br/>
<br/>
When I run the same app which is using IR API. It didn't run on Jelly Bean as expected.
I was wonder how I can control the IR from Jelly Bean OS. Luckily, I found a code sample from https://github.com/rngtng/IrDude thanks to him.<br/>
<br/>
There are list of manufactur IR pattern. Please refer http://www.remotecentral.com/cgi-bin/codes/<br/>
<br/>
<br/>
\AndroidManifest.xml<br/>
Just make sure that minSdkVersion is 17 and target version is 19. as well it needs a permission and feature to handle IR transmitter.<br/>
<pre class="prettyprint">
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.devtrigger.remotecontrol"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.TRANSMIT_IR"
android:required="false" />
<uses-feature android:name="android.hardware.consumerir" />
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.devtrigger.remotecontrol.MainActivity"
android:screenOrientation="portrait"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
</pre><br/>
<br/>
<br/>
\res\layout\activity_main.xml<br/>
There are just power , Channel up and down button.<br/>
<pre class="prettyprint">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonPower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="irSend"
android:text="Power" />
<Button
android:id="@+id/buttonChUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="irSend"
android:text="CH +" />
<Button
android:id="@+id/buttonChDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="irSend"
android:text="CH -" />
</LinearLayout>
</pre><br/>
<br/>
\src\com\devtrigger\remotecontrol\MainActivity.java<br/>
I just removed my own pattern to generalize it and share it on my blog. I put Samsung TV IR pattern on below sample and this Class works find for my Samsung Android device. I have test Galaxy S4 mini Jelly Bean and Galaxy S4 Kitkat. <br/>
<pre class="prettyprint">
package com.devtrigger.remotecontrol;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.hardware.ConsumerIrManager;
import android.os.Build;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
Object irdaService;
Method irWrite;
SparseArray<String> irData;
TextView mFreqsText;
ConsumerIrManager mCIR;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
irData = new SparseArray<String>();
irData.put(
R.id.buttonPower,
hex2dec("0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
irData.put(
R.id.buttonChUp,
hex2dec("0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 003f 0015 003f 0015 0015 0015 0040 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
irData.put(
R.id.buttonChDown,
hex2dec("0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 0015 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
irInit4KitKat();
}else{
irInit4JellyBean();
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public void irInit4KitKat() {
// Get a reference to the ConsumerIrManager
mCIR = (ConsumerIrManager)getSystemService(Context.CONSUMER_IR_SERVICE);
}
public void irInit4JellyBean() {
irdaService = this.getSystemService("irda");
Class c = irdaService.getClass();
Class p[] = { String.class };
try {
irWrite = c.getMethod("write_irsend", p);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public void irSend(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
irSend4Kitkat(view);
}else{
irSend4JellyBean(view);
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private void irSend4Kitkat(View view) {
String data = irData.get(view.getId());
if (data != null) {
String values[] = data.split(",");
int[] pattern = new int[values.length-1];
for (int i=0; i<pattern.length; i++){
pattern[i] = Integer.parseInt(values[i+1]);
}
mCIR.transmit(Integer.parseInt(values[0]), pattern);
}
}
private void irSend4JellyBean(View view) {
String data = irData.get(view.getId());
if (data != null) {
try {
irWrite.invoke(irdaService, data);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
protected String hex2dec(String irData) {
List<String> list = new ArrayList<String>(Arrays.asList(irData
.split(" ")));
list.remove(0); // dummy
int frequency = Integer.parseInt(list.remove(0), 16); // frequency
list.remove(0); // seq1
list.remove(0); // seq2
for (int i = 0; i < list.size(); i++) {
list.set(i, Integer.toString(Integer.parseInt(list.get(i), 16)));
}
frequency = (int) (1000000 / (frequency * 0.241246));
list.add(0, Integer.toString(frequency));
irData = "";
for (String s : list) {
irData += s + ",";
}
return irData;
}
}
</pre>Unknownnoreply@blogger.com12tag:blogger.com,1999:blog-673305907970891563.post-18507642068660896182014-03-14T03:49:00.001-07:002014-03-14T03:49:14.433-07:00Reversed Binary Numbers (Difficulty Level: Easy)My friend sent me a quiz how to make a reverse binary from input number and put it back as number.<br />
It is easy level though, I had fun of it <br />
If you are interested to get more quiz, please check this https://code.google.com/codejam/ <br />
There are many programing quiz in Internet. I see there are so many genius in the world. <br />
I wonder when I can resolve the difficult level of quiz. I must study math again.<br />
<br />
<br />
<pre>Task
Your task will be to write a program for reversing numbers in binary. For instance, the binary representation of 13 is 1101, and reversing it gives 1011, which corresponds to number 11.
Input
The input contains a single line with an integer N, 1 ≤ N ≤ 1000000000.
Output
Output one line with one integer, the number we get by reversing the binary representation of N.
Sample input 1
13
Sample output 1
11
Sample input 2
47
Sample output 2
61
</pre>
<br />
<br />
<br />
<br />
<pre class="prettyprint">package puzzle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author jack
*/
public class Reversebinary {
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
while ((input = br.readLine()) != null) {
int inputValue = Integer.parseInt(input);
// System.out.println(Integer.toBinaryString(inputValue));
StringBuffer revertedString = new StringBuffer();
revertedString.append(Integer.toBinaryString(inputValue)).reverse();
// System.out.println(revertedString);
System.out.println(Long.parseLong(revertedString.toString(), 2));
}
} catch (java.lang.NumberFormatException ne) {
System.out.println("Please type numbers");
} catch (IOException io) {
io.printStackTrace();
}
}
}
</pre>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-40067623204545798272014-03-14T03:37:00.004-07:002014-03-14T03:37:53.360-07:00How to get the apk out of the non-rooted Android device
I got a request to test updated Android application. but, it often happens that I need to test without apk file. <br />
Due to complicated commpany rules, politics and security, the requester can't send me the apk file.. But, they push me to do.. <br />
To resolve this situation, luckily I had one Android device which is installed the latest apk. but, I need to test it on another device.<br />
I can extract the apk from an Anroid device and install it on the other devices.<br />
<br />
To do this step, Android SDK is required. so that you will be able use 'adb' command.<br />
<br />
Step to extract apk file from an Android device.<br />
1. Connect the device to the computer <br />
2. start command prompt or shell <br />
3. adb shell pm list package -f -3 (to display all the installed apps on the device) <br />
<pre class="prettyprint">C:\>adb shell pm list package -f -3
package:/data/app/autotechniksteeg.preistafel-1.apk=autotechniksteeg.preistafel
...
package:/data/app/uk.amazon.mShop.android-2.apk=uk.amazon.mShop.android
...
</pre><br />
4. adb pull {apk name} (This will save the required apk on the current directory of the computer) <br />
<pre class="prettyprint">C:\>adb pull /data/app/uk.amazon.mShop.android-2.apk
</pre><br />
<br />
If you remove '-3' option, it will show all apk which is pre-loaded app on your device. I used 'adb shell pm list package -f -3' command. <br/>
If you like to know more about 'pm list package' filter option, please refer below. <br />
<pre class="prettyprint">pm list packages: prints all packages, optionally only
those whose package name contains the text in FILTER. Options:
-f: see their associated file.
-d: filter to only show disbled packages.
-e: filter to only show enabled packages.
-s: filter to only show system packages.
-3: filter to only show third party packages.
-i: see the installer for the packages.
-u: also include uninstalled packages.
</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-4090112128535413502013-10-24T03:12:00.001-07:002013-10-24T03:12:17.356-07:00receive argument on expect shell script
I just wanted to scp easily. I just simply made a expect shell script which receive one argument.<br/>
I just excute one script like below <br/>
<br/>
<pre class="prettyprint">
cp.sh file
</pre>
<br/>
and copy finished
<br/>
<pre class="prettyprint">
#!/usr/bin/expect -f
set filename [lindex $argv 0];
spawn scp -v /temp/$filename root@192.168.1.1:/tmp/root/
expect "Enter passphrase"
send "\r"
...
</pre>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-78983994205410207082013-08-08T00:53:00.001-07:002013-08-08T00:53:35.348-07:00Manipulate Access Control List (ACL) on mysql
I had to limit of access to mysql server due to security improvement task.<br />
We like to allow only few host to the mysql. It was very easy <br />
<br />
<pre class="prettyprint">
mysql>use mysql
mysql> select host, user from user;
----------------------------+
host user
----------------------------+
% dmitry
host : % means all (It is security breach)
host side should be ip address of web server
or
ip address of admin pc.
example)
-- Replace unlimited access rule to only allow 192.168.0.5 host rule
mysql>update user set host='192.168.0.5' where host = '%' and user in ('dmitry');
mysql>commit;
mysql>flush privileges;
Query OK, 0 rows affected (0.01 sec)
-- Remove unlimited access rule
mysql> delete from user where host = '%';
mysql> commit;
mysql> flush privileges;
</pre>
<br />
you can insert more hosts if you like to.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-37018381409115387272013-07-29T07:47:00.001-07:002013-07-29T07:47:27.534-07:00Android Json file read instead of property file read<br />
While I am building an Android application. I wanted to read a property file and wanted to act my app depends on the property file configuration.
But, property file can't handle array unless I add dependency Apache Common Configuration. As I am building an mobile application, it should be compact.
for the reason, I just used Android JSONObject to resolve this.<br />
<br />
Create "country.json" file and locate it Android device "/" directory<br />
<br />
<pre class="prettyprint">
{
"country": [
{
"name": "germany",
"code": "DE",
"continent": "Europe",
"eat": "sausage"
},
{
"name": "france",
"code": "FR",
"continent": "Europe",
"eat": "croissant"
},
{
"name": "korea",
"code": "KR",
"continent": "ASIA",
"eat": "rice"
},
{
"name": "japan",
"code": "JP",
"continent": "ASIA",
"eat": "fish"
}
]
}
</pre>
<br />
<br />
Create 2 method and use "getCountryList()" on your need.
<br />
<pre class="prettyprint">
private List<Country> getCountryList() throws Exception {
File dirSDCard = Environment.getExternalStorageDirectory();
File yourFile = new File(dirSDCard, "country.json");
InputStream jsonStream = new FileInputStream(yourFile);
JSONObject jsonObject = new JSONObject(InputStreamToString(jsonStream));
JSONArray jsonArray = jsonObject.getJSONArray("country");
List<Country> countryList = new ArrayList<Country>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonCountry = jsonArray.getJSONObject(i);
Country country = new Country();
country.setName(jsoncountry.getString("name"));
country.setCode(jsoncountry.getString("code"));
country.setContinent(jsoncountry.getString("continent"));
country.setEat(jsoncountry.getString("eat"));
countryList.add(country);
}
return countryList;
}
private String InputStreamToString(InputStream is) {
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String line;
try {
while ((line = r.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return total.toString();
}
</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-15799317403808373632013-06-17T11:14:00.000-07:002013-06-21T03:10:38.647-07:00mybatis various datasource sample jndi, hsql, oracle, mysql, sqlite configuration setup with Springframework
While doing various projects, I had to use different DBs. I just summarized datasource for Mybatis samples which is after testing. You can simply comment out to for your prefer datasource.<br/>
If you like to know detail to implement this sample with Spring framework, please follow <a href="/2012/12/mybatis-integration-with-spring-31.html">mybatis-integration-with-spring</a>.<br />
<br />
<pre class="prettyprint">
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<!-- JNDI datasource -->
<!--
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/oraclePool" />
<property name="resourceRef" value="true" />
</bean>
-->
<!-- Hsql datasource -->
<!--
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:hsql/schema.sql" />
<jdbc:script location="classpath:hsql/data.sql" />
</jdbc:embedded-database>
-->
<!-- Oracle JDBC datasource -->
<!--
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@HOST:1111:ORA" />
<property name="username" value="ID" />
<property name="password" value="PASSWORD" />
</bean>
-->
<!-- mysql JDBC datasource -->
<!--
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydata" />
<property name="username" value="user123" />
<property name="password" value="12345678" />
</bean>
-->
<!-- sqlite JDBC datasource -->
<!--
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="org.sqlite.JDBC" />
<property name="url" value="jdbc:sqlite:C:/yourdb.sqlite" />
</bean>
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.devtrigger.model" />
<property name="mapperLocations" value="classpath*:dao/**/*.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- scan for mapper interface files and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.devtrigger.dao" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
</pre>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-78784830601074256382013-03-14T09:50:00.001-07:002013-03-14T09:50:58.702-07:00Useful SVN adminitrator command
Export svn <br/>
- When I don't know SVN credential. but I have access to repository server<br/>
<pre class="prettyprint">
$svn export --force file:///home/svn/myrepos ./myrepos
</pre><br/>
<br/>
Check User access control <br/>
<pre class="prettyprint">
$cat /home/svn/myrepos/conf/authz
$cat /home/svn/myrepos/conf/passwd
$cat /home/svn/myrepos/conf/svnserve.conf
</pre><br/>
<br/>
Create version 1.6 compatible Repository<br/>
<pre class="prettyprint">
$svnadmin create myrepos --pre-1.6-compatible
</pre><br/>
<br/>
Dump svn repository <br/>
<pre class="prettyprint">
$svnadmin dump /home/svn/myrepos > /home/backup/svn/myrepos.dump
</pre><br/>
<br/>
Incremental dump svn repository <br/>
<pre class="prettyprint">
- SVN incremental Backup
$ svnadmin dump myrepos --revision 0:1000 > dumpfile1
$ svnadmin dump myrepos --revision 1001:2000 --incremental > dumpfile2
$ svnadmin dump myrepos --revision 2001:3000 --incremental > dumpfile3
$ svnadmin dump <repos> -r 58:HEAD --deltify > <file2>
</pre><br/>
<br/>
Import dump file<br/>
<pre class="prettyprint">
$ cd /home/svn
$ svnadmin load --bypass-prop-validation myrepos < /home/backup/svn/myrepos.dump
</pre><br/>
<br/>
Incremental import dump file<br/>
<pre class="prettyprint">
- SVN recovery
* incremental :
$ svnadmin load < ~/repos-0-1000.svn_dump
$ svnadmin load < ~/repos-1000-2000.svn_dump
$ svnadmin load < ~/repos-2000-3000.svn_dump
</pre><br/>
<br/>
Kill SVN daemon on Solaris<br/>
<pre class="prettyprint">
$ pkill -KILL svnserve
</pre><br/>
<br/>
Start SVN daemon
<pre class="prettyprint">
$ svnserve -d -r /home/svn/
</pre><br/>
<br/>
Pack svn
<pre class="prettyprint">
- SVN revision pack per 1000 revision
$ svnadmin pack /home/svn/sfc
</pre><br/>
<br/>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-73012962384994549282012-12-11T09:14:00.001-08:002012-12-12T00:25:33.391-08:00Mybatis integration with Spring 3.1 Framework.Hi All,<br />
<br />
I have configured new web project with mybatis and springframework several times. due to several project and being asked from team member to set up another project springframework skeleton.
There are many advantage to start up from scratch. then you will know what it all means.
But, When you need a quick start up, it would be good to have a project template.<br />
<br />
As I have configured it several times. I improved of understanding of these framework setting then I made simple optimized web project template for share with others.<br />
<br />
This sample will show the result of Json String.<br />
<br />
I recommend you to look through below source roughly and download attached sample source to test running and understanding. it will run fine on tomcat I have tested. you will not need any db connection for sample code. below sample is running based on memory DB Hsql.<br />
<br />
Sample Source is attached.<br />
you can download from below link<br />
<a href="http://mycode-lab.googlecode.com/files/web-template.zip">Download</a><br />
<br />
To integrate mybatis with Spring framework I recommend you to follow below step :<br />
<br />
1. create java web project.<br />
2. get all dependencies and configure classpath ( I am going to use maven )<br />
2. configure springframework.<br />
3. configure mybatis<br />
4. create classes and xml ( Mapper interface, Mapper xml, Spring controller, create beans)<br />
5. run it on tomcat or any WAS<br />
6. check result<br />
<br />
<br />
Directory Structure, you can refer below<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUkblM9tBFrUW75KmKdnLVW15Un8AqgVcNJ7wKuFjieAVpfGxFtw0xZKGqr_0i5xW-u5mAngmlmKLUMknuKbKWSywv7vJ16fJikEcWXN_MQaSQ5SPkCjaic-Ji62Wn-Lne0cEj_txRLos-/s1600/Screenshot+from+2012-12-11+16%253A56%253A54.png" imageanchor="1" style="margin-left:1em; margin-right:1em"><img border="0" height="400" width="188" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUkblM9tBFrUW75KmKdnLVW15Un8AqgVcNJ7wKuFjieAVpfGxFtw0xZKGqr_0i5xW-u5mAngmlmKLUMknuKbKWSywv7vJ16fJikEcWXN_MQaSQ5SPkCjaic-Ji62Wn-Lne0cEj_txRLos-/s400/Screenshot+from+2012-12-11+16%253A56%253A54.png" /></a></div><br />
<br />
<br />
Dependency Overview :<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiYVJZGBYjmkQQQ13kzngWOvt6i2ROCRImOV6d6ybFAFHVYam4E4b5efeLDqf0_jSQj-XTUVt8v2FUx9s8NzrSrgq-GGJT1BnVkAX_xfUzTBSniI70Ggt0uCW0cX_oJ5kKXR0LPLv4c7Bh/s1600/overview.png" imageanchor="1" style="margin-left:1em; margin-right:1em"><img border="0" height="365" width="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiYVJZGBYjmkQQQ13kzngWOvt6i2ROCRImOV6d6ybFAFHVYam4E4b5efeLDqf0_jSQj-XTUVt8v2FUx9s8NzrSrgq-GGJT1BnVkAX_xfUzTBSniI70Ggt0uCW0cX_oJ5kKXR0LPLv4c7Bh/s400/overview.png" /></a></div><br />
<br />
<br />
Maven Dependency : pom.xml<br />
Springframework 3.1.3, Jackson Mapper 1.9.11, Hsql 2.2.8 <br >
I have excluded mybatis spring dependency which depends on old verion of springframework<br />
<br />
<pre class="prettyprint">
<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>com.devtrigger</groupId>
<artifactId>web-template</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Web Template Project, you can copy this and use for new project set up quickly</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-version>3.1.3.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<additionalProjectFacets>
<jst.web>2.5</jst.web>
</additionalProjectFacets>
<downloadJavadocs>true</downloadJavadocs>
<downloadSources>true</downloadSources>
<wtpContextName>/</wtpContextName>
<wtpdefaultserver>${eclipse.wtpdefaultserver}</wtpdefaultserver>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>
</plugins>
</build>
</project>
</pre><br/>
<br/>
Mybatis configuration : mybatis-context.xml<br/>
If you comment out or in, you can change datasource.<br/>
I have prepared sample for JDNI, JDBC and Hsql datasources which are all tested.
Currently, It is running on Hsql for simple sample running on your memory based DB.
<pre class="prettyprint">
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<!-- JNDI datasource -->
<!--
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/oraclePool"/>
<property name="resourceRef" value="true" />
</bean>
-->
<!-- Hsql datasource -->
<!--
-->
<jdbc:embedded-database id="dataSource" >
<jdbc:script location="classpath:hsql/schema.sql"/>
<jdbc:script location="classpath:hsql/data.sql"/>
</jdbc:embedded-database>
<!-- JDBC datasource -->
<!--
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@HOST:1111:ORA" />
<property name="username" value="ID" />
<property name="password" value="PASSWORD" />
</bean>
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.devtrigger.model" />
<property name="mapperLocations" value="classpath*:dao/**/*.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- scan for mapper interface files and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.devtrigger.dao" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
</pre><br />
<br />
<br />
XML Mapper : JobMapper.xml<br />
You need to write your actual sql queries. value will be replace by "#{property}"<br/>
"<property name="typeAliasesPackage" value="com.devtrigger.model" />" is configured on mybatis-context.xml to make it short to write of bean name. You would need to write "com.devtrigger.model.JobInfo" for using a java bean or need to be used typeAlias.
Please aware that "jobInfo" is id of result map, "JobInfo" is a java bean.<br />
<pre class="prettyprint"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.devtrigger.dao.JobMapper">
<cache />
<resultMap type="JobInfo" id="jobInfo">
<result property="id" column="id" />
<result property="panNumber" column="pan_number" />
<result property="address" column="address" />
<result property="city" column="city" />
<result property="state" column="state" />
<result property="pincode" column="pincode" />
</resultMap>
<select id="selectAll" resultMap="jobInfo">
<![CDATA[
select id, pan_number, address, city, state, pincode
from JBT_MEM
]]>
</select>
<insert id="insert" parameterType="JobInfo">
<![CDATA[
insert into JBT_MEM values(#{id}, #{pan_number}, #{address}, #{city}, #{state}, #{pincode})
]]>
</insert>
<update id="update" parameterType="JobInfo">
<![CDATA[
]]>
</update>
<delete id="delete" parameterType="JobInfo">
<![CDATA[
]]>
</delete>
</mapper>
</pre><br />
<br />
Mapper Interface : JobInfo.java<br />
you need to have interface for mapping with Mapper xml file.
the method name id must mapped with ID of SQL described in the mapper xml.
<br />
<pre class="prettyprint">
package com.devtrigger.dao;
import java.util.List;
import com.devtrigger.model.JobInfo;
public interface JobMapper {
List<JobInfo> selectAll();
int insert(JobInfo sampleInfo);
int update(JobInfo sampleInfo);
int delete(JobInfo sampleInfo);
}
</pre><br />
<br />
Spring Controller : IndexController.java<br />
<br />
<pre class="prettyprint">package com.devtrigger.controller;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.devtrigger.model.JobInfo;
import com.devtrigger.service.JobService;
@Controller
public class IndexController {
private final Log log = LogFactory.getLog(this.getClass());
@Autowired
private JobService jobService;
@RequestMapping(value = "/index")
@ResponseBody
public List<JobInfo> requestIndex(){
log.debug("controller is called");
return jobService.getSampleInfo();
}
}
</pre><br />
<br />
<br />
type URL "http://localhost:8080/index.do" on your browser then you will see Json String returns. The reason I show Json result instead of jsp servlet result is, because you will find many sample on internet for jsp servlet result return<br />
BTW, you need to put your jsp file under "/WEB-INF/jsp/" if you need. <br />
<br />
<pre class="prettyprint">
[{"id":"1","panNumber":"AABBAABB","address":"Address","city":"NY","state":"AB","pincode":23500},{"id":"2","panNumber":"BGDBCBDB","address":"Address","city":"LA","state":"St","pincode":23500}]
</pre>
<br />
<br />
<br />Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-673305907970891563.post-77874045772884052142012-11-23T05:38:00.000-08:002012-12-04T08:45:27.937-08:00Sample http/https connection with Apache HttpComponents in java
Hi All<br/>
While I am building common project for our team. I think it would be worth it to share with others. even it just started to build though.<br/>
<br/>
I remember few times that url connection's destination server has been down for few hours and our service also effected due to the destination server down. because, we didn't set any time out for url connection. which consumed all of JVM thread and the result was, Stuck Thread occured, no thread from thread pool.<br/>
<br/>
For building url connection. better set time out.<br/>
<br/>
There are few things more consider to make a URL connection.<br/>
- stable, stable, stable<br/>
- in case of no response from destination server for long time, it should close connection<br/>
- http connection pooling<br/>
- https connection<br/>
- connection via proxy<br/>
- via credential access <br/>
<br/>
<br/>
The reason to make this source is, Apache httpclient is very powerful for making url connection, but without looking into detail, it is difficult to use right a way.<br/>
<br/>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEil-bzsWBwsb4nEVpA1OoaYZG_XE4LEgm5j_ahzseUZmTkcuvSnMDsWnDI0FzmK1LZ3ly1fsdGSX9Ps8QgMokMeTY93p7bMcoUTWigjUdUXG7tLJWceS6Ub56d_M7WDe1WL_YJIBSZlPUXV/s1600/httpsample.png" imageanchor="1" style=""><img border="0" height="400" width="201" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEil-bzsWBwsb4nEVpA1OoaYZG_XE4LEgm5j_ahzseUZmTkcuvSnMDsWnDI0FzmK1LZ3ly1fsdGSX9Ps8QgMokMeTY93p7bMcoUTWigjUdUXG7tLJWceS6Ub56d_M7WDe1WL_YJIBSZlPUXV/s400/httpsample.png" /></a>
<br/>
<br/>
Source code structure :
<br/>
<br/>
Maven Dependency : pom.xml file<br/>
<pre class="prettyprint">
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.devtrigger</groupId>
<artifactId>sample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sample</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<downloadJavadocs>true</downloadJavadocs>
<downloadSources>true</downloadSources>
</configuration>
</plugin>
</plugins>
</build>
</project>
</pre>
Configuration : conf.xml
<pre class="prettyprint">
<?xml version="1.0" encoding="ISO-8859-1" ?>
<sample>
<proxyServer>
192.168.1.1
</proxyServer>
<proxyPort>
8080
</proxyPort>
</sample>
</pre>
HttpClientService.java
<pre class="prettyprint">
package com.devtrigger;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpVersion;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
public class HttpClientService {
private static XMLConfiguration config;
private static final int CONNECTION_TIMEOUT = 10000;
private static final int SOCKET_TIMEOUT = 60000;
private static HttpClientService httpClientService = new HttpClientService();
private HttpClientService() {
try {
config = new XMLConfiguration("config.xml");
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
public static HttpClientService getInstance(){
return httpClientService;
}
/**
* @return
*/
DefaultHttpClient getHttpClient() {
return getHttpClient(CONNECTION_TIMEOUT, SOCKET_TIMEOUT);
}
/**
* @param domain
* @param userName
* @param password
* @return
*/
DefaultHttpClient getHttpClient(String domain, String userName, String password){
DefaultHttpClient httpClient = getHttpClient();
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(domain, 443),
new UsernamePasswordCredentials(userName, password));
return httpClient;
}
/**
* @param connectionTimeOut
* @param sockeTimeOut
* @return
*/
private DefaultHttpClient getHttpClient(int connectionTimeOut, int sockeTimeOut) {
try {
HttpParams httpParams = new BasicHttpParams();
setParams(httpParams, connectionTimeOut, sockeTimeOut);
SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
registry.register(new Scheme("https", 443, sf));
ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
return new DefaultHttpClient(ccm, httpParams);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
/**
* @param httpParams
* @param connectionTimeOut
* @param sockeTimeOut
*/
private void setParams(HttpParams httpParams, int connectionTimeOut, int sockeTimeOut){
setHttpProxy(httpParams);
setHttpProtocolParams(httpParams);
setHttpConnectionParams(httpParams, connectionTimeOut, sockeTimeOut);
httpParams.setBooleanParameter("http.protocol.expect-continue", false);
}
/**
* @param httpParams
*/
private void setHttpProxy(HttpParams httpParams) {
String proxyHost = config.getString("proxyServer");
if (!StringUtils.isEmpty(proxyHost)) {
int proxyPort = Integer.parseInt(config.getString("proxyPort"));
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
}
/**
* @param httpParams
*/
private void setHttpProtocolParams(HttpParams httpParams){
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(httpParams, "utf-8");
}
/**
* @param httpParams
* @param connectionTimeOut
* @param sockeTimeOut
*/
private void setHttpConnectionParams(HttpParams httpParams, int connectionTimeOut, int sockeTimeOut){
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeOut);
HttpConnectionParams.setSoTimeout(httpParams, sockeTimeOut);
}
}
</pre><br/>
<br/>
HttpRequestService.java<br/>
<br/>
<pre class="prettyprint">
package com.devtrigger;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class HttpRequestService {
HttpClientService httpClientService = HttpClientService.getInstance();
private final Log log = LogFactory.getLog(this.getClass());
ObjectMapper objectMapper = new ObjectMapper();
private boolean useCredential = false;
private String domain;
private String userName;
private String password;
private boolean isUseCredential() {
return useCredential;
}
private void setUseCredential(boolean useCredential) {
this.useCredential = useCredential;
}
private String getDomain() {
return domain;
}
private void setDomain(String domain) {
this.domain = domain;
}
private String getUserName() {
return userName;
}
private void setUserName(String userName) {
this.userName = userName;
}
private String getPassword() {
return password;
}
private void setPassword(String password) {
this.password = password;
}
public void enableUseCredential(String domain, String userName, String password){
setDomain(domain);
setUserName(userName);
setPassword(password);
setUseCredential(true);
}
public String doGetRequestNgetResponseBody(String url) {
return doGetRequestNgetResponseBody(url, new ArrayList<Header>());
}
/**
* @param context
* @param url
* @param headerList
* @return
*/
public String doGetRequestNgetResponseBody(String url, List<Header> headerList) {
URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpGet get = new HttpGet(uri);
for (Header header : headerList) {
get.addHeader(header);
}
return getResponseBody(get);
}
/**
* @param context
* @param url
* @param headerList
* @return
*/
public HttpResponse doGetRequestNgetResponse(String url, List<Header> headerList) {
URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpGet get = new HttpGet(uri);
for (Header header : headerList) {
get.addHeader(header);
}
return getResponse(get);
}
/**
* @param context
* @param url
* @return
*/
public HttpResponse doGetRequestNgetResponse(String url) {
URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpGet get = new HttpGet(uri);
return getResponse(get);
}
/**
* @param context
* @param url
* @param params
* @return
*/
public String doPostRequestNgetResponseBody(String url, List<BasicNameValuePair> params) {
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity ent = null;
try {
ent = new UrlEncodedFormEntity(params);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (ent != null)
post.setEntity(ent);
return getResponseBody(post);
}
/**
* @param context
* @param url
* @param params
* @return
*/
public HttpResponse doPostRequestNgetResponse(String url, List<NameValuePair> params) {
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity ent = null;
try {
ent = new UrlEncodedFormEntity(params);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (ent != null)
post.setEntity(ent);
return getResponse(post);
}
/**
* @param context
* @param url
* @param jsonParams
* @return
*/
public String doPostRequestNgetResponseBody(String url, Object jsonObject) {
HttpPost post = new HttpPost(url);
StringEntity ent = null;
try {
log.debug(objectMapper.writeValueAsString(jsonObject));
ent = new StringEntity(objectMapper.writeValueAsString(jsonObject));
ent.setContentType("application/json");
} catch (JsonGenerationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (ent != null)
post.setEntity(ent);
return getResponseBody(post);
}
/**
* @param context
* @param httpRequest
* @return
*/
private String getResponseBody(HttpRequestBase httpRequest) {
HttpResponse response = getResponse(httpRequest);
return getResponseBody(response);
}
/**
* It returns response body string
*
* @param response
* @return
*/
public String getResponseBody(HttpResponse response) {
HttpEntity resEntity = null;
if (response != null)
resEntity = response.getEntity();
if (resEntity != null) {
try {
return EntityUtils.toString(resEntity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
public boolean saveResponseBodyStream(HttpResponse response, String filePath) {
boolean result = false;
HttpEntity resEntity = null;
if (response != null)
resEntity = response.getEntity();
if (resEntity != null) {
BufferedInputStream bis;
try {
bis = new BufferedInputStream(resEntity.getContent());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
int inByte;
while ((inByte = bis.read()) != -1)
bos.write(inByte);
bis.close();
bos.close();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
/**
* @param context
* @param url
* @param headerList
* @return
*/
public HashMap<String, String> doGetRequestNgetResponseHeader(String url, List<Header> headerList) {
URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpGet get = new HttpGet(uri);
for (Header header : headerList) {
get.addHeader(header);
}
return getResponseHeader(get);
}
/**
* @param context
* @param url
* @param params
* @return
*/
public HashMap<String, String> doPostRequestNgetResponseHeader(String url, List<BasicNameValuePair> params) {
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity ent = null;
try {
ent = new UrlEncodedFormEntity(params);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (ent != null)
post.setEntity(ent);
return getResponseHeader(post);
}
/**
* @param context
* @param httpRequest
* @return
*/
private HashMap<String, String> getResponseHeader(HttpRequestBase httpRequest) {
HttpResponse response = getResponse(httpRequest);
return getResponseHeader(response);
}
/**
* @param response
* @param headerName
* @return
*/
public String getResponseHeaderValue(HttpResponse response, String headerName) {
HashMap<String, String> header = getResponseHeader(response);
return (String) header.get(headerName);
}
/**
* @param response
* @return
*/
public HashMap<String, String> getResponseHeader(HttpResponse response) {
if (response != null) {
Header[] headers = response.getAllHeaders();
return converHeaders2Map(headers);
} else {
return new HashMap<String, String>();
}
}
/**
* when response parameter is null, it will return 410 Gone status.
*
* @param response
* @return
*/
public int getResponseCode(HttpResponse response) {
if (response != null) {
return response.getStatusLine().getStatusCode();
} else {
return HttpStatus.SC_GONE;
}
}
/**
* @param headers
* @return
*/
private HashMap<String, String> converHeaders2Map(Header[] headers) {
HashMap<String, String> hashMap = new HashMap<String, String>();
for (Header header : headers) {
hashMap.put(header.getName(), header.getValue());
}
return hashMap;
}
/**
* @param context
* @param httpRequest
* @return
*/
public HttpResponse getResponse(HttpRequestBase httpRequest) {
HttpClient httpClient = null;
if(!isUseCredential()){
httpClient = httpClientService.getHttpClient();
}else{
httpClient = httpClientService.getHttpClient(getDomain(), getUserName(), getPassword());
setUseCredential(false);
}
HttpResponse response = null;
try {
response = httpClient.execute(httpRequest);
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return response;
}
}
</pre><br/>
<br/>
<br/>
HttpRequestTest.java for Junit test run.<br/>
<pre class="prettyprint">
package com.devtrigger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Test;
public class HttpRequestTest {
private final Log logger = LogFactory.getLog(this.getClass());
@Test
public void testRequest(){
HttpRequestService service = new HttpRequestService();
String response = service.doGetRequestNgetResponseBody("http://www.google.com/");
logger.debug("response : " + response);
Assert.assertTrue(response.substring(0, 20).matches(".*(?i)(HtMl).*"));
}
}
</pre><br/>
<br/>
Test Success<br/>
<br/>
In the end, you can just use like this on your source code.<br/>
HttpRequestService service = new HttpRequestService();<br/>
String response = service.doGetRequestNgetResponseBody("http://www.google.com/");<br/>
further more, you can do post request with header vales or json http body request so on.
probably you would need to extend more method.<br/>
<br/>
I hope it helps :) even though, it is not optimized and refactored.<br/>
<br/>
<br/>
<br/>
@Add Dec 4th 2012<br/>
I have simply looped the test cases 1000 times to my local server.<br/>
It worked well and look into regarding to if it is threadsafe and concurrent.
It is thread safe. I need to look into more, it's not easy for me to follow up this.
you can get more detail info below <br/>
<a href="http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html">http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html</a><br/>
"HttpClient assumes complete control over the process of connection initialization and termination as well as I/O operations on active connections. However various aspects of connection operations can be influenced using a number of parameters."<br/>
<br/>
<a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/PoolingClientConnectionManager.html">http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/PoolingClientConnectionManager.html</a><br/>
"PoolingConnectionManager maintains a maximum limit of connection on a per route basis and in total. Per default this implementation will create no more than than 2 concurrent connections per given route and no more 20 connections in total. For many real-world applications these limits may prove too constraining, especially if they use HTTP as a transport protocol for their services. Connection limits, however, can be adjusted using HTTP parameters."
<br/>
<br/>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-673305907970891563.post-87506958532971876022012-11-05T08:54:00.002-08:002012-11-07T00:29:24.204-08:00How to use Maven Module, Example from Spring Batch<br />
I have tried to use maven module for related project set up in once. but, I was not able to find good sample or answer for me. So I opened up well known open source project source files who uses maven. I found one from Spring Batch.<br />
<br />
Benefit for using maven module :<br />
I can manage related dependencies version number in once <br />
Several projects can be setup once in your IDE. <br />
Your IDE (ex: eclipse ) will look the module project which applies your latest changes between the projects.<br />
My team member will get all these benefit. Also I can save time to help team member.<br/>
<br />
It is little long to follow, so, I will explain as simple as possible for easy understanding.<br />
<br />
Directory Structure<br />
<br />
<pre class="prettyprint">/juno_workspace/SpringSource-spring-batch-326da17$ find ./ -name 'pom.xml'
./pom.xml
./spring-batch-parent/pom.xml
./spring-batch-core/pom.xml
./spring-batch-infrastructure/pom.xml
</pre>
<br />
<br />
<h2>./pom.xml file</h2><br />
The point from this file is, "packaging" is set to <span style="color: red;">"pom"</span> and defines <span style="color: red;">"modules"</span><br />
<pre class="prettyprint"><?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch</artifactId>
<name>Spring Batch</name>
<description>Spring Batch provides tools for enterprise batch or bulk processing. It can be used to wire up jobs, and track
their execution, or simply as an optimization for repetitive processing in a transactional environment. Spring Batch is part of the Spring Portfolio.</description>
<version>2.2.0.BUILD-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>spring-batch-parent</module>
<module>spring-batch-infrastructure</module>
<module>spring-batch-core</module>
<module>spring-batch-test</module>
</modules>
</pre>
<br />
<br />
<h2>./spring-batch-parent/pom.xml file</h2><br />
The point from this file is, "packaging" is set to <span style="color: red;">"pom"</span> and defines <span style="color: red;">"dependencyManagement"</span><br />
<pre class="prettyprint"><?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<name>Spring Batch Parent</name>
<description>Spring Batch parent project. Defines dependencies and common configuration for the build process.</description>
<url>http://static.springframework.org/spring-batch/${project.artifactId}</url>
<packaging>pom</packaging>
...
<dependencyManagement>
<dependencies>
...
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
...
</pre>
<br />
<br />
<h2>./spring-batch-core/pom.xml file</h2><br />
The point from this file is, "packaging" is set to <span style="color: red;">"jar"</span> and defines <span style="color: red;">parent</span><br />
<pre class="prettyprint"><?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-batch-infrastructure</artifactId>
<packaging>jar</packaging>
<name>Infrastructure</name>
<description><![CDATA[The Spring Batch Infrastructure is a set of
low-level components, interfaces and tools for batch processing
applications and optimisations.]]>
</description>
<url>http://static.springframework.org/spring-batch/${project.artifactId}</url>
<parent>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<relativePath>../spring-batch-parent</relativePath>
</parent>
...
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<optional>true</optional>
</dependency>
...
</pre>
<br />
<br />
<h2>./spring-batch-infrastructure/pom.xml file</h2><br />
The point from this file is, "packaging" is set to <span style="color: red;">"jar"</span> and defines <span style="color: red;">parent</span><br />
<pre class="prettyprint"><?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-batch-core</artifactId>
<packaging>jar</packaging>
<name>Core</name>
<description>Core domain for batch processing, expressing a domain of Jobs, Steps, Chunks, etc.</description>
<url>http://static.springframework.org/spring-batch/${project.artifactId}</url>
<parent>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<relativePath>../spring-batch-parent</relativePath>
</parent>
...
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<optional>true</optional>
</dependency>
...
</pre>
<br />
<br />
<h2>Additionally,</h2><br/> You can see, there is <span style="color: red;">no version number described</span> for <span style="color: red;">"aspectjweaver"</span> on "./spring-batch-core/pom.xml" and "./spring-batch-infrastructure/pom.xml" file. because the version number is described on "./spring-batch-parent/pom.xml" file in <span style="color: red;">"dependencyManagement"</span>. That you can manage version number in once. It will be great to manage version number like Junit or common libraries <br />
<br />
you can run mvn eclipse:eclipse and import the project on your eclipse.
I will post detail how to import project for the next.<br/>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-69290478749736813002012-09-29T02:35:00.001-07:002012-09-29T02:35:08.977-07:00customized log4j.property location by adding system propertyI put log4j.properties in classpath, but it is not located classpath root directory.
I put all properties into properties directory to keep root classpath organized clean structure.
Since I moved the log4j.properties file, log4j makes error when process start up.
I had to put below line which add system property.<br/>
-Dlog4j.configuration="properties/log4j.properties" then it works well now.
<br/><br/>
<pre class="prettyprint">
LIB_HOME=/home01/batch/batch/common_lib
JAVA_HOME=/home02/bea10/jdk160_24
CLASSPATH=${LIB_HOME}/activation-1.1.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/api.services-50.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/axis-1.4.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/axis-jaxrpc-1.4.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/axis-saaj-1.4.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/axis-wsdl4j-1.5.1.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/commons-beanutils-1.8.3.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/commons-digester-1.8.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/commons-discovery-0.2.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/commons-lang-2.6.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/commons-logging-1.0.4.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/commons-net-3.1.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/commons-validator-1.4.0.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/ibatis-sqlmap-2.3.4.726.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/log4j-1.2.16.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/mail-1.4.5.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/server.service-1.0.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/servlet-api-2.5.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/ojdbc6.jar
CLASSPATH=${CLASSPATH}:${LIB_HOME}/uk_batch.jar
export CLASSPATH
sdate=`date +'%Y%m%d'`
stime=`date +'%H%M%S'`
${JAVA_HOME}/bin/java -Dlog4j.configuration="properties/log4j.properties" -cp ${CLASSPATH} batch.Run
</pre>
<br/>
<br/>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-8470976610378600392012-09-29T02:21:00.001-07:002013-01-08T02:49:49.631-08:00Android http/https connection sample code
Http/Https connection is necessary to build a Android application for interact with web server.
To make http connection, I made 2 simple java classes.<br />
It is working well and tested on android SDK 15 version and it will probably work the other version as well. I hope it can save your time.
<br />
<br />
You can call "doPostRequestNgetResponseBody" method or similar in your android Activity class. you can find more method from "HttpClientUtil" class.
<pre class="prettyprint">
String url = "https://yoursite";
ArrayList<BasicNameValuePair> arrayList = new ArrayList<BasicNameValuePair>();
arrayList.add(new BasicNameValuePair("your param name", "your param value"));
String responseBody = HttpRequestUtil.doPostRequestNgetResponseBody(context, url, arrayList);
</pre>
<br />
<br />
To call the method, you need to have below 2 classes.
HttpClientUtil.java : return http/https connection client.
<pre class="prettyprint">
package com.jackapps.http;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import android.content.Context;
import android.net.SSLCertificateSocketFactory;
import android.net.SSLSessionCache;
public class HttpClientUtil {
private static final int CONNECTION_TIMEOUT = 5000;
private static final int SOCKET_TIMEOUT = 20000;
/**
* @param context
* @return
*/
static HttpClient getHttpClient(Context context) {
return getHttpClient(context, CONNECTION_TIMEOUT, SOCKET_TIMEOUT);
}
/**
* @param context
* @param connectionTimeOut
* @param sockeTimeOut
* @return
*/
static synchronized HttpClient getHttpClient(Context context, int connectionTimeOut, int sockeTimeOut) {
// Use a session cache for SSL sockets
SSLSessionCache sessionCache = context == null ? null : new SSLSessionCache(context);
// sets up parameters
HttpParams httpParams = new BasicHttpParams();
setHttpProtocolParams(httpParams);
setHttpConnectionParams(httpParams, connectionTimeOut, sockeTimeOut);
httpParams.setBooleanParameter("http.protocol.expect-continue", false);
// registers schemes for both http and https
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
final SSLSocketFactory sslSocketFactory = SSLCertificateSocketFactory.getHttpSocketFactory(sockeTimeOut,
sessionCache);
// sslSocketFactory.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
registry.register(new Scheme("https", sslSocketFactory, 443));
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(httpParams, registry);
return new DefaultHttpClient(manager, httpParams);
}
/**
* @param httpParams
*/
private static void setHttpProtocolParams(HttpParams httpParams){
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(httpParams, "utf-8");
}
/**
* @param httpParams
* @param connectionTimeOut
* @param sockeTimeOut
*/
private static void setHttpConnectionParams(HttpParams httpParams, int connectionTimeOut, int sockeTimeOut){
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeOut);
HttpConnectionParams.setSoTimeout(httpParams, sockeTimeOut);
}
}
</pre>
<br />
<br />
HttpRequestUtil.java : return response body, headers
<pre class="prettyprint">
package com.jackapps.http;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import android.content.Context;
import com.jackapps.model.AccessTokenInfo;
public class HttpRequestUtil {
/**
* @param context
* @param url
* @param headerList
* @return
*/
public static String doGetRequestNgetResponseBody(Context context, String url, List<Header> headerList) {
URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpGet get = new HttpGet(uri);
for (Header header : headerList) {
get.addHeader(header);
}
return getResponseBody(context, get);
}
/**
* @param context
* @param url
* @param headerList
* @return
*/
public static HttpResponse doGetRequestNgetResponse(Context context, String url, List<Header> headerList) {
URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpGet get = new HttpGet(uri);
for (Header header : headerList) {
get.addHeader(header);
}
return getResponse(context, get);
}
/**
* @param context
* @param url
* @param params
* @return
*/
public static String doPostRequestNgetResponseBody(Context context, String url, List<BasicNameValuePair> params) {
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity ent = null;
try {
ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (ent != null)
post.setEntity(ent);
return getResponseBody(context, post);
}
/**
* @param context
* @param url
* @param params
* @return
*/
public static HttpResponse doPostRequestNgetResponse(Context context, String url, List<BasicNameValuePair> params) {
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity ent = null;
try {
ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (ent != null)
post.setEntity(ent);
return getResponse(context, post);
}
/**
* @param context
* @param url
* @param jsonParams
* @return
*/
public static String doPostRequestNgetResponseBody(Context context, String url, JSONObject jsonParams) {
HttpPost post = new HttpPost(url);
StringEntity ent = null;
try {
ent = new StringEntity(jsonParams.toString(), HTTP.UTF_8);
ent.setContentType("application/json");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (ent != null)
post.setEntity(ent);
return getResponseBody(context, post);
}
/**
* @param context
* @param url
* @param jsonParams
* @return
*/
public static HttpResponse doPostRequestNgetResponse(Context context, String url, JSONObject jsonParams) {
HttpPost post = new HttpPost(url);
StringEntity ent = null;
try {
ent = new StringEntity(jsonParams.toString(), HTTP.UTF_8);
ent.setContentType("application/json");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (ent != null)
post.setEntity(ent);
return getResponse(context, post);
}
/**
* @param context
* @param httpRequest
* @return
*/
private static String getResponseBody(Context context, HttpRequestBase httpRequest) {
HttpResponse response = getResponse( context, httpRequest);
return getResponseBody(response);
}
/**
* It returns response body string
* @param response
* @return
*/
public static String getResponseBody(HttpResponse response) {
HttpEntity resEntity = null;
if (response != null)
resEntity = response.getEntity();
if (resEntity != null) {
try {
return EntityUtils.toString(resEntity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
/**
* @param context
* @param url
* @param headerList
* @return
*/
public static HashMap<String,String> doGetRequestNgetResponseHeader(Context context, String url, List<Header> headerList) {
URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpGet get = new HttpGet(uri);
for (Header header : headerList) {
get.addHeader(header);
}
return getResponseHeader(context, get);
}
/**
* @param context
* @param url
* @param params
* @return
*/
public static HashMap<String,String> doPostRequestNgetResponseHeader(Context context, String url, List<BasicNameValuePair> params) {
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity ent = null;
try {
ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (ent != null)
post.setEntity(ent);
return getResponseHeader(context, post);
}
/**
* @param context
* @param url
* @param jsonParams
* @return
*/
public static HashMap<String,String> doPostRequestNgetResponseHeader(Context context, String url, JSONObject jsonParams) {
HttpPost post = new HttpPost(url);
StringEntity ent = null;
try {
ent = new StringEntity(jsonParams.toString(), HTTP.UTF_8);
ent.setContentType("application/json");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (ent != null)
post.setEntity(ent);
return getResponseHeader(context, post);
}
/**
* @param context
* @param httpRequest
* @return
*/
private static HashMap<String,String> getResponseHeader(Context context, HttpRequestBase httpRequest) {
HttpResponse response = getResponse( context, httpRequest);
return getResponseHeader(response);
}
/**
* @param response
* @param headerName
* @return
*/
public static String getResponseHeaderValue(HttpResponse response, String headerName){
HashMap<String,String> header = getResponseHeader(response);
return (String)header.get(headerName);
}
/**
* @param response
* @return
*/
public static HashMap<String,String> getResponseHeader(HttpResponse response) {
if (response != null){
Header[] headers = response.getAllHeaders();
return converHeaders2Map(headers);
}else{
return new HashMap<String,String>();
}
}
/**
* when response parameter is null, it will return 410 Gone status.
* @param response
* @return
*/
public static int getResponseCode(HttpResponse response) {
if (response != null){
return response.getStatusLine().getStatusCode();
}else{
return HttpStatus.SC_GONE;
}
}
/**
* @param headers
* @return
*/
private static HashMap<String,String> converHeaders2Map(Header[] headers){
HashMap<String,String> hashMap = new HashMap<String,String>();
for(Header header: headers){
hashMap.put(header.getName(), header.getValue());
}
return hashMap;
}
/**
* @param context
* @param httpRequest
* @return
*/
public static HttpResponse getResponse(Context context, HttpRequestBase httpRequest) {
HttpClient httpClient = HttpClientUtil.getHttpClient(context);
HttpResponse response = null;
try {
response = httpClient.execute(httpRequest);
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return response;
}
}
</pre>Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-673305907970891563.post-70236110334499139862012-08-30T00:45:00.003-07:002012-08-30T00:45:57.098-07:00SVN backup on Unix or Solaris<br />
I made a simple script to backup SVN shell script.
It is working on ssh terminal on my session well. but when it run by crontab. it is not running and leave no error message.
It was difficult to solve this issue if there is no error message.<br />
<br />
My colleague who is expert Unix system gave me an advice, check email on Unix.
I have searched how to check email on Unix. time by time I am noticed that there is new email. I didn't know I can read that from the server.<br />
<br />
'mailx' this is the command to check email.
then you can select number to read email.
I found the log from crontab, it seems few library and command it not found.
I copy PATH, LD_LIBRARY_PATH, LD_RUN_PATH variables to shell script.
you can get the variable definition when you type 'env'<br />
<br />
In the end, it's running.<br />
<br />
<pre class="prettyprint">
#!/bin/ksh
datum=`/bin/date +%Y%m%d`
LD_LIBRARY_PATH=/lib:/usr/lib:/usr/sfw/lib:/usr/local/lib:/usr/dt/lib:/usr/share/lib:/usr/ccs/lib:/
usr/local/ssl/lib:/usr/local/instantclient10_1_32bit:/opt/coolstack/apache2/lib:/usr/local/apr/lib
PATH=/usr/sbin:/usr/bin:/usr/ucb:/opt/sunstudio12.1/bin:/opt/csw/bin/:/opt/csw/mysql51/bin/sparcv8:
/opt/coolstack/bin:/usr/local/bin:/usr/sbin:/usr/ccs/bin:/usr/sfw/bin:/usr/local/php/bin:/usr/ucb:/
opt/sunstudio12.1/bin:/opt/csw/bin/:/opt/csw/mysql51/bin/sparcv8:/opt/coolstack/bin:/usr/local/bin:
/usr/sbin:/usr/ccs/bin:/usr/sfw/bin:/usr/local/php/bin
LD_RUN_PATH=:usr/local/BerkeleyDB.4.8/lib
export LD_LIBRARY_PATH
export PATH
export LD_RUN_PATH
/usr/local/bin/svnadmin dump /home/svn/sfc > /home/backup_nas/svn/sfc_$datum.dump
</pre><br />
<br />
- SVN Backup
<pre class="prettyprint">
nohup svnadmin dump /home/svn/sfc > /home/backup_all/svn/sfc.dump &
</pre><br />
<br />
- SVN incremental Backup
<pre class="prettyprint">
$ svnadmin dump myrepos --revision 0:1000 > dumpfile1
$ svnadmin dump myrepos --revision 1001:2000 --incremental > dumpfile2
$ svnadmin dump myrepos --revision 2001:3000 --incremental > dumpfile3
svnadmin dump <repos> -r 58:HEAD --deltify > <file2>
</pre><br />
<br />
- SVN recovery
<pre class="prettyprint">
svnadmin load < ~/repos-0-1000.svn_dump
svnadmin load < ~/repos-1000-2000.svn_dump
svnadmin load < ~/repos-2000-3000.svn_dump
</pre><br />
<br />
- SVN revision pack per 1000 revision
<pre class="prettyprint">
svnadmin pack /home/svn/sfc
</pre><br />
<br />
- SVN repository create
<pre class="prettyprint">
/usr/local/bin/svnadmin create path/to/repos
</pre><br />
<br />
- SVN daemon shutdown
<pre class="prettyprint">
pkill -KILL svnserve
</pre><br />
<br />
- SVN daemon start
<pre class="prettyprint">
svnserve -d -r /home/svn/
</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-59573764529057749842012-07-19T10:13:00.000-07:002012-07-19T10:13:19.110-07:00Maven java project archetype<br />
If you use maven, you like to use archetype to get all benefits of maven.<br />
To create maven java project, you can use below command below.<br />
<br />
It's an example to create sample archetype.<br />
<br />
<pre class="prettyprint">
mvn archetype:generate -DgroupId=com.example -DartifactId=test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
</pre><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTp7goCEsVKYJWhWGXvltH0PKQUWYaE6041zkkJPZ8VhKaN1JnPHyEKX2twh9kk4XjAbXERLNZXBkl4vlsXzJHDcHK9pjRB0AhUiPuDR-LMl1iPQd9hhBEr0gFVgAUCjza37NrX7tWGLEv/s1600/archetype.png" imageanchor="1" style="margin-left:1em; margin-right:1em"><img border="0" height="281" width="633" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTp7goCEsVKYJWhWGXvltH0PKQUWYaE6041zkkJPZ8VhKaN1JnPHyEKX2twh9kk4XjAbXERLNZXBkl4vlsXzJHDcHK9pjRB0AhUiPuDR-LMl1iPQd9hhBEr0gFVgAUCjza37NrX7tWGLEv/s400/archetype.png" /></a></div>
<br />
<br />
Then you can simply go under test directory and run below command<br />
<pre class="prettyprint">
mvn eclipse:eclipse
</pre><br />
<br />
After that, simply import this project into eclipse<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheVR47FFh8yb2lBeZpenDyH_2Wf0txzbJ3XdkqwQNkJzBMIaQu3AGd2VMzRvN4u1tUkyOK3-NeqEt_EvRqWRElS3YtjvoSKX4Wi3YHDsoLRWLFP5xtRUpK00rACTRU6AgRJmpGdtkDFgt1/s1600/import1.png" imageanchor="1" style="margin-left:1em; margin-right:1em"><img border="0" height="378" width="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheVR47FFh8yb2lBeZpenDyH_2Wf0txzbJ3XdkqwQNkJzBMIaQu3AGd2VMzRvN4u1tUkyOK3-NeqEt_EvRqWRElS3YtjvoSKX4Wi3YHDsoLRWLFP5xtRUpK00rACTRU6AgRJmpGdtkDFgt1/s400/import1.png" /></a></div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEKMxkgSuAPEv9xRwC48IRiBGFWy4jqyw_PTOU_03uGpp7X9PqtMyEemv8RKjGFrmYFTCS4ptq2WB0dSYjI70QdFY2vOybS1u9IgVJnKoQo0-CRH-xgHOrm0EQg4UckCqgKBgGlpv8anW1/s1600/import2.png" imageanchor="1" style="margin-left:1em; margin-right:1em"><img border="0" height="380" width="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEKMxkgSuAPEv9xRwC48IRiBGFWy4jqyw_PTOU_03uGpp7X9PqtMyEemv8RKjGFrmYFTCS4ptq2WB0dSYjI70QdFY2vOybS1u9IgVJnKoQo0-CRH-xgHOrm0EQg4UckCqgKBgGlpv8anW1/s400/import2.png" /></a></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-673305907970891563.post-71989229056326858432012-06-18T08:56:00.003-07:002012-06-18T08:56:23.834-07:00how to find byte code class file JDK compiled java versionyou can find out the byte code class version.<br />
<br />
On Linux, Mac OS X or Windows with Cygwin installed, the file(1) command knows the class version. Extract a class from a jar and use file to identify it:<br />
<br />
<pre class="prettyprint">
$ jar xf log4j-1.2.15.jar
$ file ./org/apache/log4j/Appender.class
./org/apache/log4j/Appender.class: compiled Java class data, version 45.3
</pre>
<br />
A different class version, for example:<br />
<br />
<pre class="prettyprint">
root@mypc:/merchant-sample/classes/com/paypal/core$ file SSLUtil.class
SSLUtil.class: compiled Java class data, version 50.0 (Java 1.6)
</pre>
<br />
<br />
The class version major number corresponds to the following Java JDK versions:
<br />
<br />
46 = Java 1.2<br />
47 = Java 1.3<br />
48 = Java 1.4<br />
49 = Java 5<br />
50 = Java 6<br />
51 = Java 7<br />
<br />
Alternatively, you can make a simple test class that can identify class version.
<br />
<pre class="prettyprint">
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
checkClassVersion("Class path you want to know");
}
private static void checkClassVersion(String filename)
throws IOException
{
DataInputStream in = new DataInputStream
(new FileInputStream(filename));
int magic = in.readInt();
if(magic != 0xcafebabe) {
System.out.println(filename + " is not a valid class!");;
}
int minor = in.readUnsignedShort();
int major = in.readUnsignedShort();
System.out.println(filename + ": " + major + " . " + minor);
in.close();
}
}
</pre>
<br />
<br />Unknownnoreply@blogger.com0