001/* 002 * acme4j - Java ACME client 003 * 004 * Copyright (C) 2016 Richard "Shred" Körber 005 * http://acme4j.shredzone.org 006 * 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 013 */ 014package org.shredzone.acme4j; 015 016import java.io.Serializable; 017import java.net.URL; 018import java.util.Objects; 019 020/** 021 * A generic ACME resource. 022 */ 023public abstract class AcmeResource implements Serializable { 024 private static final long serialVersionUID = -7930580802257379731L; 025 026 private transient Session session; 027 private URL location; 028 029 /** 030 * Create a new {@link AcmeResource}. 031 * 032 * @param session 033 * {@link Session} the resource is bound with 034 */ 035 protected AcmeResource(Session session) { 036 rebind(session); 037 } 038 039 /** 040 * Gets the {@link Session} this resource is bound with. 041 */ 042 protected Session getSession() { 043 if (session == null) { 044 throw new IllegalStateException("Use rebind() to bind this resource to a session."); 045 } 046 047 return session; 048 } 049 050 /** 051 * Sets a new {@link Session}. 052 */ 053 protected void setSession(Session session) { 054 this.session = Objects.requireNonNull(session, "session"); 055 } 056 057 /** 058 * Sets the resource's location. 059 */ 060 protected void setLocation(URL location) { 061 this.location = Objects.requireNonNull(location, "location"); 062 } 063 064 /** 065 * Rebinds this resource to a {@link Session}. 066 * <p> 067 * Sessions are not serialized, because they contain volatile session data and also a 068 * private key. After de-serialization of an {@link AcmeResource}, use this method to 069 * rebind it to a {@link Session}. 070 * 071 * @param session 072 * {@link Session} to bind this resource to 073 */ 074 public void rebind(Session session) { 075 if (this.session != null) { 076 throw new IllegalStateException("Resource is already bound to a session"); 077 } 078 setSession(session); 079 } 080 081 /** 082 * Gets the resource's location. 083 */ 084 public URL getLocation() { 085 return location; 086 } 087 088}