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.servlet4;
018
019import javax.servlet.http.HttpServletRequest;
020
021import org.apache.wicket.http2.markup.head.PushBuilder;
022import org.apache.wicket.http2.markup.head.PushItem;
023import org.apache.wicket.http2.markup.head.PushItemHeaderValue;
024import org.apache.wicket.http2.markup.head.PushItemHeaderValue.HeaderOperation;
025import org.apache.wicket.request.Request;
026import org.apache.wicket.request.cycle.RequestCycle;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * Allows to push resources with the Servlet 4 specific push builder API
032 * 
033 * @author Tobias Soloschenko
034 */
035public class Servlet4PushBuilder implements PushBuilder
036{
037        private static final Logger LOG = LoggerFactory.getLogger(Servlet4PushBuilder.class);
038
039        @Override
040        public void push(HttpServletRequest httpServletRequest, PushItem... pushItems)
041        {
042                Request request = RequestCycle.get().getRequest();
043                HttpServletRequest httpRequest = (HttpServletRequest) request.getContainerRequest();
044                javax.servlet.http.PushBuilder pushBuilder = httpRequest.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}