// 
// 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.PlainHeader;
import com.nimbusds.jose.shaded.jcip.ThreadSafe;
import com.nimbusds.jose.PlainObject;

@ThreadSafe
public class PlainJWT extends PlainObject implements JWT
{
    private static final long serialVersionUID = 1L;
    private JWTClaimsSet claimsSet;
    
    public PlainJWT(final JWTClaimsSet claimsSet) {
        super(claimsSet.toPayload());
        this.claimsSet = claimsSet;
    }
    
    public PlainJWT(final PlainHeader header, final JWTClaimsSet claimsSet) {
        super(header, claimsSet.toPayload());
        this.claimsSet = claimsSet;
    }
    
    public PlainJWT(final Base64URL firstPart, final Base64URL secondPart) throws ParseException {
        super(firstPart, secondPart);
    }
    
    @Override
    public JWTClaimsSet getJWTClaimsSet() throws ParseException {
        if (this.claimsSet != null) {
            return this.claimsSet;
        }
        final Map<String, Object> jsonObject = this.getPayload().toJSONObject();
        if (jsonObject == null) {
            throw new ParseException("Payload of unsecured JOSE object is not a valid JSON object", 0);
        }
        return this.claimsSet = JWTClaimsSet.parse(jsonObject);
    }
    
    @Override
    protected void setPayload(final Payload payload) {
        this.claimsSet = null;
        super.setPayload(payload);
    }
    
    public static PlainJWT parse(final String s) throws ParseException {
        final Base64URL[] parts = JOSEObject.split(s);
        if (!parts[2].toString().isEmpty()) {
            throw new ParseException("Unexpected third Base64URL part in the unsecured JWT object", 0);
        }
        return new PlainJWT(parts[0], parts[1]);
    }
}
