View Javadoc

1   /**
2    * Copyright 2010, CSIRO Australia.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /**
18   * 
19   */
20  package au.csiro.pidclient.business;
21  
22  import java.text.MessageFormat;
23  
24  /**
25   * Represents the identify information required when calling the ANDS Persistent Identifier service.
26   * 
27   * Copyright 2010, CSIRO Australia All rights reserved.
28   * 
29   * @author Robert Bridle on 08/02/2010
30   * @version $Revision: 7131 $ $Date: 2010-06-09 14:25:15 +1000 (Wed, 09 Jun 2010) $
31   */
32  public class AndsPidIdentity
33  {
34      /**
35       * The unique Id provided to the caller upon IP registration with the ANDS Persistent Identifier service.
36       */
37      private String appId;
38      
39      /**
40       * The identifier or name of the repository calling the service.
41       */
42      private String identifier;
43      
44      /**
45       * The domain of the organisation calling the service.
46       */
47      private String authDomain;
48  
49      /**
50       * 
51       */
52      public AndsPidIdentity()
53      {
54      }
55  
56      /**
57       * Constructor
58       * 
59       * @param appId
60       *            the unique Id provided to the caller upon IP registration with the ANDS Persistent Identifier service.
61       * @param identifier
62       *            the identifier or name of the repository calling the service.
63       * @param authDomain
64       *            the domain of the organisation calling the service.
65       */
66      @SuppressWarnings(value = "all")
67      public AndsPidIdentity(String appId, String identifier, String authDomain)
68      {
69          this.setAppId(appId);
70          this.setIdentifier(identifier);
71          this.setAuthDomain(authDomain);
72      }
73  
74      /**
75       * @return the appId
76       */
77      public String getAppId()
78      {
79          return escape(appId);
80      }
81  
82      /**
83       * @param appId
84       *            the appId to set
85       */
86      public void setAppId(String appId)
87      {
88          this.appId = appId;
89      }
90  
91      /**
92       * @return the identifier
93       */
94      public String getIdentifier()
95      {
96          return escape(identifier);
97      }
98  
99      /**
100      * @param identifier
101      *            the identifier to set
102      */
103     public void setIdentifier(String identifier)
104     {
105         this.identifier = identifier;
106     }
107 
108     /**
109      * @return the authDomain
110      */
111     public String getAuthDomain()
112     {
113         return escape(authDomain);
114     }
115 
116     /**
117      * @param authDomain
118      *            the authDomain to set
119      */
120     public void setAuthDomain(String authDomain)
121     {
122         this.authDomain = authDomain;
123     }
124 
125     /**
126      * Formats the identity information into a XML format that the ANDS Persistent Identifier service understands.
127      * 
128      * @param methodName
129      *            the name of the method to be called using this identity information.
130      * @return the identity XML string representing the identity information.
131      */
132     @SuppressWarnings(value = "all")
133     public String toXML(String methodName)
134     {
135         return MessageFormat.format("<request name=\"{0}\">" 
136             + "<properties>" 
137             + "<property name=\"appId\" value=\"" + this.getAppId() + "\"/>" 
138             + "<property name=\"identifier\" value=\"" + this.getIdentifier() + "\"/>"
139             + "<property name=\"authDomain\" value=\"" + this.getAuthDomain() + "\"/>" 
140             + "</properties>" 
141             + "</request>",
142             new Object[] { methodName });
143     }
144 
145     /*
146      * (non-Javadoc)
147      * 
148      * @see java.lang.Object#toString()
149      */
150     @Override
151     public String toString()
152     {
153         return "RequestorIdentity [appId=" + appId + ", authDomain=" + authDomain + ", identifier=" + identifier + "]";
154     }
155 
156     /**
157      * Escape reserved characters, assumes UTF-8 or UTF-16 as encoding.
158      * 
159      * @param in
160      *            the String whose reserved characters we want to remove.
161      * @return the in String, stripped of reserved characters.
162      */
163     public String escape(String in)
164     {
165         if(in == null)
166         {
167             return in;
168         }
169         
170         StringBuffer buffer = new StringBuffer();
171         for (int i = 0; i < in.length(); i++)
172         {
173             char c = in.charAt(i);
174             if (c == '<')
175             {
176                 buffer.append("&lt;");
177             }
178             else if (c == '>')
179             {
180                 buffer.append("&gt;");
181             }
182             else if (c == '&')
183             {
184                 buffer.append("&amp;");
185             }
186             else if (c == '"')
187             {
188                 buffer.append("&quot;");
189             }
190             else if (c == '\'')
191             {
192                 buffer.append("&apos;");
193             }
194             else
195             {
196                 buffer.append(c);
197             }
198         }
199         return buffer.toString();
200     }
201 
202 }