Friday, December 28, 2012
corejava: File Upload example using spring
Spring Security Implement Logout Handler
<logout logout-url="/resources/j_spring_security_logout" success-handler-ref="com.mycompany.security.SpringSecurityLogoutHandler" />
Create the Class which will be implemneting "org.springframework.security.web.authentication.logout.LogoutHandler" interface and in it's logout method do all the stuff you want at the time of logout.
package com.mycompany.security;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
public class SpringSecurityLogoutHandler implements LogoutHandler {
@Override
public void logout(HttpServletRequest request, HttpServletResponse arg1,
Authentication arg2) {
// Write your logout logic here
}
}
Spring MVC file upload example
<form id="fileuploadForm" action="fileupload" method="POST" enctype="multipart/form-data" class="cleanform">
<c:if test="${successfull==Y}">
<c:out value="File Was Successfully uploaded"></c:out>
</c:if>
<label for="file">File</label>
<input id="file" type="file" name="file" />
<p><button type="submit">Upload</button></p>
</form>
</div>
import java.io.IOException;
//import org.springframework.mvc.extensions.ajax.AjaxUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartFile;
public class FileUploadController {
@ModelAttribute
public void ajaxAttribute(WebRequest request, Model model) {
public void fileUploadForm() {
"text/html"
)
public String processUpload(@RequestParam MultipartFile file, Model model) throws IOException {
//**This byte array can then be sent to any content managment server to save the file
byte[] fileByteArray=file.getBytes();
//**Or you may want to save the file using java IO on a folder somewhere in you server
return "fileupload/showFileUpload";
@RequestMapping(params="fileupload")
public String showFileUploadPage(){
return "fileupload/showFileUpload";
}
}
Saturday, December 15, 2012
Deploy JPA2.0 application on weblogic10.3.3
Please note that in order to run your JPA2.0 application on weblogic10.3.3 which is JPA1.0 compliant you will have to rename your persistence.xml to something like foo.xml and mentione the name of this xml file in your applicationContext.xml as (I am using Spring here )
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property value="classpath:META-INF/foo.xml" name="persistenceXmlLocation"/>
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>
rename the persistenceUnit and dataSource according to the beans you have defined in your application
and you will have to define package exclusions in your weblogic.xml file as
I invested few days to resolve the problem , and sharing the solution hoping it might benefit someone someday.
Below are some related threads I created for this problem , there you will find the details of problems you can face and the recomended solutions , but the solution provided above is working for me now and is gurenteed :).
https://www.coderanch.com/t/598227/Spring/Create-update-operations-ROO-causing
http://www.coderanch.com/t/599976/BEA-Weblogic/Weblogic-load-PersistenceProvider-wrong-jar
http://stackoverflow.com/questions/13806457/weblogic-10-3-3-trying-to-load-org-eclipse-persistence-jpa-persistenceprovider-i/13898999#13898999
https://forums.oracle.com/forums/thread.jspa?threadID=2474374&stqc=true
If you want to understand why we have package exclusions in our weblogic.xml file then below blog will help you alot understanding class loading in weblogic and why we need the package exclusions , I don't want to repeat the same story here , and this guy has written a great blog on it already
http://middlewaremagic.com/weblogic/?p=6725
Thursday, November 22, 2012
spring get bean from applicationcontext
Monday, October 22, 2012
How to add Oracle JDBC driver in your Maven local repository
http://www.mkyong.com/maven/how-to-add-oracle-jdbc-driver-in-your-maven-local-repository/
Which in short guides to do following steps in order to add your JDBC driver to your Maven local repository
Run below mvn command
mvn install:install-file -Dfile=D:\app\mkyong\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar
of course you will have to replace the path to OJDBC jar file with your machine's path. Maven will show you a build success message. After that you will have to add following dependency to your project's pom.xml
<!-- ORACLE database driver --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0</version> </dependency>
Friday, October 19, 2012
Enabling spring security in your spring application
package mypackage;
And you will have to add AuthenticationManager in your applicationContext-security.xml file as below
User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String name = user.getUsername(); //get logged in username
@RequestMapping(value="/login", method = RequestMethod.GET)
public String printWelcome(ModelMap model, Principal principal ) {
String name = principal.getName(); //get logged in username
model.addAttribute("username", name);
return "hello";
}
Saturday, October 13, 2012
Weblogic BEA-149265 , Caused By: java.lang.ClassNotFoundException: HttpServlet
I have configured weblogic 10.3.3 server in eclipse and the application had no errors. It was running from inside eclipse like a charm.
Tuesday, September 11, 2012
Spring MVC from getting started to advance level
A very good introductory article of Spring MVC
http://blog.springsource.com/2011/01/04/green-beans-getting-started-with-spring-mvc/
Spring showcase
http://blog.springsource.org/2010/07/22/spring-mvc-3-showcase/
Thursday, August 30, 2012
Common mistakes done in applications which can give a hacker full knowledge of your database
Its a bad practice to throw the whole exception message on the client console , if the exception is a jdbc exception throwed because of an error in update, any error in trigger execution e.t.c the raw exception stack trace has a lot of information about the tables in database fields in it and business rules implenented which caused this exception. This is a lot for a hacker he can easily build a knowledge base based on these exceptions, validations and business rules implemented in your database, infact your whole database can be open to him.
Its therefor a bad practice to let the presentation layer see the raw jdbc exception stack trace instead application developers should try to develop the wrapper exceptions and should try to show only relevent exception data on presentation layer.
Note : "I recently had a chance to work with a very good consultant from Mcafee, he told me that this is one of the main reasons hackers can hack the most secured applications easily once they hv access to the application they try different operations to cause the exceptions and build their knowledge by looking into the different exception". This was a very good informative session where I learnt a lot I will be writing more blogs on it so keep your fingers crossed and wait for some more good blog posts: ).
Saturday, August 4, 2012
java.sql.SQLSyntaxErrorException: ORA-01722: invalid number
One possible reason for java.sql.SQLSyntaxErrorException: ORA-01722: invalid number and it's solution
Below is the query I was trying to execute
select * from a_table where bi_num= 60
Reason :
Solution:
Thursday, July 26, 2012
HTML Give border title in div
<div style="width:100%;">
<fieldset>
<legend style="color:blue;font-weight:bold;">Initiator Information</legend>
<table border="0" cellpadding="0" cellspacing="5" >
<tr>
<td width="146">User Id</td>
<td width="216"><label for="userId"></label>
<input type="text" name="userId" id="userId" /></td>
<td width="122">Name</td>
<td width="474"><input type="text" name="userName" id="userName" /></td>
</tr>
<tr>
<td>Department</td>
<td><label for="userDepartment"></label>
<input type="text" name="userDepartment" id="userDepartment" /></td>
<td>Division</td>
<td><label for="userDevision"></label>
<input type="text" name="userDevision" id="userDevision" /></td>
</tr>
</table>
</fieldset>
</div>
The above code will show the div in browser like below