Changeset 5387 for CMDIValidator/trunk/cmdi-validator-core/src/main/java/eu/clarin/cmdi/validator/extensions/CheckHandlesExtension.java
- Timestamp:
- 06/25/14 14:51:13 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
CMDIValidator/trunk/cmdi-validator-core/src/main/java/eu/clarin/cmdi/validator/extensions/CheckHandlesExtension.java
r5384 r5387 17 17 package eu.clarin.cmdi.validator.extensions; 18 18 19 import java.io.IOException; 19 20 import java.net.URI; 20 21 import java.net.URISyntaxException; … … 46 47 private static final Logger logger = 47 48 LoggerFactory.getLogger(CheckHandlesExtension.class); 48 private final int threads;49 49 private final boolean resolveHandles; 50 50 private HandleResolver resolver = null; … … 52 52 53 53 54 public CheckHandlesExtension(int threads, boolean resolveHandles) { 55 if (threads < 1) { 56 throw new IllegalArgumentException("threads < 1"); 57 } 58 this.threads = threads; 54 public CheckHandlesExtension(boolean resolveHandles) { 59 55 this.resolveHandles = resolveHandles; 60 56 } 61 57 62 58 63 public CheckHandlesExtension(boolean resolveHandles) { 64 this(Runtime.getRuntime().availableProcessors(), resolveHandles); 59 public boolean isResolvingHandles() { 60 return resolveHandles; 61 } 62 63 64 public HandleResolver.Statistics getStatistics() { 65 return (resolver != null) ? resolver.getStatistics() : null; 65 66 } 66 67 … … 69 70 protected void doInitialize() throws CMDIValidatorInitException { 70 71 if (resolveHandles) { 71 this.resolver = new HandleResolver( threads);72 this.resolver = new HandleResolver(); 72 73 } 73 74 … … 89 90 selector.setContextItem(document); 90 91 for (XdmItem item : selector) { 91 String handle = item.getStringValue(); 92 if (handle != null) { 93 handle = handle.trim(); 92 String handle = null; 93 final String h = item.getStringValue(); 94 if (h != null) { 95 handle = h.trim(); 94 96 if (handle.isEmpty()) { 95 97 handle = null; 98 } else { 99 if (!handle.equals(h)) { 100 result.reportWarning(-1, -1, "handle '" + h + 101 "' contains leading or tailing spaces " + 102 "within <ResourceRef> element"); 103 } 96 104 } 97 105 } … … 124 132 checkHandleResolves(actionableURI, result); 125 133 } catch (URISyntaxException e) { 134 /* should not happen */ 126 135 throw new CMDIValidatorException( 127 136 "created an invalid URI", e); … … 137 146 } else if (HDL_PROXY_HTTP.equalsIgnoreCase(uri.getScheme()) || 138 147 HDL_PROXY_HTTPS.equalsIgnoreCase(uri.getScheme())) { 139 if (HDL_PROXY_HOST.equalsIgnoreCase(uri.getHost())) { 148 if (uri.getHost() != null) { 149 if (!HDL_PROXY_HOST.equalsIgnoreCase(uri.getHost())) { 150 result.reportError(-1, -1, 151 "The URI of PID '" + handle + 152 "' contains an unexpected host part of '" + 153 uri.getHost() + "'"); 154 } 140 155 checkHandleResolves(uri, result); 141 156 } else { 157 result.reportError(-1, -1, "The URI of PID '" + handle + 158 "' is missing the host part"); 159 } 160 } else { 161 if (uri.getScheme() != null) { 142 162 result.reportError(-1, -1, 143 "PID '" + handle + 144 "' contains an unexpected host part in the URI: " + 145 uri.getHost()); 146 } 147 } else { 148 result.reportError(-1, -1, 149 "PID '" + handle + 150 "' contains an unexpected schema part in the URI: " + 151 uri.getScheme()); 163 "The URI of PID '" + handle + 164 "' contains an unexpected schema part of '" + 165 uri.getScheme() + "'"); 166 } else { 167 result.reportError(-1, -1, "The URI of PID '" + handle + 168 "' is missing a proper schema part"); 169 } 152 170 } 153 171 } catch (URISyntaxException e) { … … 161 179 CMDIValidatorWriteableResult result) throws CMDIValidatorException { 162 180 if (resolver != null) { 163 int code = resolver.resolve(uri); 164 switch (code) { 165 case HttpStatus.SC_OK: 166 /* no special message in this case */ 167 break; 168 case HttpStatus.SC_UNAUTHORIZED: 169 /* FALL-THROUGH */ 170 case HttpStatus.SC_FORBIDDEN: 171 result.reportInfo(-1, -1, "PID '" + uri + 172 "' resolved to an access protected resource (" + code + 173 ")"); 174 break; 175 case HttpStatus.SC_NOT_FOUND: 176 result.reportError(-1, -1, "PID '" + uri + 177 "' resolved to an non-existing resource (" + code + ")"); 178 break; 179 default: 180 result.reportError(-1, -1, "PID '" + uri + 181 "' resolved with an unexpected result (" + code + ")"); 182 break; 181 try { 182 int code = resolver.resolve(uri); 183 switch (code) { 184 case HttpStatus.SC_OK: 185 /* no special message in this case */ 186 break; 187 case HttpStatus.SC_UNAUTHORIZED: 188 /* FALL-THROUGH */ 189 case HttpStatus.SC_FORBIDDEN: 190 result.reportInfo(-1, -1, "PID '" + uri + 191 "' resolved to an access protected resource (" + 192 code + ")"); 193 break; 194 case HttpStatus.SC_NOT_FOUND: 195 result.reportError(-1, -1, "PID '" + uri + 196 "' resolved to an non-existing resource (" + code + 197 ")"); 198 break; 199 case HandleResolver.TIMEOUT: 200 result.reportWarning(-1, -1, "Timeout while resolving PID '" + 201 uri + "'"); 202 break; 203 case HandleResolver.UNKNOWN_HOST: 204 result.reportWarning(-1, -1, "Unable to resolve host '" + 205 uri.getHost() + "' while resolving PID '" + 206 uri + "'"); 207 break; 208 case HandleResolver.ERROR: 209 result.reportWarning(-1, -1, 210 "An error occurred while resolving PID '" + 211 uri + "'"); 212 break; 213 default: 214 result.reportWarning(-1, -1, "PID '" + uri + 215 "' resolved with an unexpected result (" + code + 216 ")"); 217 break; 218 } // switch 219 } catch (IOException e) { 220 throw new CMDIValidatorException( 221 "error while resolving handle '" + uri + "'", e); 183 222 } 184 223 }
Note: See TracChangeset
for help on using the changeset viewer.