How to Integrate Glassfish with IIS

After working a new project for awhile (I’m working with PHP and Java).  As a quick background story, I’m using Symfony2 and Guzzle to make my calls to my Java webservices.  This was working out great except for one minor issue.  Well, minor if you count not being able to call my REST services through Guzzle.  My problem was that IIS is running my PHP application on port 80 while my Glassfish REST services were running on port 8080 (the Glassfish default).  I needed to set up my applications forward requests to Glassfish.  So I’ll quickly guide you how to set this up.  This will work for any application server, not just Glassfish.

  1. Make sure you have Glassfish and IIS installed.  I’m using Glassfish 3.1.2 and IIS 7.5.  Any other versions should work with a few modifications.  You can download the latest version of Glassfish here http://glassfish.java.net/ and all your latest IIS needs can probably be found here http://www.iis.net/.
  2. Download Microsoft Web Platform Installer 3.0.  You can find a download here http://www.microsoft.com/web/downloads/platform.aspx.  After you’ve finished your download install it.
  3. After you’ve install Microsoft Web Platform Installer 3.0 you need to install Web Farm Framework.  To do this launch IIS (Click start, run, then type IIS).
  4. You should see Web Platform Installer installed in the bottom pane of IIS.  Double click on it and then do a search for Web Farm Framework.  Image
  5. Click add and then Install.  You might have a few other components that need to be installed.  Go ahead and click accept.Image
  6. Woohoo that was awfully easy.  Now navigate to the main screen of IIS.  You should have a Server Farms node.  Click on Create Server Farm in the right panel.Image
  7. Enter the name of the server farm and click next.  Click finish without adding a server.  Click yes to add routing rules for the server.
  8. Select Servers then click Add Servers on the right pane.Image
  9. Enter the IP address or server name of the server you’re adding to the farm.  In my case this is localhost.  Click on Add and this will add the server to your farm.
  10. Click on Advanced settings and select the server you’ve added.  You should see the ports that you can assign to your server. Enter your port number for HTTP and HTTPS.  I’m entering 8080 because that’s what my Glassfish Server is running on.Image
  11. Great we’re almost there.  All we have to do now is set up some routing rules.  I only want certain URLs getting routed to Glassfish.  Select your server farm and then double click on Routing Rules.Image
  12. Now just add the rules you want to exclude.  Everything else will be forwarded.  I don’t want any PHP extensions forwarded nor do I want anything that’s URL contains app_dev.php (Development purposes with PHP).  My rules look like this.Image
  13. You’re all set!  Now you can hit your Glassfish URL’s without entering the port.  This fixed one of the issues I was having with Guzzle.

Now that we’ve set this up I’ll show you how to set up Symfony2 and Guzzle so you can start writing a PHP application that makes REST calls.

Setting up Symfony2 with Guzzle Part 2

Sorry for the late post. I’ve been unbelievably busy lately and haven’t had time to update my blog. Needless to say here is part 2 of setting up Symfony2 with Guzzle.

  1. Now that we have Guzzle set up we need set which web services are going to be called through Guzzle.  Navigate to app/config in Symfony2 and create a new file called webservices.xml.  It should look something like this: 
    You can use inheritance for your web services.  Each client is a new instance or different URL of an interface to the web service.  The class property tells you what class to instanciate whenever you are making the API calls through Symfony2.  Extends is pretty self explanitory.  It’s the parent class or parent properties.  In this case the access_key and the secret_key are inherited to each of the clients that use abstract.aws.
  2. The next step is to create a client that you can use.  Each of the clients defined in your webservices.xml that have a class must point to a valid PHP class.  You can either use a Client or you can extend Guzzle\Service\Client and create your own custom client.  Just make sure that you have a constructor
  3. Now you need to create some commands for your client otherwise you won’t be able to call anything.  Commands are basically the specific calls you want to make.  For example you might have a book REST service.  You might have different API calls to do your basic CRUD functions.  You will need to create a command for each of those functions.  When you create these, they must extend from Guzzle\Service\Command\AbstractCommand.  What you want to do here is set several properties.  This might be an ID field. /**
    * Set the data to post
    *
    * @param bookId
    *
    * @return string
    */
    public function setBookId($bookId)
    {
    return $this->set('bookId', $bookId);
    }
  4. The requirement for this class is that you have a build() function.  This function will build your URL for you and set the data you need.  You will set whether you are sending a POST, GET, PUT or any other HTTP verbs.  In mine I have a custom function that will append the end of the URL and set the correct ID.protected function build()
    {
    $data = $this->get('bookId');
    $this->request = $this->client->getBookRequest('book/'.$data,RequestInterface::GET );
    }
  5. Now all we have to do is call Guzzle and everything will work!  Create a builder object and get it through Symfony2 service.  Use the builder to get the client you want.  This will be the client name you specified in webservices.xml above.  Then specify which command you want to execute.  Then execute the request and Guzzle will handle everything else for you.$builder = $this->get('guzzle.service_builder');
    $client = $builder['book'];
    $command = $client->getCommand('GetBook');
    $command->setBookId(1);
    $response = $client->execute($command);
  6. Now one thing to note is that you can return a custom object if you need to.  This will keep your code cleaner.  In order to do that you must definte a process() function and a getResult() function in your command.  In this example I want my response to be an array so I convert the response to an array and I’m done! /**
    * {@inheritdoc}
    */
    protected function process()
    {
    $this->result = json_decode($this->getResponse()->getBody(),true);
    }

    /**
    *
    *
    * @return array
    */
    public function getResult()
    {
    return parent::getResult();
    }

  7. There you go.  If you have any questions feel free to leave a comment or email me directly.

Setting up Symfony2 with Guzzle Part 1

So in working with my latest project I needed to make some REST calls through PHP.  After doing a bit of research I decided to use Guzzle http://guzzlephp.org/.  The reason why I decided to use Guzzle is that it’s simple, easily extensible, and has a testing framework.  So I’m going to make a few assumptions in this tutorial.  I’m assuming that you’ve already got Symfony2 up and running on your machine.  If not, go to http://symfony.com/ and follow the Get Started link to get up and running.  So now without further ado!

  1. Navigate to your Symfony2 location and run this:
    git submodule add git://github.com/guzzle/guzzle.git vendor/guzzle
    git submodule add git://github.com/ddeboer/GuzzleBundler vendor/bundles/Ddeboer/GuzzleBundle
  2. Now go to your Autoload.php (app/autoload.php) file and register namespaces.
    $loader->registerNamespaces(array(// ...
    'Guzzle' => __DIR__.'/../vendor/guzzle/src',
    'Ddeboer' => __DIR__.'/../vendor/bundles',
    // ...
    ));
  3. Add the GuzzleBundle to your application kernel. (app/AppKernel.php)
    public function registerBundles()
    {
    // ...
    new Ddeboer\GuzzleBundle\DdeboerGuzzleBundle(),
    // ...
    }
  4. The last step you need to do is set up your Guzzle service builder. (app/config/config.yml)

    ddeboer_guzzle:
    service_builder:
    configuration_file: "%kernel.root_dir%/config/webservices.xml"
    cache:
    adapter: doctrine
    driver: apc
    I had some problems with the cache section so you can remove that if you run into any problems.

Now you’re all set up to start sending REST requests through Guzzle in your Symfony2 application.  Next blog post I’ll show you how to use Guzzle’s extensibility (that should definitely be a word) to turn your REST calls into an API that’s super easy to use.