View Javadoc

1   //========================================================================
2   //Copyright 2011-2012 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //All rights reserved. This program and the accompanying materials
5   //are made available under the terms of the Eclipse Public License v1.0
6   //and Apache License v2.0 which accompanies this distribution.
7   //The Eclipse Public License is available at
8   //http://www.eclipse.org/legal/epl-v10.html
9   //The Apache License v2.0 is available at
10  //http://www.opensource.org/licenses/apache2.0.php
11  //You may elect to redistribute this code under either of these licenses.
12  //========================================================================
13  
14  package org.eclipse.jetty.spdy.generator;
15  
16  import java.nio.ByteBuffer;
17  import java.security.cert.Certificate;
18  import java.security.cert.CertificateEncodingException;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.eclipse.jetty.spdy.ByteBufferPool;
23  import org.eclipse.jetty.spdy.SessionException;
24  import org.eclipse.jetty.spdy.api.SessionStatus;
25  import org.eclipse.jetty.spdy.frames.ControlFrame;
26  import org.eclipse.jetty.spdy.frames.CredentialFrame;
27  
28  public class CredentialGenerator extends ControlFrameGenerator
29  {
30      public CredentialGenerator(ByteBufferPool bufferPool)
31      {
32          super(bufferPool);
33      }
34  
35      @Override
36      public ByteBuffer generate(ControlFrame frame)
37      {
38          CredentialFrame credential = (CredentialFrame)frame;
39  
40          byte[] proof = credential.getProof();
41  
42          List<byte[]> certificates = serializeCertificates(credential.getCertificateChain());
43          int certificatesLength = 0;
44          for (byte[] certificate : certificates)
45              certificatesLength += certificate.length;
46  
47          int frameBodyLength = 2 + 4 + proof.length + certificates.size() * 4 + certificatesLength;
48  
49          int totalLength = ControlFrame.HEADER_LENGTH + frameBodyLength;
50          ByteBuffer buffer = getByteBufferPool().acquire(totalLength, true);
51          generateControlFrameHeader(credential, frameBodyLength, buffer);
52  
53          buffer.putShort(credential.getSlot());
54          buffer.putInt(proof.length);
55          buffer.put(proof);
56          for (byte[] certificate : certificates)
57          {
58              buffer.putInt(certificate.length);
59              buffer.put(certificate);
60          }
61  
62          buffer.flip();
63          return buffer;
64      }
65  
66      private List<byte[]> serializeCertificates(Certificate[] certificates)
67      {
68          try
69          {
70              List<byte[]> result = new ArrayList<>(certificates.length);
71              for (Certificate certificate : certificates)
72                  result.add(certificate.getEncoded());
73              return result;
74          }
75          catch (CertificateEncodingException x)
76          {
77              throw new SessionException(SessionStatus.PROTOCOL_ERROR, x);
78          }
79      }
80  }