Friday, December 28, 2012

corejava: File Upload example using spring

corejava: File Upload example using spring: Below is a running example to upload a file using spring , please add the below lone in your application-context file to add CommonsMulti...

Spring Security Implement Logout Handler

In your applicationContext-security.xml file add the success-handler like below
 <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


Below is a running example to upload a file using spring , please add the below lone in your application-context file to add CommonsMultipartResolver


   <bean id="multipartResolver"

        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

Write your JSP page like below , Please do add HTML code if you want to run it as a seprate page

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div  xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:page="urn:jsptagdir:/WEB-INF/tags/form" xmlns:table="urn:jsptagdir:/WEB-INF/tags/form/fields" version="2.0">

         <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>

Write the controller to handle requests related to file upload
package com.aramco.peasd.dbp.web;

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;

@Controller
@RequestMapping("/fileupload")
public class FileUploadController {
       @ModelAttribute
       public void ajaxAttribute(WebRequest request, Model model) {

//            model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));

       }
       @RequestMapping(method=RequestMethod.GET)
       public void fileUploadForm() {
  

       }
@RequestMapping(method=RequestMethod.POST,produces =
"text/html"
)
       public String processUpload(@RequestParam MultipartFile file, Model model) throws IOException {
        

              model.addAttribute(  "message", "File '" + file.getOriginalFilename() + "' uploaded successfully");
              model.addAttribute("successfull", "Y");
              //**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";
//this will render to a page called showFileUpload.jspx
       }
       @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
 
 
 
<?xml version="1.0" encoding="UTF-8"?>
    <wls:weblogic-version>10.3.3</wls:weblogic-version>
 <wls:container-descriptor>
  <wls:index-directory-enabled>false</wls:index-directory-enabled>
  <!-- prefer-application-packages> <package-name>javax.persistence.spi.*</package-name>
   </prefer-application-packages -->
  <wls:prefer-application-packages>
   <wls:package-name>antlr.*</wls:package-name>
   <wls:package-name>org.apache.commons.*</wls:package-name>
   <wls:package-name>org.apache.xmlbeans.*</wls:package-name>
   <wls:package-name>org.springframework.*</wls:package-name>
   <wls:package-name>org.hibernate.*</wls:package-name>
 
   <wls:package-name>org.hibernate.validator.*</wls:package-name>
 
   <wls:package-name>javax.persistence.*</wls:package-name>
   <wls:package-name>org.joda.*</wls:package-name>
  </wls:prefer-application-packages>
 </wls:container-descriptor>
</wls:weblogic-web-app>
 

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