Search in sources :

Example 1 with HttpDirection

use of com.att.aro.core.packetanalysis.pojo.HttpDirection in project VideoOptimzer by attdevsupport.

the class SessionManagerImpl method readFileAndPopulateRequestResponse.

private ArrayList<HttpRequestResponseInfo> readFileAndPopulateRequestResponse(Session session, ArrayList<HttpRequestResponseInfo> results, String filePath, PacketDirection packetDirection, HttpDirection httpDirection) throws IOException {
    String dataRead;
    long timeStamp = 0;
    HttpRequestResponseInfo rrInfo = null;
    int requestCount = 0;
    TreeMap<Double, PacketInfo> packetMap;
    BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
    Map<Double, PacketInfo> usedPackets = new HashMap<>();
    if (packetDirection == PacketDirection.UPLINK) {
        packetMap = new TreeMap<>(session.getAllPackets().stream().filter(packetInfo -> packetInfo.getDir().equals(PacketDirection.UPLINK)).collect(Collectors.toMap(PacketInfo::getTimeStamp, Function.identity(), (existing, replacement) -> existing)));
    } else {
        packetMap = new TreeMap<>(session.getAllPackets().stream().filter(packetInfo -> packetInfo.getDir().equals(PacketDirection.DOWNLINK)).collect(Collectors.toMap(PacketInfo::getTimeStamp, Function.identity(), (existing, replacement) -> existing)));
    }
    try {
        while ((dataRead = bufferedReader.readLine()) != null) {
            if (dataRead.length() > 0) {
                String comparisonString = "RequestTime: ";
                if (dataRead.startsWith(comparisonString)) {
                    ++requestCount;
                    timeStamp = Long.parseLong(dataRead.substring(comparisonString.length(), dataRead.length()));
                    continue;
                }
                rrInfo = initializeRequestResponseObject(dataRead, session, packetDirection);
                if (rrInfo != null) {
                    rrInfo.setTCP(true);
                    rrInfo.setRawSize(-1);
                    // Converting the System Time in Millis to Seconds and Microsecond format.
                    double time = ((double) timeStamp / 1000) + (((double) timeStamp % 1000) / 1000000.0);
                    // The math below allows the request time to have a start time relative to trace capture.
                    rrInfo.setTime(time - pcapTimeOffset);
                    // TODO: Will Review this after ARO22945-1645
                    if (packetMap.containsKey(rrInfo.getTime()) && !usedPackets.containsKey(rrInfo.getTime())) {
                        rrInfo.setFirstDataPacket(packetMap.get(rrInfo.getTime()));
                        usedPackets.put(rrInfo.getTime(), packetMap.get(rrInfo.getTime()));
                    } else {
                        Map.Entry<Double, PacketInfo> lowKey = packetMap.floorEntry(rrInfo.getTime());
                        Map.Entry<Double, PacketInfo> highKey = packetMap.ceilingEntry(rrInfo.getTime());
                        if (lowKey != null) {
                            setFirstAndLastDataPacket(results, usedPackets, rrInfo, packetMap, lowKey);
                        } else if (highKey != null) {
                            setFirstAndLastDataPacket(results, usedPackets, rrInfo, packetMap, highKey);
                        }
                    }
                    rrInfo.writeHeader(dataRead);
                    while ((dataRead = bufferedReader.readLine()) != null && dataRead.length() != 0) {
                        rrInfo.writeHeader(System.lineSeparator());
                        rrInfo.writeHeader(dataRead);
                        parseHeaderLine.parseHeaderLine(dataRead, rrInfo);
                    }
                    rrInfo.writeHeader(System.lineSeparator());
                    // Check for payload and read
                    File file = new File(filePath + "_" + requestCount);
                    if (file.exists()) {
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                        rrInfo.writePayload(bis);
                        bis.close();
                    }
                    results.add(rrInfo);
                }
            }
        }
    } finally {
        bufferedReader.close();
    }
    return results;
}
Also used : UDPPacket(com.att.aro.core.packetreader.pojo.UDPPacket) StringUtils(org.apache.commons.lang.StringUtils) BufferedInputStream(java.io.BufferedInputStream) IParseHeaderLine(com.att.aro.core.packetanalysis.IParseHeaderLine) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) HttpPattern(com.att.aro.core.packetanalysis.pojo.HttpPattern) TCPPacket(com.att.aro.core.packetreader.pojo.TCPPacket) Function(java.util.function.Function) HttpRequestResponseInfo(com.att.aro.core.packetanalysis.pojo.HttpRequestResponseInfo) RequestResponseTimeline(com.att.aro.core.packetanalysis.pojo.RequestResponseTimeline) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Logger(org.apache.log4j.Logger) PacketInfo(com.att.aro.core.packetanalysis.pojo.PacketInfo) DomainNameSystem(com.att.aro.core.packetreader.pojo.DomainNameSystem) Matcher(java.util.regex.Matcher) Packet(com.att.aro.core.packetreader.pojo.Packet) Map(java.util.Map) URI(java.net.URI) TcpInfo(com.att.aro.core.packetanalysis.pojo.TcpInfo) HttpDirection(com.att.aro.core.packetanalysis.pojo.HttpDirection) Util(com.att.aro.core.util.Util) Set(java.util.Set) Session(com.att.aro.core.packetanalysis.pojo.Session) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) ISessionManager(com.att.aro.core.packetanalysis.ISessionManager) IByteArrayLineReader(com.att.aro.core.packetanalysis.IByteArrayLineReader) List(java.util.List) TreeMap(java.util.TreeMap) Entry(java.util.Map.Entry) Termination(com.att.aro.core.packetanalysis.pojo.Termination) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) LogManager(org.apache.log4j.LogManager) PacketDirection(com.att.aro.core.packetreader.pojo.PacketDirection) Collections(java.util.Collections) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpRequestResponseInfo(com.att.aro.core.packetanalysis.pojo.HttpRequestResponseInfo) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedReader(java.io.BufferedReader) PacketInfo(com.att.aro.core.packetanalysis.pojo.PacketInfo) FileReader(java.io.FileReader) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) File(java.io.File)

