Java Code Examples for javax.servlet.http.HttpServletRequest#newPushBuilder()

The following examples show how to use javax.servlet.http.HttpServletRequest#newPushBuilder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServerPush.java    From javaee8-cookbook with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    PushBuilder pb = request.newPushBuilder();
    if (pb != null) {
        pb.path("images/javaee-logo.png")
          .addHeader("content-type", "image/png")
          .push();
    }

    try (PrintWriter writer = response.getWriter();) {
        StringBuilder html = new StringBuilder();
        html.append("<html>");
        html.append("<center>");
        html.append("<img src='images/javaee-logo.png'><br>");
        html.append("<h2>Image pushed by ServerPush</h2>");
        html.append("</center>");
        html.append("</html>");
        writer.write(html.toString());
    }
}
 
Example 2
Source File: PushFilter.java    From javaee8-cookbook with Apache License 2.0 6 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain)
        throws IOException, ServletException {
    
    HttpServletRequest httpReq = (HttpServletRequest)request;
    PushBuilder builder =  httpReq.newPushBuilder();
    
    if (builder != null){
        builder
            .path("resources/javaee-logo.png")
            .path("resources/style.css")
            .path("resources/functions.js")
            .push();            
        System.out.println("Resources pushed");
    }

    chain.doFilter(request, response);
    
}
 
Example 3
Source File: ServletRequestMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
public static Object resolvePushBuilder(HttpServletRequest request, Class<?> paramType) {
	PushBuilder pushBuilder = request.newPushBuilder();
	if (pushBuilder != null && !paramType.isInstance(pushBuilder)) {
		throw new IllegalStateException(
				"Current push builder is not of type [" + paramType.getName() + "]: " + pushBuilder);
	}
	return pushBuilder;

}
 
Example 4
Source File: ServletRequestMethodArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
public static Object resolvePushBuilder(HttpServletRequest request, Class<?> paramType) {
	PushBuilder pushBuilder = request.newPushBuilder();
	if (pushBuilder != null && !paramType.isInstance(pushBuilder)) {
		throw new IllegalStateException(
				"Current push builder is not of type [" + paramType.getName() + "]: " + pushBuilder);
	}
	return pushBuilder;

}
 
Example 5
Source File: ObtainPushBuilder.java    From Java-EE-8-Sampler with MIT License 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    PushBuilder pushBuilder = request.newPushBuilder();

    if (pushBuilder != null) {
        pushBuilder.path("images/hero-banner.jpg").push();
        pushBuilder.path("css/menu.css").push();
        pushBuilder.path("js/marquee.js").push();
    }

    getServletContext().getRequestDispatcher("/duke.jsp").forward(request, response);

}
 
Example 6
Source File: ServerPushServlet.java    From javaee8-cookbook with Apache License 2.0 4 votes vote down vote up
private void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException{
    String usePush = request.getParameter("usePush");
    
    if ("true".equalsIgnoreCase(usePush)){
        PushBuilder pb = request.newPushBuilder();
        if (pb != null) {
            for(int row=0; row < 5; row++){
                for(int col=0; col < 8; col++){
                    pb.path("image/keyboard_buttons/keyboard_buttons-" + row + "-" + col + ".jpeg")
                      .addHeader("content-type", "image/jpeg")
                      .push();
                }
            }
        }
    }

    try (PrintWriter writer = response.getWriter()) {
        StringBuilder html = new StringBuilder();
        html.append("<html>");
        html.append("<center>");
        
        html.append("<table cellspacing='0' cellpadding='0' border='0'>");

        for(int row=0; row < 5; row++){
            html.append("  <tr>");
            for(int col=0; col < 8; col++){
                html.append("    <td>");
                html.append("<img src='image/keyboard_buttons/keyboard_buttons-" + row + "-" + col + ".jpeg' style='width:100px; height:106.25px;'>");                    
                html.append("    </td>");            
            }
            html.append("  </tr>");            
        }
        
        html.append("</table>");
        
        html.append("<br>");
        
        if ("true".equalsIgnoreCase(usePush)){
            html.append("<h2>Image pushed by ServerPush</h2>");
        } else{
            html.append("<h2>Image loaded using HTTP/1.0</h2>");
        }
        
        html.append("</center>");
        html.append("</html>");
        writer.write(html.toString());
    }
}