Search This Blog

Monday, January 17, 2011

Adding custom http header to each request with FireFox and IE

Sometimes you need to add an extra http header for each request , to simulate requests from third parties or clients.
in my case i needed to add an extra header for each request to simulate "single sign on" to my system, so an extra header added by the client with the user id , and i wanted to test such solution, without the need to develop a proxy or any test tool, so i found this.
For Firefox -  use the add on Modify header to Add, modify and filter http request headers.
For IE browser - it more complicated:

  • you have to download Fiddler
  • Open the 'Customize Rules' window (Rules->Customize Rules...)
  • Find this function: static function OnBeforeRequest(oSession: Session)
  • inside this function, add the header values that you would like to add, in this format: oSession.oRequest["headerName"] = "headerValue";
To get your new header on each request , open your IE , and make sure Fiddler is running.

enjoy 
Yaniv Tzanany

Tuesday, January 4, 2011

Working with WebSphere MQ JMS Under WebSphere app server

i struggled allot to make this JMS  sample working .
follow the next instruction to use JMS under web sphere.

1. get into admin console
2. get into JMS->connection factories  -> new
    choose web sphere MQ , click OK .
give it a logic name , and a jndi name such as jms/myMqFactory, and set the correct parameters during the wizard.
click finish and save.

3. goto Resources -> JMS ->queues
4. click new , and choose websphere MQ message provider
set the name and jndi name such as jms/mqRequestQ
and set the queue name
5. click apply and save.

some resources i used :
Developing a JMS client

IBM WebSphere Developer Technical Journal: Building an Enterprise Service Bus with WebSphere Application Server V6 -- Part 3
A java.lang.ClassCastException error occurs during a JNDI lookup of a queue connection factory


How to extract Queue Manager Name from CF / QCF in your J2EE JMS application



here is the code snippets, i used tp send message to mqRequestQ and get result from mqResponseQ , and pick the message with the same msg id:

private void testJMS(String qIn,String qOut )
{

    String  outString                 = "I am a string from JMS";
    String  qcfName                   = "jms/myMqFactory";
    String  qnameIn                   = "jms/mqRequestQ";
    String  qnameOut                  = "jms/mqResponseQ";
    boolean verbose                   = false;

    Session           session    = null;


    Connection        connection = null;
    ConnectionFactory   qcf  = null;

    Queue                  inQueue    = null;
    Queue                  outQueue   = null;
    InitialContext context = null;
    try {
context = new InitialContext();
qcf = (ConnectionFactory)context.lookup( qcfName );
inQueue = (Queue)context.lookup( qnameIn );
outQueue = (Queue)context.lookup( qnameOut );

   if(qcf != null)
   {
connection = qcf.createConnection();  
connection.start();
boolean transacted = false;
session = connection.createSession( transacted,
Session.AUTO_ACKNOWLEDGE);
((MQQueue)inQueue).setTargetClient(JMSC.MQJMS_CLIENT_NONJMS_MQ); //its 1
MessageProducer  queueSender = session.createProducer(inQueue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
queueSender.setTimeToLive(60000); // in milli seconds 0 - unlimited
TextMessage outMessage = session.createTextMessage(outString);
outMessage.setJMSCorrelationID("Alis0000");
queueSender.send(outMessage);
queueSender.close();

String msgID  = outMessage.getJMSMessageID();
String selector1 = "JMSMessageID='"+msgID+"'";
MessageConsumer queueReciever =  session.createConsumer(outQueue, selector1);
javax.jms.Message inMessage =  queueReciever.receive(2000);
if ( inMessage instanceof TextMessage )
{
String replyString = ((TextMessage) inMessage).getText();
}
queueReciever.close();

session.close();
session = null;
connection.close();
connection = null;
   }

} catch (NamingException e) {
// there is no such DSN lets report it and will try to work without the DataSource name

} catch (JMSException je) {
      System.out.println("JMS failed with "+je);
      Exception le = je.getLinkedException();
      if (le != null)
      {
  System.out.println("linked exception "+le);
      }
}
}

enjoy
Yaniv Tzanany