add GetFailed
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 12 Apr 2008 12:15:21 +0000 (12:15 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 12 Apr 2008 12:15:21 +0000 (12:15 +0000)
add getFields() to BaseMessage
make getField() in BaseMessage protected

git-svn-id: http://trooper/svn/projects/jSite/trunk@720 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/util/fcp/BaseMessage.java
src/net/pterodactylus/util/fcp/FcpAdapter.java
src/net/pterodactylus/util/fcp/FcpConnection.java
src/net/pterodactylus/util/fcp/FcpListener.java
src/net/pterodactylus/util/fcp/GetFailed.java [new file with mode: 0644]

index d64cef2..e5f043a 100644 (file)
@@ -3,6 +3,8 @@
  */
 package net.pterodactylus.util.fcp;
 
+import java.util.Map;
+
 /**
  * A basic message abstraction that wraps a received FCP message.
  * 
@@ -41,8 +43,18 @@ public class BaseMessage {
         * @return The content of the field, or <code>null</code> if there is no
         *         such field
         */
-       public String getField(String field) {
+       protected String getField(String field) {
                return receivedMessage.getField(field);
        }
 
+       /**
+        * Returns all fields from the received message.
+        * 
+        * @see FcpMessage#getFields()
+        * @return All fields from the message
+        */
+       protected Map<String, String> getFields() {
+               return receivedMessage.getFields();
+       }
+
 }
index 0402045..7dc8b6e 100644 (file)
@@ -184,6 +184,13 @@ public class FcpAdapter implements FcpListener {
        }
 
        /**
+        * @see FcpListener#receivedGetFailed(FcpConnection, GetFailed)
+        */
+       public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
+               /* empty. */
+       }
+
+       /**
         * @see FcpListener#receivedProtocolError(FcpConnection, ProtocolError)
         */
        public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
