CPD Results

The following document contains the results of PMD's CPD 5.1.2.

Duplications

File Project Line
com/ericsson/research/transport/ws/spi/WSHybi10Handshake.java TrAP Networking: Websockets Base 82
com/ericsson/research/transport/ws/spi/WSRfc6455Handshake.java TrAP Networking: Websockets Base 82
            h.append(8);
            h.append(CRLF);
            h.append(SEC_WEBSOCKET_KEY_HEADER);
            h.append(COLON_SPACE);
            byte[] randomBytes = new byte[16];
            random.nextBytes(randomBytes);
            this.key = new String(Base64.encode(randomBytes));
            h.append(this.key);
            h.append(CRLFCRLF);
        }
        else
        {
            h.append(SERVER_PREAMBLE);
            h.append(WSHixie75Handshake.COMMON_PART);
            h.append(SEC_WEBSOCKET_ACCEPT_HEADER);
            h.append(COLON_SPACE);
            String nounceAndGUID = this.key + GUID;
            try
            {
                byte[] sha1 = this.computeSHA1(nounceAndGUID);
                h.append(Base64.encode(sha1));
                h.append(CRLFCRLF);
            }
            catch (WSException e)
            {
                throw new IOException(e);
            }
        }
        synchronized (os)
        {
            os.write(h.toString().getBytes(ISO_8859_1));
            os.flush();
        }
    }
    
    public void headersRead() throws WSException
    {
        if (this.protocol.client)
        {
            byte[] expected = this.computeSHA1(this.key + GUID);
            if (this.getHeader(SEC_WEBSOCKET_ACCEPT_HEADER) == null)
                throw new WSException("Missing " + SEC_WEBSOCKET_ACCEPT_HEADER + " header");
            byte[] actual = Base64.decode(this.getHeader(SEC_WEBSOCKET_ACCEPT_HEADER));
            if (!Arrays.equals(expected, actual))
                throw new WSException("Failed to validate " + SEC_WEBSOCKET_ACCEPT_HEADER + " header value");
            this.protocol.protocol = this.getHeader(SEC_WEBSOCKET_PROTOCOL_HEADER);
        }
        else
        {
            if (this.getHeader(HOST_HEADER) == null)
                throw new WSException("Missing " + HOST_HEADER + " header");
            if (!"8".equals(this.getHeader(SEC_WEBSOCKET_VERSION_HEADER)))
File Project Line
com/ericsson/research/trap/impl/ClientTrapEndpoint.java TrAP Core 246
com/ericsson/research/trap/impl/TrapPeerImpl.java TrAP Core 455
            if (this.logger.isDebugEnabled())
            {
                StringBuilder logMsg = new StringBuilder();
                logMsg.append("Lost a transport. TrapEndpont state is ");
                logMsg.append(this.getState().toString());
                logMsg.append(". I have ");
                logMsg.append(this.activeTransports.size());
                logMsg.append(" active transports. Name/State follows... ");
                
                synchronized (this.activeTransports)
                {
                    Iterator<TrapTransport> it = this.activeTransports.iterator();
                    
                    while (it.hasNext())
                    {
                        TrapTransport at = it.next();
                        logMsg.append(at.getTransportName());
                        logMsg.append("/");
                        logMsg.append(at.getState());
                        logMsg.append(", ");
                    }
                }
                
                this.logger.debug(logMsg.toString());
            }
            
            if (this.getState() == TrapState.SLEEPING && System.currentTimeMillis() >= this.canReconnectUntil)
            {
                this.logger.debug("Timer expired on reconnect");
                this.setState(TrapState.CLOSED);
                return;
            }
            
            // This was an already connected transport. If we have other transports available, we should silently try and reconnect it in the background
            if ((oldState == TrapTransportState.AVAILABLE) || (oldState == TrapTransportState.UNAVAILABLE) || (oldState == TrapTransportState.CONNECTED))
            {
                
                if (this.activeTransports.size() != 0)
File Project Line
com/ericsson/research/trap/spi/transports/ServerWebSocketTransport.java TrAP Transport WebSocket by ER NIO 74
com/ericsson/research/trap/spi/transports/WebServerSocketTransport.java TrAP Transport WebSocket by Netty 82
	}
	
	public void listen(ListenerTrapTransportDelegate listener, Object context) throws TrapException
	{
		this.listenerDelegate = listener;
		this.listenerContext = context;

		this.delegate = new TrapTransportDelegate() {

			@Override
			public void ttStateChanged(TrapTransportState newState, TrapTransportState oldState, TrapTransport transport, Object context)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void ttMessageReceived(TrapMessage message, TrapTransport transport, Object context)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void ttMessageSent(TrapMessage message, TrapTransport transport, Object context)
			{
				// TODO Auto-generated method stub
				
			}

            @Override
            public void ttMessagesFailedSending(Collection<TrapMessage> messages, TrapTransport transport, Object context)
            {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void ttNeedTransport(TrapMessage message, TrapTransport transport, Object context)
            {
                // TODO Auto-generated method stub
                
            }
		};

		try
		{

			int port = 0;
			try
			{
				port = Integer.parseInt(this.getOption(WebSocketConstants.CONFIG_SERVER_PORT));
			}
			catch (Throwable t)
			{
			}

			this.host = this.getOption(WebSocketConstants.CONFIG_SERVER_HOST);

			if ((this.host == null) || (this.host.trim().length() == 0))
			{
				this.host = "0.0.0.0";
				this.defaultHost = true;
			}
File Project Line
com/ericsson/research/trap/spi/transports/WebServerSocketTransport.java TrAP Transport WebSocket by Netty 61
com/ericsson/research/trap/spi/transports/TomcatWSListener.java TrAP Transport WebSocket for Tomcat Container 68
	public String getTransportName()
	{
		return "websocket";
	}
	
	@Override
	public String getProtocolName()
	{
		return TrapTransportProtocol.WEBSOCKET;
	}

	@Override
	public boolean canConnect()
	{
		return false;
	}

	@Override
	public boolean canListen()
	{
		return true;
	}

	public void listen(ListenerTrapTransportDelegate listener, Object context) throws TrapException
	{
		this.listenerDelegate = listener;
		this.listenerContext = context;

		this.delegate = new TrapTransportDelegate() {

			@Override
			public void ttStateChanged(TrapTransportState newState, TrapTransportState oldState, TrapTransport transport, Object context)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void ttMessageReceived(TrapMessage message, TrapTransport transport, Object context)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void ttMessageSent(TrapMessage message, TrapTransport transport, Object context)
			{
				// TODO Auto-generated method stub
				
			}

            @Override
            public void ttMessagesFailedSending(Collection<TrapMessage> messages, TrapTransport transport, Object context)
            {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void ttNeedTransport(TrapMessage message, TrapTransport transport, Object context)
            {
                // TODO Auto-generated method stub
                
            }
		};
File Project Line
com/ericsson/research/transport/ws/spi/WSHybi10Handshake.java TrAP Networking: Websockets Base 51
com/ericsson/research/transport/ws/spi/WSRfc6455Handshake.java TrAP Networking: Websockets Base 51
    public WSHybi10Handshake(WSAbstractProtocol protocol)
    {
        super(protocol);
    }
    
    public void sendHandshake(OutputStream os) throws IOException
    {
        StringBuffer h = new StringBuffer();
        if (this.protocol.client)
        {
            h.append(GET);
            h.append(" ");
            h.append(this.protocol.resource);
            h.append(" ");
            h.append(HTTP11);
            h.append(WSHixie75Handshake.COMMON_PART);
            h.append(HOST_HEADER);
            h.append(COLON_SPACE);
            h.append(this.protocol.host);
            if ((this.protocol.securityContext == null && this.protocol.port != 80) || (this.protocol.securityContext != null && this.protocol.port != 443))
            {
                h.append(":");
                h.append(this.protocol.port);
            }
            h.append(CRLF);
            h.append(SEC_WEBSOCKET_ORIGIN_HEADER);
File Project Line
com/ericsson/research/trap/impl/http/HTTPServlet25.java Trap HTTP Server using HTTPServlet interface 76
com/ericsson/research/trap/impl/http/HTTPServlet30.java Trap HTTP Server using HTTPServlet 3.0 interface 71
        this.ctxPath = this.ctxPath + this.path;
        HTTPServletAdaptor.addServletContext(this.ctxPath);
    }
    
    @Override
    public void destroy()
    {
        HTTPServletAdaptor.removeServletContext(this.ctxPath);
    }
    
    @Override
    protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
    {
        this.internalService(arg0, arg1);
    }
    
    @Override
    protected void doDelete(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
    {
        this.internalService(arg0, arg1);
    }
    
    @Override
    protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
    {
        this.internalService(arg0, arg1);
    }
    
    @Override
    protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
    {
        this.internalService(arg0, arg1);
    }
    
    @Override
    protected void doPut(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
    {
        this.internalService(arg0, arg1);
    }
File Project Line
com/ericsson/research/trap/examples/EchoServer.java TrAP API 86
com/ericsson/research/trap/examples/TLSEchoServer.java TrAP API 108
com/ericsson/research/trap/examples/TLSInsecureEchoServer.java TrAP API 89
        echoServer.listen(new EchoServer());
        
        // Now to tell clients how to connect. Since we elected no configuration, Trap will allocate random ports.
        clientConfig = echoServer.getClientConfiguration();
        System.out.println("New clients should connect to the following configuration:");
        System.out.println(echoServer.getClientConfiguration());
    }
    
    public void incomingTrapConnection(TrapEndpoint endpoint, TrapListener listener, Object context)
    {
        // Endpoints received by this method are NOT strongly referenced, and may be GC'd. Thus, we must retain
        clients.add(endpoint);
        
        // We also want feedback from the endpoint.
        endpoint.setDelegate(this, true);
    }
    
    public void trapData(byte[] data, int channel, TrapEndpoint endpoint, Object context)
    {
        // Log!
        System.out.println("Echo Server Got message: [" + StringUtil.toUtfString(data) + "] of length " + data.length);
        
        try
        {
            // Echo the data back with the same parameters.
            endpoint.send(data, channel, false);
        }
        catch (TrapException e)
        {
            e.printStackTrace();
        }
    }
    
    public void trapClose(TrapEndpoint endpoint, Object context)
    {
        // Remove the strong reference to allow garbage collection
        clients.remove(endpoint);
    }
    
}
File Project Line
com/ericsson/research/trap/impl/queues/LinkedBlockingMessageQueue.java TrAP Core 122
com/ericsson/research/trap/impl/queues/LinkedMessageQueue.java TrAP Core 70
        }
    }
    
    public TrapMessage peek()
    {
        synchronized (this.messageQueue)
        {
            try
            {
                return this.messageQueue.get(0);
            }
            catch (IndexOutOfBoundsException e)
            {
                return null;
            }
        }
    }
    
    public TrapMessage pop()
    {
        synchronized (this.messageQueue)
        {
            try
            {
                TrapMessage m = this.messageQueue.remove(0);
                this.messageQueue.notifyAll();
                return m;
            }
            catch (IndexOutOfBoundsException e)
            {
                return null;
            }
        }
    }
    
    public long size()
    {
        return this.maxQueueSize;
    }
    
    public void resize(long newSize)
    {
        this.maxQueueSize = newSize;
    }
    
    public String getQueueType()
    {
        return TrapEndpoint.BLOCKING_MESSAGE_QUEUE;
    }
    
    public int length()
    {
        return this.messageQueue.size();
    }
    
    public long blockingTimeout()
File Project Line
com/ericsson/research/trap/examples/EchoClient.java TrAP API 96
com/ericsson/research/trap/examples/TLSInsecureEchoClient.java TrAP API 95
        this.client = TrapFactory.createClient(trapCfg, true);
        
        // Tell the client to use us as a delegate.
        // We'll only handle two events this time: open and data
        this.client.setDelegate(this, true);
        
        // Start connecting.
        this.client.open();
    }
    
    // Called when the client is open (=ready to send)
    public void trapOpen(TrapEndpoint endpoint, Object context)
    {
        // Send a message!
        this.sendMessage();
    }
    
    // Called when the client receives data. Since we connect to the echo server, the same message should come back.
    public void trapData(byte[] data, int channel, TrapEndpoint endpoint, Object context)
    {
        System.out.println("Got message: " + StringUtil.toUtfString(data));
        this.sendMessage();
    }
    
    private void sendMessage()
    {
        String message = "Message {" + this.counter++ + "}";
        System.out.println("Now sending: " + message);
        
        try
        {
            this.client.send(StringUtil.toUtfBytes(message));
        }
        catch (TrapException e)
        {
            e.printStackTrace();
        }
    }
    
}
File Project Line
com/ericsson/research/trap/spi/transports/ApacheClientHttpTransport.java Trap HTTP Client using Apache HTTP client 142
com/ericsson/research/trap/spi/transports/ClientHttpTransport.java Trap HTTP Client using Sun HTTP client 168
    {
        
        synchronized (this.messagesToSend)
        {
            
            if ((this.getState() != TrapTransportState.CONNECTED) && (this.getState() != TrapTransportState.AVAILABLE) && (this.getState() != TrapTransportState.UNAVAILABLE) && (message.getOp() != TrapMessage.Operation.CLOSE))
                throw new TrapTransportException(message, this.getState());
            
            if (message != null)
            {
                if (this.logger.isTraceEnabled())
                    this.logger.trace("[HTTP] Scheduling message {} with id {}", message.getOp(), message.getMessageId());
                // Don't slam messages yet
                this.messagesToSend.add(message);
            }
            
            if (expectMore)
                return;
            
            this.flushTransport();
            
        }
    }
File Project Line
com/ericsson/research/trap/spi/transports/WebSocketTransport.java TrAP Transport WebSocket by Netty 107
com/ericsson/research/trap/spi/transports/TomcatWSTransport.java TrAP Transport WebSocket for Tomcat Container 112
							WebSocketTransport.this.flushTransport();
						}
					}, 1);
				}
				
				if (expectMore)
				{
					
					if (this.outBuf == null)
						this.outBuf = new ByteArrayOutputStream();
					
					this.outBuf.write(raw);
					return;
				}
				
				this.performSend(raw);
				
			}
		}
		catch (IOException e)
		{
			this.logger.debug(e.toString());
			this.setState(TrapTransportState.ERROR);
			throw new TrapTransportException(message, this.state);
		}
		catch (TrapTransportException e)
		{
			this.setState(TrapTransportState.ERROR);
			throw e;
		}
		catch (Throwable t)
		{
			t.printStackTrace();
		}
	}
	
	private void performSend(byte[] raw) throws IOException
	{
File Project Line
com/ericsson/research/trap/utils/Base64.java TrAP Utils API 122
com/ericsson/research/trap/utils/Base64.java TrAP Utils API 157
char[] out = new char[oLen];
int ip = iOff;
int iEnd = iOff + iLen;
int op = 0;
while (ip < iEnd) {
   int i0 = in[ip++] & 0xff;
   int i1 = ip < iEnd ? in[ip++] & 0xff : 0;
   int i2 = ip < iEnd ? in[ip++] & 0xff : 0;
   int o0 = i0 >>> 2;
   int o1 = ((i0 &   3) << 4) | (i1 >>> 4);
   int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
   int o3 = i2 & 0x3F;
   out[op++] = map1[o0];
File Project Line
com/ericsson/research/trap/spi/transports/ServerWebSocketTransport.java TrAP Transport WebSocket by ER NIO 74
com/ericsson/research/trap/spi/transports/TomcatWSListener.java TrAP Transport WebSocket for Tomcat Container 89
	}
	
	public void listen(ListenerTrapTransportDelegate listener, Object context) throws TrapException
	{
		this.listenerDelegate = listener;
		this.listenerContext = context;

		this.delegate = new TrapTransportDelegate() {

			@Override
			public void ttStateChanged(TrapTransportState newState, TrapTransportState oldState, TrapTransport transport, Object context)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void ttMessageReceived(TrapMessage message, TrapTransport transport, Object context)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void ttMessageSent(TrapMessage message, TrapTransport transport, Object context)
			{
				// TODO Auto-generated method stub
				
			}

            @Override
            public void ttMessagesFailedSending(Collection<TrapMessage> messages, TrapTransport transport, Object context)
            {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void ttNeedTransport(TrapMessage message, TrapTransport transport, Object context)
            {
                // TODO Auto-generated method stub
                
            }
		};
File Project Line
com/ericsson/research/trap/impl/ClientTrapEndpoint.java TrAP Core 120
com/ericsson/research/trap/impl/TrapPeerImpl.java TrAP Core 204
        try
        {
            Class<TrapTransport>[] transports = TrapTransports.getTransportClasses(this.getClass().getClassLoader());
            
            for (int i = 0; i < transports.length; i++)
            {
                if (!TrapTransport.class.isAssignableFrom(transports[i]) || transports[i].isAssignableFrom(AbstractTransport.class))
                    continue;
                
                TrapTransport t;
                
                try
                {
                    t = transports[i].newInstance();
                }
                catch (Exception e)
                {
                    this.logger.debug("Failed to instantiate {}; Most probably a server transport...", transports[i].getName(), e);
                    continue;
                }
                
                if (!t.canConnect())
                    continue;
File Project Line
com/ericsson/research/trap/examples/AutoConfiguredServer.java TrAP API 85
com/ericsson/research/trap/examples/EchoServer.java TrAP API 91
com/ericsson/research/trap/examples/TLSEchoServer.java TrAP API 113
com/ericsson/research/trap/examples/TLSInsecureEchoServer.java TrAP API 94
        System.out.println(clientConfig);
    }
    
    public void incomingTrapConnection(TrapEndpoint endpoint, TrapListener listener, Object context)
    {
        // Endpoints received by this method are NOT strongly referenced, and may be GC'd. Thus, we must retain
        clients.add(endpoint);
        
        // We also want feedback from the endpoint.
        endpoint.setDelegate(this, true);
    }
    
    public void trapData(byte[] data, int channel, TrapEndpoint endpoint, Object context)
    {
        // Log!
        System.out.println("Echo Server Got message: [" + StringUtil.toUtfString(data) + "] of length " + data.length);
        
        try
        {
            // Echo the data back with the same parameters.
            endpoint.send(data, channel, false);
        }
        catch (TrapException e)
        {
            e.printStackTrace();
        }
    }
    
    public void trapClose(TrapEndpoint endpoint, Object context)
    {
        // Remove the strong reference to allow garbage collection
        clients.remove(endpoint);
    }
    
}
File Project Line
com/ericsson/research/transport/ws/spi/WSServerImpl.java TrAP Networking: Websockets using NIO 86
com/ericsson/research/transport/ws/spi/WSServerImpl.java TrAP Networking: Websockets using Sockets 93
			}
		}
	}
	
	public WSURI getURI() {
		String scheme = (securityContext==null ? "ws" : "wss");

		InetSocketAddress inetAddress = getAddress();
		String host = inetAddress.getAddress().getHostAddress();
		int port = inetAddress.getPort();
		String resource = "ws";
		
		byte[] address = inetAddress.getAddress().getAddress();
		boolean isv6 = address.length > 4;
		
		if (isv6)
			host = "[" + host + "]";

		try {
			return new WSURI(scheme + "://" + host + ":" + port + "/" + resource);
		} catch (IllegalArgumentException e) {
File Project Line
com/ericsson/research/trap/spi/transports/ListenerHttpTransport.java Trap HTTP Server using Sun HTTP server 286
com/ericsson/research/trap/spi/transports/ServerSocketTransport.java Socket Server using ER NIO 131
            SSLContext sslc = null;
            
            if (this.getOption(CERT_USE_INSECURE_TEST) != null)
            {
                sslc = SSLUtil.getContext(new SSLMaterial("jks", "trapserver.jks", "Ericsson"), new SSLMaterial("jks", "trapserver.jks", "Ericsson"));
                this.logger.warn("Using insecure SSL context");
            }
            else
            {
                try
                {
                    String keyType = this.getOption(CERT_KEYSTORE_TYPE);
                    String keyName = this.getOption(CERT_KEYSTORE_NAME);
                    String keyPass = this.getOption(CERT_KEYSTORE_PASS);
                    
                    String trustType = this.getOption(CERT_TRUSTSTORE_TYPE);
                    String trustName = this.getOption(CERT_TRUSTSTORE_NAME);
                    String trustPass = this.getOption(CERT_TRUSTSTORE_PASS);
File Project Line
com/ericsson/research/trap/spi/transports/WebSocketTransport.java TrAP Transport WebSocket by ER NIO 177
com/ericsson/research/trap/spi/transports/WebSocketTransport.java TrAP Transport WebSocket by Netty 87
            WSInterface mSock = this.socket;
            
            if (mSock == null)
                throw new TrapTransportException(message, this.getState());
            
            synchronized (mSock)
            {
                
                byte[] raw = message.serialize();
                
                this.delayed |= this.lastSend == System.currentTimeMillis();
                this.delayed &= this.useDelay;
                if (this.delayed && !this.delayQueued)
                {
                    this.delayQueued = true;
                    ThreadPool.executeAfter(new Runnable() {
                        
                        @Override
                        public void run()
                        {
                            WebSocketTransport.this.flushTransport();
                        }
                    }, 1);
                }
                
                if (expectMore || this.delayed)
File Project Line
com/ericsson/research/transport/ws/spi/WSHixie75Handshake.java TrAP Networking: Websockets Base 92
com/ericsson/research/transport/ws/spi/WSHixie76Handshake.java TrAP Networking: Websockets Base 141
				throw new WSException("Failed to match origin ("+protocol.origin+" != "+getHeader(WEBSOCKET_ORIGIN_HEADER)+")");
			StringBuffer location = new StringBuffer();
			if(protocol.securityContext!=null)
				location.append(WSS_SCHEMA);
			else
				location.append(WS_SCHEMA);
			location.append(protocol.host);
			if((protocol.securityContext==null && protocol.port != 80) || (protocol.securityContext!=null && protocol.port != 443)) {
				location.append(":");
				location.append(protocol.port);
			}
			location.append(protocol.resource);
			if (!location.toString().equals(getHeader(WEBSOCKET_LOCATION_HEADER)))
File Project Line
com/ericsson/research/trap/spi/transports/ListenerHttpTransport.java Trap HTTP Server using Sun HTTP server 99
com/ericsson/research/trap/spi/transports/ServerWebSocketTransport.java TrAP Transport WebSocket by ER NIO 81
com/ericsson/research/trap/spi/transports/WebServerSocketTransport.java TrAP Transport WebSocket by Netty 89
com/ericsson/research/trap/spi/transports/TomcatWSListener.java TrAP Transport WebSocket for Tomcat Container 96
        this.delegate = new TrapTransportDelegate() {
            
            @Override
            public void ttStateChanged(TrapTransportState newState, TrapTransportState oldState, TrapTransport transport, Object context)
            {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void ttMessageReceived(TrapMessage message, TrapTransport transport, Object context)
            {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void ttMessageSent(TrapMessage message, TrapTransport transport, Object context)
            {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void ttMessagesFailedSending(Collection<TrapMessage> messages, TrapTransport transport, Object context)
            {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void ttNeedTransport(TrapMessage message, TrapTransport transport, Object context)
            {
                // TODO Auto-generated method stub
                
            }
        };