Proper timeout handling with Apache HttpClient

August 24th, 2009 by Brian filed in work has 5 comments

I’ve seen some really bad things happen when developers don’t code in proper timeout handling. Occasionally I’ve been asked what the best way to handle timeouts is – so I thought I’d share my take on it:

  1. MultiThreadedHttpConnectionManager connectionManager =  new MultiThreadedHttpConnectionManager();
  2. HttpConnectionManagerParams params = connectionManager.getParams();
  3.  
  4. params.setConnectionTimeout(connectiontimeout); //set connection timeout (how long it takes to connect to remote host)
  5. params.setSoTimeout(sotimeout); //set socket timeout (how long it takes to retrieve data from remote host)
  6.  
  7. HttpMethodBase baseMethod = null;
  8.  
  9. try {
  10.   HttpClient httpClient = new HttpClient(connectionManager);
  11.   httpClient.getParams().setParameter("http.connection-manager.timeout", poolTimeout); //set timeout on how long we’ll wait for a connection from the pool
  12.  
  13.   baseMethod = new GetMethod();
  14.   int statusCode = httpClient.executeMethod();
  15.  
  16.   …
  17. }
  18. catch (ConnectTimeoutException cte ){
  19.   //Took too long to connect to remote host
  20. }
  21. catch (SocketTimeoutException ste){
  22.   //Remote host didn’t respond in time
  23. }
  24. catch (Exception se){
  25.   //Some other error occurred
  26. }
  27. finally {
  28.   if (baseMethod != null)
  29.     baseMethod.releaseConnection();
  30. }

Tags: , , ,

Subversion keeps changing permissions of my files [FIX]

August 4th, 2009 by Brian filed in work has no comments

On my Unix machine whenever I did an “svn update” and files were actually updated, the permissions would change, rendering them inaccessible to the httpd process, therefore 403 Forbidden errors in the browser.

After much searching that yielded a lot of results about storing executable flags in SVN using propset. This didn’t help as I didn’t want it to be executable, just world readable. Turns out the problem was in my .profile, my umask setting was 077. Changing it to 012 solved it for me. Note: 012 makes files you create group writable.

Snippet from my .profile

#umask 077
umask 012
export SVN_SSH="ssh -l olore"
export EDITOR="/usr/bin/vi"

Read more about umask on Wikipedia

Tags: , , ,

Dojo 1.1 and 1.3 on the same page

June 16th, 2009 by Brian filed in work has no comments

Today I dug into the deep dark world of running multiple versions of Dojo on the same page. Turns out, it’s not all that dark! Working with some pages as my guide I was able to get 1.1 & 1.3 working in the same page.

This is a step towards running multiple versions withing WebSphere Portal, which I’ll likely be dealing with in the near future.

  1. <html>
  2. <head>
  3. <title>Dojo 1.1 and Dojo 1.3 together on the same page </title>
  4.  
  5. <!– Bring in Dojo 1.1.x –>
  6. <script type="text/javascript">
  7.   djConfig = {
  8.     parseOnLoad: true,
  9.     baseUrl: "dojo11/dojo/"
  10.   }
  11. </script>
  12. <script type="text/javascript" src="dojo11/dojo/dojo.js"></script>
  13.  
  14. <!–
  15. redefine djConfig (allowed in 1.1+)
  16. create namespaced dojo, dijit &amp; dojox
  17. –>
  18. <script type="text/javascript">
  19.   djConfig = {
  20.     isDebug: true,
  21.     baseRelativePath: "dojo13/dojo",
  22.     scopeMap: [
  23.       ["dojo", "dojo13"],
  24.       ["dijit", "dijit13"],
  25.       ["dojox", "dojox13"]
  26.    ]
  27. }
  28. </script>
  29. <script type="text/javascript" src="dojo13/dojo/dojo.js"></script>
  30.  
  31. <!– test it –>
  32. <script type="text/javascript">
  33.   dojo.addOnLoad(function(){
  34.     alert(dojo.version);
  35.   });
  36.   dojo13.addOnLoad(function(){
  37.     alert(dojo13.version);
  38.  });
  39. </script>
  40. </head>
  41.  
  42. <body>
  43. Dojo 1.1 and Dojo 1.3 together on the same page
  44. </body>
  45.  
  46. </html>

Tags:

Websphere Portal theme navigation context menus

May 1st, 2009 by Brian filed in work has 2 comments

I spend a lot of my day working with WebSphere Portal server, and as of late, spending more time (than I’d like!) with themes & skins. I am currently working with Portal v6.1, but the information below should be useful for previous versions as well.WebSphere software

Our portal is pretty open, we want people to come in & customize it as much as they’d like. One feature that we allow is for the user to come in & create new pages. We are running our own theme policy which is a copy of the DoubleTopNav policy. Our theme displays these 2 levels of navigation at the top, and we have no side navigation.

The hurdle I ran into was – how do I prevent the “New Page” option from appearing in the portlet menu for pages in the second level of navigation? The way the out-of-the-box Portal theme works is that when you create a new page, it is created as a child of the page that you are on. This works fine for top level pages, but there no policy or rule that can be set to prevent the “New Page” option from appearing in the portlet menu for second level pages.

The solution, proposed by a member of the Portal team was to create an alternate pageContextMenu.jsp which simply didn’t have a “New Page” option. Since the call the that JSP is done via AJAX, prior to the call we can determine what navigation level we are on and call the appropriate version of pageContextMenu.jsp.

