// 
// Decompiled by Procyon v0.6.0
// 

package com.nimbusds.jwt;

import com.nimbusds.jose.JOSEObject;
import com.nimbusds.jose.Payload;
import java.util.Map;
import java.text.ParseException;
import com.nimbusds.jose.util.Base64URL;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.shaded.jcip.ThreadSafe;
import com.nimbusds.jose.JWSObject;

@ThreadSafe
public class SignedJWT extends JWSObject implements JWT
{
    private static final long serialVersionUID = 1L;
    private JWTClaimsSet claimsSet;
    
    public SignedJWT(final JWSHeader header, final JWTClaimsSet claimsSet) {
        super(header, claimsSet.toPayload());
        this.claimsSet = claimsSet;
    }
    
    public SignedJWT(final Base64URL firstPart, final Base64URL secondPart, final Base64URL thirdPart) throws ParseException {
        super(firstPart, secondPart, thirdPart);
    }
    
    @Override
    public JWTClaimsSet getJWTClaimsSet() throws ParseException {
        if (this.claimsSet != null) {
            return this.claimsSet;
        }
        final Map<String, Object> json = this.getPayload().toJSONObject();
        if (json == null) {
            throw new ParseException("Payload of JWS object is not a valid JSON object", 0);
        }
        return this.claimsSet = JWTClaimsSet.parse(json);
    }
    
    @Override
    protected void setPayload(final Payload payload) {
        this.claimsSet = null;
        super.setPayload(payload);
    }
    
    public static SignedJWT parse(final String s) throws ParseException {
        final Base64URL[] parts = JOSEObject.split(s);
        if (parts.length != 3) {
            throw new ParseException("Unexpected number of Base64URL parts, must be three", 0);
        }
        return new SignedJWT(parts[0], parts[1], parts[2]);
    }
}
