com.exactpro.sf.services.http.handlers.BaseHTTPMatcherHandlerDecode Maven / Gradle / Ivy
/******************************************************************************
* Copyright 2009-2022 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.exactpro.sf.services.http.handlers;
import static com.exactpro.sf.common.messages.MessagePropertiesConstants.MESSAGE_TYPE_PROPERTY;
import static com.exactpro.sf.common.messages.MessagePropertiesConstants.PROTOCOL_PROPERTY;
import static com.exactpro.sf.common.messages.MetadataExtensions.setMessageProperties;
import static com.exactpro.sf.common.messages.structures.StructureUtils.getAttributeValue;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicReference;
import org.jetbrains.annotations.NotNull;
import com.exactpro.sf.common.messages.IMessage;
import com.exactpro.sf.common.messages.IMessageFactory;
import com.exactpro.sf.common.messages.MsgMetaData;
import com.exactpro.sf.common.messages.structures.IDictionaryStructure;
import com.exactpro.sf.common.messages.structures.IMessageStructure;
import com.exactpro.sf.common.util.ICommonSettings;
import com.exactpro.sf.services.http.HTTPClient;
import com.exactpro.sf.services.http.HTTPClientSettings;
import com.exactpro.sf.services.http.HTTPMessageHelper;
import com.exactpro.sf.services.http.ResponseInformation;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author sergey.smirnov
*
*/
public class BaseHTTPMatcherHandlerDecode extends MessageToMessageDecoder {
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName() + "@" + Integer.toHexString(hashCode()));
protected final IDictionaryStructure dictionary;
protected final HTTPClientSettings settings;
protected final IMessageFactory msgFactory;
protected final String clientName;
protected final Semaphore channelBusy;
protected final AtomicReference cookie;
private final Queue queue;
private final Map errorsMapping = new HashMap<>();
/**
* @param dictionary
* @param settings
* @param msgFactory
* @param clientName
* @param queue contains information about the next expected response
* @param channelBusy
* @param cookie
*/
public BaseHTTPMatcherHandlerDecode(IDictionaryStructure dictionary, ICommonSettings settings, IMessageFactory msgFactory, String clientName,
Queue queue, Semaphore channelBusy, AtomicReference cookie) {
this.dictionary = dictionary;
this.settings = (HTTPClientSettings)settings;
this.msgFactory = msgFactory;
this.clientName = clientName;
this.queue = queue;
this.channelBusy = channelBusy;
this.cookie = cookie;
for(IMessageStructure messageStructure : dictionary.getMessages().values()) {
Integer errorCode = getAttributeValue(messageStructure, HTTPMessageHelper.ERROR_CODE_ATTRIBUTE);
if (errorCode != null) {
errorsMapping.put(errorCode, messageStructure.getName());
}
}
}
@Override
protected void decode(ChannelHandlerContext handlerContext, FullHttpResponse msg, List