Piece of cake, right? Eh .. maybe not cake, but not all that difficult. After creating the new pageContextMenu2.jsp & removing the “New Page” option, the next place to go is head_inlineJS.jspf. Below is the code I have added/modified to the head_inlineJS.jspf.

  1. <%– START – see if we are in 2nd level of navigation –%>
  2. <%
  3. String pageContextMenuJSP = "pageContextMenu";
  4. %>
  5. <portal-navigation:navigation startLevel="2" stopLevel="3">
  6.   <portal-navigation:navigationLoop>
  7.     <portal-logic:if nodeInSelectionPath="yes">
  8.     <%
  9.       //it’s in selection path &amp; it’s selected (this is the page we are on!
  10.       if (wpsSelectionModel.isNodeSelected(wpsNavNode)){
  11.         int level = wpsNavLevel.intValue();
  12.         if (level == 2) {
  13.           pageContextMenuJSP = "pageContextMenu2";
  14.         }
  15.       }
  16.     %>
  17.     </portal-logic:if>
  18.   </portal-navigation:navigationLoop>
  19. </portal-navigation:navigation>
  20. <%– END – see if we are in 2nd level of navigation –%>
  21.  
  22. <%– Context Menu Initialization –%>
  23. <c-rt:set var="pageNoActionsText" ><portal-fmt:text bundle=‘nls.engine’ key=‘info.emptymenu’ /></c-rt:set>
  24. (function(){
  25.  
  26. var pageMenuURL = ‘<portal-navigation:url themeTemplate="<%=pageContextMenuJSP%>" />’;
  27. <%– OLD var pageMenuURL = ‘<portal-navigation:url themeTemplate="pageContextMenu" />’; –%>

Now that you’ve got that taken care of, touch head.jspf && touch Default.jsp, reload your page and you should no longer see “New Page” as an option in the page drop down for pages in the 2nd level of your navigation.

That worked and was a thing of beauty .. well, almost. I wasn’t a big fan of looping through the entire navigation model, but I didn’t know any other way. I ran the code past my colleague from the Portal team and he pointed me at the navigationSelectionModel. The code below is smaller & faster. Best of all – it works :)

  1. <%– 2009.05.04 OLORE – see if we are in 2nd level of navigation –%>
  2. <%
  3. String pageContextMenuJSP = "pageContextMenu";
  4.  
  5. int level_count = 0;
  6. for (java.util.Iterator i = nsm.iterator(); i.hasNext(); ) {
  7.   NavigationNode node = (NavigationNode) i.next();
  8.     level_count++;
  9. }
  10. if (level_count >= 4) { // level 4 == our 2nd level of top navigation (shouldn’t ever be >)
  11.   pageContextMenuJSP = "pageContextMenu2";
  12. }
  13.  
  14. %>
  15. <%– 2009.05.04 OLORE – see if we are in 2nd level of navigation –%>
  16.  
  17. <%– Context Menu Initialization –%>
  18. <c-rt:set var="pageNoActionsText" ><portal-fmt:text bundle=‘nls.engine’ key=‘info.emptymenu’ /></c-rt:set>
  19. (function(){
  20.  
  21.   <%– 2009.05.01 OLORE – use pageContextMenu2 if we are in 2nd level nav –%>
  22.   var pageMenuURL = ‘<portal-navigation:url themeTemplate="<%=pageContextMenuJSP%>" />’;
  23.   <%– 2009.05.01 OLORE – use pageContextMenu2 if we are in 2nd level nav –%>

Tags: , , ,

Launched new Twitter accounts

April 30th, 2009 by Brian filed in play has 1 comment

I was inspired by this story in the Anchorage Daily News about a woman that started @AKBadDrivers “who started describing bad drivers on her personal Twitter feed from her spot in the passenger seat as she commuted in from Wasilla.” The simple brilliance of this usage of Twitter really struck me.

With tweets like

  • silver jeep, FLB 272. thanks for cutting me off not once but TWICE when you were going straight in turn only lanes prick.
  • To the girl in the Green Explorer outside Muldoon Fred Meyers this afternoon: Sorry my U-turn scared you. My bad :-(
  • white Nissan pu FEL 365 hang up ur cell phone pay attention 2 ur driving. ur lane ended & you nearly hit me merging 2 mine.

I wanted a piece of that action!

So I started @NJBadDrivers and @CTBadDrivers. Then I realized I better grab @NYBadDrivers to complete the tri-state trifecta. Imitation is the sincerest form, right ? After talking to my buddy who moved up to Alaska, he said that people really are crazy drivers up there, but come on, can they really be worse than New York, Connecticut or New Jersey drivers? (Admittedly, I’ve had licenses in all 3 states.)

I’m running with Christopher Finke’s slick little python application called retweet on my Dreamhost account. If Chrstopher’s name sounds familiar (it should!) it’s cuz he’s the mastermind behind ScribeFire and the TwitterBar Firefox add-on. For those interested in running retweet on Dreamhost, be sure to use python2.5 as it already has the sqlite3 library configured. Once I figured that out, I cron’d it and it’s been working like a charm.

So how’s it work? All you need to do is send a message to one of the accounts via Twitter and it will get retweeted by the bot. Just say “@NJBadDrivers does anyone around here know how to function at a four way stop???” or “@NYBadDrivers srsly people, traffic circles are not that difficult”.

I wonder… is reserving Twitter names the new domain squatting? If so, would it be called twatting?

Tags: , , , ,