Example 2 with HttpDirection

use of com.att.aro.core.packetanalysis.pojo.HttpDirection in project VideoOptimzer by attdevsupport.

the class SessionManagerImpl method getLastRRInfo.

private HttpRequestResponseInfo getLastRRInfo(byte[] streamArray, PacketInfo packetInfo, TCPPacket tcpPacket, PacketInfo previousPacket, int carryoverPayloadLength, Session session, List<HttpRequestResponseInfo> results, PacketDirection packetDirection) {
    HttpRequestResponseInfo rrInfo = null;
    String line = null;
    int readerIndex = -1;
    boolean carryoverPayloadForNewRRInfo = false;
    int remainingLength = 0;
    try {
        do {
            while (true) {
                readerIndex = storageReader.getIndex();
                line = storageReader.readLine();
                if (line == null) {
                    return rrInfo;
                }
                if (line.length() != 0) {
                    break;
                }
            }
            rrInfo = initializeRequestResponseObject(line, session, packetDirection);
            if (rrInfo == null || tcpPacket == null) {
                break;
            }
            carryoverPayloadForNewRRInfo = false;
            rrInfo.setTCP(true);
            rrInfo = populateRRInfo(session, tcpPacket, rrInfo);
            boolean isExtractable, isTCP;
            isExtractable = isTCP = session.isUdpOnly() ? false : true;
            HttpDirection direction = PacketDirection.DOWNLINK.equals(packetDirection) ? HttpDirection.RESPONSE : HttpDirection.REQUEST;
            // Set first and last packet info data to rrInfo
            if (previousPacket == null || readerIndex >= carryoverPayloadLength) {
                populateRRInfo(rrInfo, packetInfo, isExtractable, isTCP, direction);
            } else {
                populateRRInfo(rrInfo, previousPacket, isExtractable, isTCP, direction);
            }
            if (!rrInfo.isHeaderParseComplete()) {
                return rrInfo;
            } else {
                results.add(rrInfo);
                rrInfo.getHeaderData().write(streamArray, 0, storageReader.getIndex());
                remainingLength = streamArray.length - storageReader.getIndex();
                if (remainingLength <= 0) {
                    return rrInfo;
                }
                if (rrInfo.getContentLength() > 0) {
                    if (rrInfo.getContentLength() <= remainingLength) {
                        rrInfo.getPayloadData().write(streamArray, storageReader.getIndex(), rrInfo.getContentLength());
                        storageReader.setArrayIndex(storageReader.getIndex() + rrInfo.getContentLength());
                        remainingLength = streamArray.length - storageReader.getIndex();
                        if (remainingLength <= 0) {
                            return rrInfo;
                        }
                        // We have found the whole payload data for the current rrInfo. There's still remaining data which belongs to a new rrInfo object.
                        // Continue processing for the next rrInfo object.
                        carryoverPayloadForNewRRInfo = true;
                    } else {
                        rrInfo.getPayloadData().write(streamArray, storageReader.getIndex(), remainingLength);
                        return rrInfo;
                    }
                } else {
                    // We are not sure if content length is explicitly 0 in the response or if it is a set by default during the initialization of object.
                    // Try to create a new rrInfo object for next line.
                    carryoverPayloadForNewRRInfo = true;
                }
            }
        } while (true);
    } catch (Exception e) {
        LOGGER.error("Error extracting request response object for packet id " + packetInfo.getPacketId() + " and session port " + session.getLocalPort(), e);
    }
    // We have a carry over payload data belonging to previous rrInfo object
    if (tcpPacket != null && carryoverPayloadForNewRRInfo && remainingLength > 0) {
        rrInfo = results.get(results.size() - 1);
        rrInfo.getPayloadData().write(streamArray, readerIndex, remainingLength);
    }
    return rrInfo;
}
Also used : HttpRequestResponseInfo(com.att.aro.core.packetanalysis.pojo.HttpRequestResponseInfo) HttpDirection(com.att.aro.core.packetanalysis.pojo.HttpDirection) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Aggregations

