javax.servlet.http.PushBuilder Java Examples

The following examples show how to use javax.servlet.http.PushBuilder. 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: 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 #2
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 #3
Source File: PushController.java    From tutorials with MIT License 5 votes vote down vote up
@GetMapping(path = "/demoWithPush")
public String demoWithPush(PushBuilder pushBuilder) {
    if (null != pushBuilder) {
        pushBuilder.path("resources/logo.png").push();
    }
    return "demo";
}
 
Example #4
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 #5
Source File: PushServlet.java    From ee8-sandbox with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException {
    PushBuilder pushBuilder = req.newPushBuilder().
        path("main.css");
    pushBuilder.push();
    res.getWriter().println("<html><head><title>HTTP2 Test</title><link rel=\"stylesheet\" href=\"main.css\"></head><body>Hello Servlet Push!!!</body></html>");
}
 
Example #6
Source File: HttpServletRequestImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public PushBuilder newPushBuilder() {
    if(exchange.getConnection().isPushSupported()) {
        return new PushBuilderImpl(this);
    }
    return null;
}
 
Example #7
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 #8
Source File: ServletRequestMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unused")
public void supportedParams(ServletRequest p0,
							MultipartRequest p1,
							HttpSession p2,
							Principal p3,
							Locale p4,
							InputStream p5,
							Reader p6,
							WebRequest p7,
							TimeZone p8,
							ZoneId p9,
							HttpMethod p10,
							PushBuilder p11) {
}
 
Example #9
Source File: ServletRequestMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	resolver = new ServletRequestMethodArgumentResolver();
	mavContainer = new ModelAndViewContainer();
	servletRequest = new MockHttpServletRequest("GET", "");
	webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());

	method = getClass().getMethod("supportedParams", ServletRequest.class, MultipartRequest.class,
			HttpSession.class, Principal.class, Locale.class, InputStream.class, Reader.class,
			WebRequest.class, TimeZone.class, ZoneId.class, HttpMethod.class, PushBuilder.class);
}
 
Example #10
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 #11
Source File: HttpServletRequestImpl.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public PushBuilder newPushBuilder() {
    if (exchange.isPushSupported()) {
        return new PushBuilderImpl(this);
    }
    return null;
}
 
Example #12
Source File: ServletRequestMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unused")
public void supportedParams(ServletRequest p0,
							MultipartRequest p1,
							HttpSession p2,
							Principal p3,
							Locale p4,
							InputStream p5,
							Reader p6,
							WebRequest p7,
							TimeZone p8,
							ZoneId p9,
							HttpMethod p10,
							PushBuilder p11) {
}
 
Example #13
Source File: ServletRequestMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	resolver = new ServletRequestMethodArgumentResolver();
	mavContainer = new ModelAndViewContainer();
	servletRequest = new MockHttpServletRequest("GET", "");
	webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());

	method = getClass().getMethod("supportedParams", ServletRequest.class, MultipartRequest.class,
			HttpSession.class, Principal.class, Locale.class, InputStream.class, Reader.class,
			WebRequest.class, TimeZone.class, ZoneId.class, HttpMethod.class, PushBuilder.class);
}
 
Example #14
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());
    }
}
 
Example #15
Source File: PushBuilderImpl.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public PushBuilder removeHeader(String name) {
    headers.remove(name);
    return this;
}
 
Example #16
Source File: PushBuilderImpl.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public PushBuilder addHeader(String name, String value) {
    headers.add(name, value);
    return this;
}
 
Example #17
Source File: PushBuilderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public PushBuilder setHeader(String name, String value) {
    headers.put(new HttpString(name), value);
    return this;
}
 
Example #18
Source File: PushBuilderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public PushBuilder addHeader(String name, String value) {
    headers.add(new HttpString(name), value);
    return this;
}
 
Example #19
Source File: PushBuilderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public PushBuilder removeHeader(String name) {
    headers.remove(name);
    return this;
}
 
Example #20
Source File: PushBuilderImpl.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public PushBuilder setHeader(String name, String value) {
    headers.set(name, value);
    return this;
}