Following Redirects in Jersey REST Client

There is a method in com.sun.jersey.api.client.Client that causes the Client to follow any redirections:

 /**
  * Set if redirection should be performed or not.
  *
  * This method is the functional equivalent to setting the property
  * {@link ClientConfig#PROPERTY_FOLLOW_REDIRECTS} on the property bag
  * returned from {@link #getProperties}
  *
  * @param redirect if true then the client will automatically redirect
  *        to the URI declared in 3xx responses.
  */
 void setFollowRedirects(Boolean redirect)

WordPress and MySQL

<?php
define('DB_NAME', 'username');
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'sqlhost');
 
$table_prefix  = 'wp_';
 
$server = DB_HOST;
$loginsql = DB_USER;
$passsql = DB_PASSWORD;
$base = DB_NAME;
?>
CREATE DATABASE username;
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER
   ON username.* TO username@webhost IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

Bug in HTTPBasicAuthFilter

com.sun.jersey.api.client.filter.HTTPBasicAuthFilter incorrectly pads the Base64 encoded strings with null characters, instead of ‘=’. jersey-client-1.0.3.jar

It might be better to use a pre-exisiting Base64 library. Like the one in CommonsCodec.

public class HttpBasicFilter extends ClientFilter
{
   private static final String c_base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
                                              "abcdefghijklmnopqrstuvwxyz0123456789+/";
 
   private final String m_authentication;
 
   /**
    * Adds an authentication header using a username and password
    *
    * @param username the user name to send
    * @param password the passowrd to send
    */
   public HttpBasicFilter(final String username, final String password)
   {
      m_authentication = "Basic " + encode(username + ":" + password);
   }
 
   private String encode(final String string)
   {
      byte[] bytes = getBytes(string);
      final int padding = (3 - (bytes.length % 3)) % 3;
 
      bytes = zeroPad(bytes.length + padding, bytes);
 
      final StringBuilder encoded = new StringBuilder();
      for (int i = 0; i &lt; bytes.length; i += 3)
      {
         final int threeBytes = (bytes[i] &lt;&lt; 16) + (bytes[i + 1] &lt;&lt; 8) + bytes[i + 2];
         encoded.append(c_base64code.charAt((threeBytes &gt;&gt; 18) &amp; 0x3f)).
                 append(c_base64code.charAt((threeBytes &gt;&gt; 12) &amp; 0x3f)).
                 append(c_base64code.charAt((threeBytes &gt;&gt; 6) &amp; 0x3f)).
                 append(c_base64code.charAt(threeBytes &amp; 0x3f));
      }
      return encoded.substring(0, encoded.length() - padding) + "==".substring(0, padding);
   }
 
   private byte[] getBytes(final String string)
   {
      byte[] bytes;
      try
      {
         bytes = string.getBytes("UTF-8");
      }
      catch (final UnsupportedEncodingException e)
      {
         g_logger.log(Level.WARNING, "unable to decode string as UTF-8", e);
         bytes = string.getBytes();
      }
      return bytes;
   }
 
   private byte[] zeroPad(final int length, final byte[] bytes)
   {
      final byte[] padded = new byte[length];
      System.arraycopy(bytes, 0, padded, 0, bytes.length);
      return padded;
   }
 
   /**
    * {@inheritDoc}
    */
   @Override
   public ClientResponse handle(final ClientRequest request) throws ClientHandlerException
   {
      if (!request.getMetadata().containsKey(HttpHeaders.AUTHORIZATION))
      {
         request.getMetadata().add(HttpHeaders.AUTHORIZATION, m_authentication);
      }
      return getNext().handle(request);
   }
}

Custom AuthenticationProvider in Spring Security

In your application context you need

<beans:beans   xmlns="http://www.springframework.org/schema/security"
               xmlns:beans="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.springframework.org/schema/beans
                                   http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                                   http://www.springframework.org/schema/security
                                   http://www.springframework.org/schema/security/spring-security-2.0.4.xsd"> 
   <http>
      [...]
   </http>
   <beans:bean id="myAuthenticationProvider"
                       class="com.example.security.MyAuthenticationProvider">
      <custom-authentication-provider/>
   </beans:bean>

If you are using username and password authentication, your bean can then extend the abstract base class that comes with Spring:

org.springframework.security.providers.dao.AbstractUserDetailsAuthenticationProvider

Struts2 and Spring Integration

It is possible to use Spring to create Struts2 actions. Here are some snippets which make it work.

in your web.xml

<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>
<filter>
   <filter-name>struts2</filter-name>
   <filter-class>
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   </filter-class>
</filter>
<filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

In your struts.xml

<constant name="struts.objectFactory" value="spring" />

In your pom.xml

<dependency>
   <groupId>org.apache.struts</groupId>
   <artifactId>struts2-core</artifactId>
   <version>2.1.6</version>
</dependency>
<dependency>
   <groupId>org.apache.struts</groupId>
   <artifactId>struts2-spring-plugin</artifactId>
   <version>2.1.6</version>
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring</artifactId>
   <version>2.5.5</version>
</dependency>