Skip to Content
0
Former Member
May 30, 2008 at 02:09 PM

NullPointer Exception with JSPDynpage

26 Views

Hi Gurus,

I have built this JSPDynpage that uploads a document from the desktop to a KM repository.

When I run the iView the JSP loads beautifully and I can see the "browse" and "upload" buttons. When I select a file from my desktop and click the upload button, it throws a java.lang.NullPointerException at

IUser epUser=(IUser)request.getUser().getUser();

I understand that IUser and getUser() are depricated but still they should work. Isnt it ?

Following is my code for JSPDynpage:


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.sapportals.htmlb.event.Event;
import com.sapportals.htmlb.page.DynPage;
import com.sapportals.htmlb.page.PageException;
import com.sapportals.htmlb.rendering.IPageContext;
import com.sapportals.htmlb.rendering.PageContextFactory;
import com.sapportals.htmlb.FileUpload;
import com.sapportals.portal.htmlb.page.JSPDynPage;
import com.sapportals.portal.htmlb.page.PageProcessorComponent;
import com.sapportals.portal.prt.component.*;
import com.sapportals.portal.prt.session.IUserContext;
import com.sapportals.wcm.repository.AccessDeniedException;
import com.sapportals.wcm.repository.Content;
import com.sapportals.wcm.repository.ICollection;
import com.sapportals.wcm.repository.IResource;
import com.sapportals.wcm.repository.NotSupportedException;
import com.sapportals.wcm.repository.ResourceContext;
import com.sapportals.wcm.repository.ResourceException;
import com.sapportals.wcm.repository.ResourceFactory;
import com.sapportals.wcm.util.uri.RID;
import com.sapportals.portal.security.usermanagement.IUser;


public class FileUploadUtility extends PageProcessorComponent {

  public DynPage getPage(){
    return new FileUploadUtilityDynPage();
  }

  public static class FileUploadUtilityDynPage extends JSPDynPage{
  
    private fileUpload fileUploadBean = null;
    
    
	protected IPortalComponentRequest request;
	protected IPortalComponentResponse response;
	protected IPortalComponentSession session; 
	protected IPortalComponentContext context;
	protected IPortalComponentProfile profile;
	protected String userId;
	
	/*
	private final static int INITIAL_STATE = 0;    
	private final static int OUTPUT_STATE = 1;
	private final static int ERROR_STATE = 2;  
	private int state = INITIAL_STATE;
    */
  
    public void doInitialization(){
		request = (IPortalComponentRequest) this.getRequest();
		session = ((IPortalComponentRequest)getRequest()).getComponentSession();
		context = request.getComponentContext();
	    
	    
		profile = context.getProfile();
		IUserContext userContext = request.getUser();
		userId = userContext.getUserId();
		System.err.println("berry + Getting userID " + userId);			
		fileUploadBean = new fileUpload();
		session.putValue("fileUploadBean", fileUploadBean);
		//uploadUtility();
    }

    public void uploadUtility(){
    	
		String mimetype;
		String resourceName;
		String repository;
		FileInputStream sourceFileInput = null;
		
		
		System.err.println("berry + Getting Iuser");
		IUser epUser=(IUser)request.getUser().getUser();
		System.err.println("epUser - " + epUser);
		ResourceContext ctx = new ResourceContext(epUser);
		repository = "/documents/";
		RID rid=RID.getRID(repository);
		IPageContext context = PageContextFactory.createPageContext(request,response);

	//	PageContext context = PageContextFactory.createPageContext(request, response);
		Event event=context.getCurrentEvent();
		
		if(null!=event)
		{
			String event_name=event.getAction();
			if(null!=event_name && event_name.trim().equalsIgnoreCase("upload_file"))
				{
				//FileUpload fileUpload = (FileUpload)context.getComponentForId("fileupload");
				FileUpload fileUpload = (FileUpload)context.getComponentForId("fileupload");
				
					if(null !=fileUpload && null!=fileUpload.getFile())
							{
							mimetype = fileUpload.getFile().getContentType();
							resourceName = fileUpload.getFile().getFileName();
							//sourceFileInput = new FileInputStream(fileUpload.getFile().getFile().getAbsolutePath());
							
							try {
								sourceFileInput = new FileInputStream(fileUpload.getFile().getFile().getAbsolutePath());
							} catch (FileNotFoundException fnf) {
								System.err.print("FileNotFoundException Occured " + fnf.toString()); 
							}
							
							
							Content content = new Content(sourceFileInput, mimetype, -1L);
							//IResource resource = (ResourceFactory.getInstance().getResource(rid, ctx));
							
							IResource resource = null;
							try {
								resource =(IResource) (ResourceFactory.getInstance().getResource(rid, ctx));
							} catch (ResourceException resexp) {
								System.err.print("Resource Exception Occured for IResource" + resexp.toString()); 					}
							
							
							ICollection aCollection=(ICollection)resource;
							//IResource newResource = aCollection.createResource(resourceName, null, content);
							try {
								IResource newResource = (IResource) aCollection.createResource(resourceName, null, content);
							} catch (NotSupportedException e2) {
								System.err.print("Not Supported Exception Occured" + e2.toString());
							} catch (AccessDeniedException e2) {
								System.err.print("Access Denied Exception Occured" + e2.toString());
							} catch (ResourceException e2) {
								System.err.print("Resource Exception Occured for ICollection" + e2.toString()); 	
							}//end of catch
				}//end of innermost if
		}//end of 2nd if
	}//end of 1st If

	
		com.sapportals.portal.prt.resource.IResource res = request.getResource(com.sapportals.portal.prt.resource.IResource.JSP,"jsp/fileupload.jsp");
		request.dispatchRequest(res,response);
	
    	
}// end of uploadUtil


    public void doProcessAfterInput() throws PageException {
		uploadUtility();
    }

    public void doProcessBeforeOutput() throws PageException {
      this.setJspName("File_Upload.jsp");
    }
  }
}