Showing posts with label servlet. Show all posts
Showing posts with label servlet. Show all posts

Wednesday, 29 February 2012

How to mock http servlet with jetty to test some http-post,http-get in unit test?

Solution:
1. Write you own mock servlet
public class MockServlet extends HttpServlet {
    private String servletData;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        contextService(req, resp);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private void contextService(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
        servletData = IOUtils.toString(req.getInputStream());

        resp.setContentType("text/html");
        resp.setStatus(HttpServletResponse.SC_OK);
        resp.getWriter().print("Response from mock server: Submit is success.");
    }

    public String getServletData() {
        return servletData;
    }
}

2. define mock beans in spring application context (servlet and jetty server)
2.1 servlet:

<bean id="mockServlet" class="my.MockServlet"/>

2.2 jetty server

        
            
                
                    <property name="host" value="localhost"/>
                    <property name="port" value="8900"/>
                
            
        
        
            
                
                    <property name="contextPath" value="/"/>
                    
                        <bean class="org.mortbay.jetty.servlet.SessionHandler">
                    
                    <property name="resourceBase" value="c:/temp"/>
                    
                        
                            
                            
                                
                                    
                                    
                                        <property name="name" value="defaultServlet"/>
                                        
                                            <bean class="org.mortbay.jetty.servlet.DefaultServlet"/>
                                        
                                        
                                            
                                                <entry key="resourceBase" value="./"/>
                                            
                                        
                                    
                                    
                                        <property name="name" value="mockServlet"/>
                                        <property name="servlet" ref="mockServlet"/>
                                        
                                            
                                                <entry key="resourceBase" value="./"/>
                                            
                                        
                                    
                                
                            
                            
                                
                                    
                                        
                                            
                                                /
                                            
                                        
                                        <property name="servletName" value="defaultServlet"/>
                                    
                                    
                                        
                                            
                                                /test/myAction
                                            
                                        
                                        <property name="servletName" value="mockCharityServlet"/>
                                    
                                
                            
                        
                    
                
                
                
            
        
    


3. You Http Client code

final HttpPost post = new HttpPost("http://127.0.0.1:9800/test/myAction");
final HttpResponse resp = hc.execute(post);
final HttpEntity respContent = resp.getEntity();
final InputStream is = respContent.getContent();
final StringWriter strWr = new StringWriter();
IOUtils.copy(is, strWr);
return strWr.toString();

4. Junit
String expected = "Response from mock server: Submit is success.";
String actual = mockClient.submit();
assertEquals(expected, actual);

Sunday, 13 November 2011

how do you download a file from your web application? - From book java/j2ee interview

Files can be downloaded from a web application by using the right combination of
headers.
//set the header to a non-standard value for attachments to be saved by the browser with the
//Save-As dialog so that it is unrecognized by the browsers because often browsers try to do
//something special when they recognize the content-type.
response.setContentType(“application/x-download”);
//use Content-Disposition “attachment” to invoke “Save As” dialog and “inline” for displaying
//the file content on the browser without invoking the “Save As” dialog.
response.setHeader(“Content-disposition”, “attachment;filename=” + fileName);

Solution2:
@RequestMapping(value = "get_zip_archive")
public ResponseEntity<byte[]> getZippedLogFilesArchive(@RequestParam("tempFileName") String tempFileName) throws IOException {
    final File zipFile = new File(TEMP_DIR, tempFileName);
    try {
        final HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Disposition", "attachment; filename=" + tempFileName);
        headers.setContentLength(zipFile.length());
        return new ResponseEntity<byte[]>(Files.toByteArray(zipFile), headers, HttpStatus.OK);
    } finally {
        Files.deleteRecursively(zipFile);
    }
}



--jsp page--
function downloadReport(id) {
 window.location = "downloadReportData?id=" + id;
}