View Javadoc

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