Java Code Examples for org.eclipse.jetty.util.B64Code#encode()

The following examples show how to use org.eclipse.jetty.util.B64Code#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Resource.java    From IoTgo_Android_App with MIT License 6 votes vote down vote up
public String getWeakETag()
{
    try
    {
        StringBuilder b = new StringBuilder(32);
        b.append("W/\"");
        
        String name=getName();
        int length=name.length();
        long lhash=0;
        for (int i=0; i<length;i++)
            lhash=31*lhash+name.charAt(i);
        
        B64Code.encode(lastModified()^lhash,b);
        B64Code.encode(length()^lhash,b);
        b.append('"');
        return b.toString();
    } 
    catch (IOException e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: Resource.java    From IoTgo_Android_App with MIT License 6 votes vote down vote up
public String getWeakETag()
{
    try
    {
        StringBuilder b = new StringBuilder(32);
        b.append("W/\"");
        
        String name=getName();
        int length=name.length();
        long lhash=0;
        for (int i=0; i<length;i++)
            lhash=31*lhash+name.charAt(i);
        
        B64Code.encode(lastModified()^lhash,b);
        B64Code.encode(length()^lhash,b);
        b.append('"');
        return b.toString();
    } 
    catch (IOException e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 3
Source File: ProxyServletService.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * If the URI contains user info in the form <code>user[:pass]@</code>, attempt to preempt the server
 * returning a 401 by providing Basic Authentication support in the initial request to the server.
 *
 * @param uri the URI which may contain user info
 * @param request the outgoing request to which an authorization header may be added
 */
void maybeAppendAuthHeader(URI uri, Request request) {
    if (uri != null && uri.getUserInfo() != null) {
        String[] userInfo = uri.getUserInfo().split(":");

        if (userInfo.length >= 1) {
            String user = userInfo[0];
            String password = userInfo.length >= 2 ? userInfo[1] : null;
            String authString = password != null ? user + ":" + password : user + ":";

            String basicAuthentication = "Basic " + B64Code.encode(authString, StringUtil.__ISO_8859_1);
            request.header(HttpHeader.AUTHORIZATION, basicAuthentication);
        }
    }
}
 
Example 4
Source File: WebSocketConnectionRFC6455.java    From IoTgo_Android_App with MIT License 5 votes vote down vote up
public static String hashKey(String key)
{
    try
    {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(key.getBytes("UTF-8"));
        md.update(MAGIC);
        return new String(B64Code.encode(md.digest()));
    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: WebSocketConnectionRFC6455.java    From IoTgo_Android_App with MIT License 5 votes vote down vote up
public static String hashKey(String key)
{
    try
    {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(key.getBytes("UTF-8"));
        md.update(MAGIC);
        return new String(B64Code.encode(md.digest()));
    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 6
Source File: ProxyServletService.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * If the URI contains user info in the form <code>user[:pass]@</code>, attempt to preempt the server
 * returning a 401 by providing Basic Authentication support in the initial request to the server.
 *
 * @param uri the URI which may contain user info
 * @param request the outgoing request to which an authorization header may be added
 */
void maybeAppendAuthHeader(URI uri, Request request) {
    if (uri != null && uri.getUserInfo() != null) {
        String[] userInfo = uri.getUserInfo().split(":");

        if (userInfo.length >= 1) {
            String user = userInfo[0];
            String password = userInfo.length >= 2 ? userInfo[1] : null;
            String authString = password != null ? user + ":" + password : user + ":";

            String basicAuthentication = "Basic " + B64Code.encode(authString, StringUtil.__ISO_8859_1);
            request.header(HttpHeader.AUTHORIZATION, basicAuthentication);
        }
    }
}
 
Example 7
Source File: SolrBasicAuthentication.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public SolrBasicAuthentication(String user, String password) {
  this.value = "Basic " + B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1);
}
 
Example 8
Source File: WebSocketClientFactory.java    From IoTgo_Android_App with MIT License 4 votes vote down vote up
public HandshakeConnection(AsyncEndPoint endpoint, WebSocketClient.WebSocketFuture future)
{
    super(endpoint, System.currentTimeMillis());
    _endp = endpoint;
    _future = future;

    byte[] bytes = new byte[16];
    new Random().nextBytes(bytes);
    _key = new String(B64Code.encode(bytes));

    Buffers buffers = new SimpleBuffers(_buffers.getBuffer(), null);
    _parser = new HttpParser(buffers, _endp, new HttpParser.EventHandler()
    {
        @Override
        public void startResponse(Buffer version, int status, Buffer reason) throws IOException
        {
            if (status != 101)
            {
                _error = "Bad response status " + status + " " + reason;
                _endp.close();
            }
        }

        @Override
        public void parsedHeader(Buffer name, Buffer value) throws IOException
        {
            if (__ACCEPT.equals(name))
                _accept = value.toString();
        }

        @Override // TODO simone says shouldn't be needed
        public void startRequest(Buffer method, Buffer url, Buffer version) throws IOException
        {
            if (_error == null)
                _error = "Bad response: " + method + " " + url + " " + version;
            _endp.close();
        }

        @Override // TODO simone says shouldn't be needed
        public void content(Buffer ref) throws IOException
        {
            if (_error == null)
                _error = "Bad response. " + ref.length() + "B of content?";
            _endp.close();
        }
    });
}
 
Example 9
Source File: WebSocketClientFactory.java    From IoTgo_Android_App with MIT License 4 votes vote down vote up
public HandshakeConnection(AsyncEndPoint endpoint, WebSocketClient.WebSocketFuture future)
{
    super(endpoint, System.currentTimeMillis());
    _endp = endpoint;
    _future = future;

    byte[] bytes = new byte[16];
    new Random().nextBytes(bytes);
    _key = new String(B64Code.encode(bytes));

    Buffers buffers = new SimpleBuffers(_buffers.getBuffer(), null);
    _parser = new HttpParser(buffers, _endp, new HttpParser.EventHandler()
    {
        @Override
        public void startResponse(Buffer version, int status, Buffer reason) throws IOException
        {
            if (status != 101)
            {
                _error = "Bad response status " + status + " " + reason;
                _endp.close();
            }
        }

        @Override
        public void parsedHeader(Buffer name, Buffer value) throws IOException
        {
            if (__ACCEPT.equals(name))
                _accept = value.toString();
        }

        @Override // TODO simone says shouldn't be needed
        public void startRequest(Buffer method, Buffer url, Buffer version) throws IOException
        {
            if (_error == null)
                _error = "Bad response: " + method + " " + url + " " + version;
            _endp.close();
        }

        @Override // TODO simone says shouldn't be needed
        public void content(Buffer ref) throws IOException
        {
            if (_error == null)
                _error = "Bad response. " + ref.length() + "B of content?";
            _endp.close();
        }
    });
}