net.tokensmith.jwt.jws.serialization.SecureJwtSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jwt Show documentation
Show all versions of jwt Show documentation
Java Implementation of JSON Web Tokens
package net.tokensmith.jwt.jws.serialization;
import net.tokensmith.jwt.entity.jwt.Claims;
import net.tokensmith.jwt.entity.jwt.JsonWebToken;
import net.tokensmith.jwt.factory.SecureJwtFactory;
import net.tokensmith.jwt.serialization.JwtSerde;
import net.tokensmith.jwt.serialization.exception.JwtToJsonException;
import java.io.ByteArrayOutputStream;
public class SecureJwtSerializer {
private SecureJwtFactory secureJwtFactory;
private JwtSerde jwtSerde;
public SecureJwtSerializer(SecureJwtFactory secureJwtFactory, JwtSerde jwtSerde) {
this.secureJwtFactory = secureJwtFactory;
this.jwtSerde = jwtSerde;
}
public String compactJwtToString(Claims claims) throws JwtToJsonException {
return compactJwt(claims).toString();
}
public ByteArrayOutputStream compactJwt(T claims) throws JwtToJsonException {
JsonWebToken jsonWebToken;
try {
jsonWebToken = secureJwtFactory.makeJwt(claims);
} catch (JwtToJsonException e) {
throw e;
}
ByteArrayOutputStream compactJwt;
try {
compactJwt = jwtSerde.compactJwt(jsonWebToken);
} catch (JwtToJsonException e) {
throw e;
}
return compactJwt;
}
}