HttpDirection (com.att.aro.core.packetanalysis.pojo.HttpDirection)2 HttpRequestResponseInfo (com.att.aro.core.packetanalysis.pojo.HttpRequestResponseInfo)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 IByteArrayLineReader (com.att.aro.core.packetanalysis.IByteArrayLineReader)1 IParseHeaderLine (com.att.aro.core.packetanalysis.IParseHeaderLine)1 ISessionManager (com.att.aro.core.packetanalysis.ISessionManager)1 HttpPattern (com.att.aro.core.packetanalysis.pojo.HttpPattern)1 PacketInfo (com.att.aro.core.packetanalysis.pojo.PacketInfo)1 RequestResponseTimeline (com.att.aro.core.packetanalysis.pojo.RequestResponseTimeline)1 Session (com.att.aro.core.packetanalysis.pojo.Session)1 TcpInfo (com.att.aro.core.packetanalysis.pojo.TcpInfo)1 Termination (com.att.aro.core.packetanalysis.pojo.Termination)1 DomainNameSystem (com.att.aro.core.packetreader.pojo.DomainNameSystem)1 Packet (com.att.aro.core.packetreader.pojo.Packet)1 PacketDirection (com.att.aro.core.packetreader.pojo.PacketDirection)1 TCPPacket (com.att.aro.core.packetreader.pojo.TCPPacket)1 UDPPacket (com.att.aro.core.packetreader.pojo.UDPPacket)1 Util (com.att.aro.core.util.Util)1 BufferedInputStream (java.io.BufferedInputStream)1