+ * Encodes text in a way that makes it possible for the text to be stored in
+ * a {@link SimpleFieldSet}. Backslashes, CR, and LF are prepended with a
+ * backslash.
+ *
+ * @param text
+ * The text to encode
+ * @return The encoded text
+ */
+ protected static String encodeString(String text) {
+ return text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
+ }
+
+ protected Optional<Sone> getOptionalSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
+ String soneId = getString(simpleFieldSet, parameterName, null);
+ return (soneId == null) ? Optional.<Sone>absent() : core.getSone(soneId);
+ }
+
+ protected Sone getMandatoryLocalSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
+ Sone sone = getMandatorySone(simpleFieldSet, parameterName);
+ if (!sone.isLocal()) {
+ throw new FcpException("Could not load Sone from “" + sone.getId() + "”.");
+ }
+ return sone;
+ }
+
+ protected Sone getMandatorySone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
+ String soneId = getMandatoryParameter(simpleFieldSet, parameterName);
+ Optional<Sone> sone = getMandatorySone(soneId);
+ return sone.get();
+ }
+
+ private Optional<Sone> getMandatorySone(String soneId) throws FcpException {
+ Optional<Sone> sone = core.getSone(soneId);
+ if (!sone.isPresent()) {
+ throw new FcpException("Could not load Sone from “" + soneId + "”.");
+ }
+ return sone;
+ }
+
+ private String getMandatoryParameter(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
+ String soneId = simpleFieldSet.get(parameterName);
+ if (soneId == null) {
+ throw new FcpException("Could not load Sone ID from “" + parameterName + "”.");
+ }
+ return soneId;
+ }
+
+ /**
+ * Returns a post whose ID is a parameter in the given simple field set.