[Solved] spring-security-web classes are not available. You need these to use filter-chain-map

Recently I downloaded the latest Spring Tool Suite (STS) with Java 8 SE support. My intention was to give it a try to Stream API on my personal project. An error was shown on the spring security configuration XML once I imported it to the STS.

The STS displayed following configuration error:

“ spring-security-web classes are not available. You need these to use <filter- chain-map>”

I visited  the Google search page, googled the error. This was what I found.

FilterChainProxy class (org.springframework.security.web.FilterChainProxy) of spring security artifact  imports few javax.servlet classes. The pom.xml didn’t have servlet library dependency in it.

I added following servlet dependency to it.


<dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>servlet-api</artifactId>
     <version>{version}</version>
 </dependency>

After maven install, the error was  gone, so I decided to initiate the app.

The console displayed following exception  long before I could see the index page:

“ServletDispatcher cannot be cast to Javax.servlet.Servlet exception”

Adding servlet-api artifact will include servlet JAR along with the container specific servlet libraries, which would load different servlet API class-loaders at run time and causes the above exception.  The servlet artifact is only needed at compilation in this case, so setting the scope to ‘provided’ will resolve the issue.

About dependency scope from Maven doc:

  • compile
    This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
  • provided
    This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

 

The final dependency definition for javax.servlet with ‘provided’ scope


<dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>servlet-api</artifactId>
     <version>3.0.1</version>
     <scope>provided</scope>
 </dependency>

 

 

 

Advertisements

Read Property Value From Properties File In Spring

The Java properties file can be used to store project configurations or settings.

constant.properties

ui.defaultIconUrl=graph.facebook.com/ek.pranav/picture

Environment – Interface representing the environment in which the current application is running.

@Configuration – Configuration classes are candidates for component scanning (typically using Spring XML’s <context:component-scan/> element) and therefore may also take advantage of @Autowired/@Inject at the field and method level (but not at the constructor level).

@PropertySource – Adds property sources to the enclosing Environment.

 

The values in the properties can be accessed by injecting Environment object to the configuration class.

ConstantPropertyInjector.java


@Configuration
@PropertySource("classpath:META-INF/constant.properties")
public class ConstantPropertyInjector {

@Autowired Environment env;

   @Bean
   public ConstantImageHolder getConstantProperty(){
     ConstantImageHolder constant = new ConstantImageHolder ();
     constant.setDefaultIcon(env.getProperty("ui.defaultIconUrl"));
     return constant;
   }

}

 

Parse an XML document using SAX parser

Simple API for XML(SAX) is an event driven parser which reads XML step by step. SAX Parser is faster and uses less memory than DOM parser.

Event handling methods used:
startElement() – Receive notification of the start of an element.
endElement() – Receive notification of the end of an element.
characters() – Receive notification of character data inside an element.

XML document to be parsed
movie_list.xml

<movies>
    <movie>
        <name>The Terminal</name>
        <image>http://.../the_terminal.jpg</image>
        <description>terminal foo foo foo</description>
    </movie>
    <movie>
        <name>Shutter Island</name>
        <image>http://.../shutter_island.png</image>
        <description>shutter foo foo foo </description>
    </movie>
    <movie>
        <name>Old Boy</name>
        <image>http://.../old_boy.gif</image>
        <description>old foo foo foo </description>
    </movie>
</movies>

Create Model object
Movie.java

public class Movie {

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getImgURL() {
		return imgURL;
	}

	public void setImgURL(String imgURL) {
		this.imgURL = imgURL;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	private String name;
	private String imgURL;
	private String description;

	@Override
	public String toString() {
		return this.name + " " + this.imgURL + " " + this.description;
	}

}

Extend DefaultHandler and override above mentioned event handling methods.
MovieHandler.java

public class MovieHandler extends DefaultHandler {
    List<Movie> movieList = new ArrayList<Movie>();
    Movie movie = null;
    String data = null;
 
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
               // When start tag of 'movie' element found, create an object and add it to the list
        switch (qName) {
        case "movie":
 
            movie = new Movie();
            movieList.add(movie);
            break;
 
        }
    }
 
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
 
        switch (qName) {
        case "name":
            movie.setName(data);
            break;
        case "description":
            movie.setDescription(data);
            break;
        case "image":
            movie.setImgURL(data);
            break;
 
        }
    }
 
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        data = String.copyValueOf(ch, start, length).trim();
 
    }
 
}

Set up the parser and get it started
MovieSaxParser.java

	
public class MovieSaxParser {
 
    static final Logger LOG = Logger.getLogger(MovieSaxParser.class);
    String static fileLocation = "/home/prnv/movie_list.xml";
 
    public static void main(String[] args) {
        try {
            SAXParserFactory saxFactory = SAXParserFactory.newInstance();
            SAXParser parser = saxFactory.newSAXParser();
            MovieHandler handler = new MovieHandler();
            parser.parse(fileLocation, handler);
            for (Movie movie : handler.movieList) {
                System.out.println(movie);
            }
        } catch (ParserConfigurationException | SAXException | IOException e) {
            LOG.error("Exception ", e);
 
        }
 
    }
}

Backup files in EC2 Instance to Amazon S3

EC2 Instance : Ubuntu Server 12.04 LTS
Tool : s3cmd

Install s3cmd:

sudo apt-get install s3cmd

Configure s3cmd for managing data on S3:
Get the access key, secret key from your AWS account prior to this step.

s3cmd --configure

Listing buckets:

s3cmd ls

Create a bucket:

s3cmd mb s3://bucket_name

Upload files to S3:

s3cmd put -r ~/backup_directory/ s3://bucket_name/

Synchronize files:

s3cmd sync -r ~/backup_directory/ s3://bucket_name/

Download files from a bucket:

s3cmd get -r s3://bucket_name/ ~/my_s3_downloads/

Delete files in a bucket:

s3cmd del -r s3://bucket_name/

Remove a bucket:

s3cmd rb -f s3://bucket_name

Write a shell script to synchronize files in the EC2 Instance:
backup_sync_ec2_s3.sh

#!/bin/bash
s3cmd sync -r ~/backup_directory/ s3://bucket_name/

Edit crontab file:

crontab -e

Schedule script:
Add cronjob to trigger automated backup everyday at 23:59 hrs

59 23 * * * sh ~/backup_sync_ec2_s3.sh