index a57a823..6c6f991 100644 (file)
@@ -451,6 +451,18 @@ public class FcpConnection {
        }
 
        /**
+        * Notifies all listeners that a “GetFailed” message was received.
+        * 
+        * @param getFailed
+        *            The “GetFailed” message
+        */
+       public void fireReceivedGetFailed(GetFailed getFailed) {
+               for (FcpListener fcpListener: fcpListeners) {
+                       fcpListener.receivedGetFailed(this, getFailed);
+               }
+       }
+
+       /**
         * Notifies all listeners that a “ProtocolError” message was received.
         * 
         * @param protocolError
@@ -557,6 +569,8 @@ public class FcpConnection {
                        fireReceivedStartedCompression(new StartedCompression(fcpMessage));
                } else if ("FinishedCompression".equals(messageName)) {
                        fireReceivedFinishedCompression(new FinishedCompression(fcpMessage));
+               } else if ("GetFailed".equals(messageName)) {
+                       fireReceivedGetFailed(new GetFailed(fcpMessage));
                } else if ("DataFound".equals(messageName)) {
                        fireReceivedDataFound(new DataFound(fcpMessage));
                } else if ("AllData".equals(messageName)) {
index ad1205d..7af6309 100644 (file)
@@ -261,6 +261,16 @@ public interface FcpListener extends EventListener {
        public void receivedConfigData(FcpConnection fcpConnection, ConfigData configData);
 
        /**
+        * Notifies a listener that a “GetFailed” message was recevied.
+        * 
+        * @param fcpConnection
+        *            The connection that received the message
+        * @param getFailed
+        *            The “GetFailed” message
+        */
+       public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed);
+
+       /**
         * Notifies a listener that a “ProtocolError” was received.
         * 
         * @param fcpConnection
diff --git a/src/net/pterodactylus/util/fcp/GetFailed.java b/src/net/pterodactylus/util/fcp/GetFailed.java
new file mode 100644 (file)
index 0000000..c42f567
--- /dev/null
@@ -0,0 +1,201 @@
+/*
+ * jSite2 - GetFailed.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.util.fcp;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * The “GetFailed” message signals the client that a {@link ClientGet} request
+ * has failed. This also means that no further progress messages for that
+ * request will be sent.
+ * 
+ * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+ * @version $Id$
+ */
+public class GetFailed extends BaseMessage {
+
+       /**
+        * Creates a new “GetFailed” message that wraps the received message.
+        * 
+        * @param receivedMessage
+        *            The received message
+        */
+       GetFailed(FcpMessage receivedMessage) {
+               super(receivedMessage);
+       }
+
+       /**
+        * Returns the code of the error.
+        * 
+        * @return The code of the error, or <code>-1</code> if the error code
+        *         could not be parsed
+        */
+       public int getCode() {
+               return FcpUtils.safeParseInt(getField("Code"));
+       }
+
+       /**
+        * Returns the identifier of the request.
+        * 
+        * @return The identifier of the request
+        */
+       public String getIdentifier() {
+               return getField("Identifier");
+       }
+
+       /**
+        * Returns whether the request is on the global queue.
+        * 
+        * @return <code>true</code> if the request is on the global queue,
+        *         <code>false</code> if it is on the client-local queue
+        */
+       public boolean isGlobal() {
+               return Boolean.valueOf(getField("Global"));
+       }
+
+       /**
+        * Returns the description of the error code.
+        * 
+        * @return The description of the error code
+        */
+       public String getCodeDescription() {
+               return getField("CodeDescription");
+       }
+
+       /**
+        * Returns the extra description of the error.
+        * 
+        * @return The extra description of the error
+        */
+       public String getExtraDescription() {
+               return getField("ExtraDescription");
+       }
+
+       /**
+        * Returns the short description of the error.
+        * 
+        * @return The short description of the error
+        */
+       public String getShortCodeDescription() {
+               return getField("ShortCodeDescription");
+       }
+
+       /**
+        * Returns the expected data length, if already knows.
+        * 
+        * @return The expected data length, or <code>-1</code> if the length
+        *         could not be parsed
+        */
+       public long getExpectedDataLength() {
+               return FcpUtils.safeParseLong(getField("ExpectedDataLength"));
+       }
+
+       /**
+        * Returns the expected content type of the request.
+        * 
+        * @return The expected content type
+        */
+       public String getExpectedMetadataContentType() {
+               return getField("ExpectedMetadata.ContentType");
+       }
+
+       /**
+        * Returns whether the expected values (see {@link #getExpectedDataLength()}
+        * and {@link #getExpectedMetadataContentType()}) have already been
+        * finalized and can be trusted. If the values have not been finalized that
+        * can change over time.
+        * 
+        * @return <code>true</code> if the expected values have already been
+        *         finalized, <code>false</code> otherwise
+        */
+       public boolean isFinalizedExpected() {
+               return Boolean.valueOf(getField("FinalizedExpected"));
+       }
+
+       /**
+        * Returns the URI the request is redirected to (in case of a request for a
+        * USK). This is returned so that client applications know that the URI of
+        * the key has updated.
+        * 
+        * @return The URI the request was redirected to
+        */
+       public String getRedirectURI() {
+               return getField("RedirectURI");
+       }
+
+       /**
+        * Returns a list of complex error codes with the message. Use
+        * {@link #getComplexErrorDescription(int)} and
+        * {@link #getComplexErrorCount(int)} to get details.
+        * 
+        * @return A list of complex error codes
+        */
+       public int[] getComplexErrorCodes() {
+               Map<String, String> allFields = getFields();
+               List<Integer> errorCodeList = new ArrayList<Integer>();
+               for (Entry<String, String> field: allFields.entrySet()) {
+                       String fieldKey = field.getKey();
+                       if (fieldKey.startsWith("Errors.")) {
+                               int nextDot = fieldKey.indexOf('.', 7);
+                               if (nextDot > -1) {
+                                       int errorCode = FcpUtils.safeParseInt(fieldKey.substring(7, nextDot));
+                                       if (errorCode != -1) {
+                                               errorCodeList.add(errorCode);
+                                       }
+                               }
+                       }
+               }
+               int[] errorCodes = new int[errorCodeList.size()];
+               int errorIndex = 0;
+               for (int errorCode: errorCodeList) {
+                       errorCodes[errorIndex++] = errorCode;
+               }
+               return errorCodes;
+       }
+
+       /**
+        * Returns the description of the complex error. You should only hand it
+        * error codes you got from {@link #getComplexErrorCodes()}!
+        * 
+        * @param errorCode
+        *            The error code
+        * @return The description of the complex error
+        */
+       public String getComplexErrorDescription(int errorCode) {
+               return getField("Errors." + errorCode + ".Desecription");
+       }
+
+       /**
+        * Returns the count of the complex error. You should only hand it error
+        * codes you got from {@link #getComplexErrorCodes()}!
+        * 
+        * @param errorCode
+        *            The error code
+        * @return The count of the complex error, or <code>-1</code> if the count
+        *         could not be parsed
+        */
+       public int getComplexErrorCount(int errorCode) {
+               return FcpUtils.safeParseInt(getField("Errors." + errorCode + ".Count"));
+       }
+
+}