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.undertow;
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 Undertow 2+ specific push builder API
032 * 
033 * @author Tobias Soloschenko
034 */
035public class UndertowPushBuilder implements PushBuilder
036{
037        private static final Logger LOG = LoggerFactory.getLogger(UndertowPushBuilder.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                io.undertow.servlet.spec.HttpServletRequestImpl undertowRequest = (io.undertow.servlet.spec.HttpServletRequestImpl) httpRequest;
045                // Added explicit cast here to ensure this is the implementation of undertow
046                io.undertow.servlet.spec.PushBuilderImpl pushBuilder = (io.undertow.servlet.spec.PushBuilderImpl)undertowRequest.newPushBuilder();
047                if (pushBuilder != null)
048                {
049                        for (PushItem pushItem : pushItems)
050                        {
051                                pushBuilder.path(pushItem.getUrl());
052                                pushItem.getHeaders().entrySet().stream().forEach(pushHeader -> {
053                                        String key = pushHeader.getKey();
054                                        PushItemHeaderValue value = pushHeader.getValue();
055                                        if(value.getOperation() == HeaderOperation.ADD){
056                                                pushBuilder.addHeader(key, value.getValue());
057                                        }else{
058                                                pushBuilder.setHeader(key, value.getValue());
059                                        }
060                                });
061                                pushBuilder.push();
062                        }
063                }
064                else
065                {
066                        LOG.warn("Attempted to use HTTP2 Push but it is not supported for the current request: {}!",
067                                        httpRequest);
068                }
069        }
070}