001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.http2.markup.head.tomcat; 018 019import javax.servlet.http.HttpServletRequest; 020 021import org.apache.catalina.core.ApplicationPushBuilder; 022import org.apache.wicket.http2.markup.head.PushBuilder; 023import org.apache.wicket.http2.markup.head.PushItem; 024import org.apache.wicket.http2.markup.head.PushItemHeaderValue; 025import org.apache.wicket.http2.markup.head.PushItemHeaderValue.HeaderOperation; 026import org.apache.wicket.request.Request; 027import org.apache.wicket.request.cycle.RequestCycle; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031/** 032 * Allows to push resources with the Tomcat 8.5+ specific push builder API 033 */ 034public class Tomcat85PushBuilder implements PushBuilder 035{ 036 private static final Logger LOG = LoggerFactory.getLogger(Tomcat85PushBuilder.class); 037 038 @Override 039 public void push(HttpServletRequest httpServletRequest, PushItem... pushItems) 040 { 041 Request request = RequestCycle.get().getRequest(); 042 HttpServletRequest httpRequest = (HttpServletRequest) request.getContainerRequest(); 043 org.apache.catalina.connector.RequestFacade tomcatRequest = (org.apache.catalina.connector.RequestFacade) httpRequest; 044 ApplicationPushBuilder pushBuilder = (ApplicationPushBuilder) tomcatRequest.newPushBuilder(); 045 if (pushBuilder != null) 046 { 047 for (PushItem pushItem : pushItems) 048 { 049 pushBuilder.path(pushItem.getUrl()); 050 pushItem.getHeaders().entrySet().stream().forEach(pushHeader -> { 051 String key = pushHeader.getKey(); 052 PushItemHeaderValue value = pushHeader.getValue(); 053 if(value.getOperation() == HeaderOperation.ADD){ 054 pushBuilder.addHeader(key, value.getValue()); 055 }else{ 056 pushBuilder.setHeader(key, value.getValue()); 057 } 058 }); 059 pushBuilder.push(); 060 } 061 } 062 else 063 { 064 LOG.warn("Attempted to use HTTP2 Push but it is not supported for the current request: {}!", 065 httpRequest); 066 } 067 } 068}