List of usage examples for java.lang NumberFormatException getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:com.novartis.opensource.yada.adaptor.JDBCAdaptor.java
/** * Sets a {@code ?i} parameter value mapped to the correct {@link java.sql.Types#INTEGER} JDBC setter. * @param pstmt the statement in which to set the parameter values * @param index the current parameter/* w w w . ja va 2 s . c o m*/ * @param type the data type of the parameter (retained here for logging) * @param val the value to set * @throws SQLException when a parameter cannot be set, for instance if the data type is wrong or unsupported * @since 5.1.0 */ protected void setIntegerParameter(PreparedStatement pstmt, int index, char type, String val) throws SQLException { try { int ival = Integer.parseInt(val); pstmt.setInt(index, ival); } catch (NumberFormatException e) { l.warn("Error: " + e.getMessage() + " caused by " + e.getClass()); l.debug("Setting param [" + String.valueOf(index) + "] of type [" + String.valueOf(type) + "] to: null"); pstmt.setNull(index, java.sql.Types.INTEGER); } catch (NullPointerException e) { l.warn("Error: " + e.getMessage() + " caused by " + e.getClass()); l.debug("Setting param [" + String.valueOf(index) + "] of type [" + String.valueOf(type) + "] to: null"); pstmt.setNull(index, java.sql.Types.INTEGER); } }
From source file:com.clustercontrol.agent.Agent.java
/** * ??awakeAgent?/*from w w w.j a v a 2 s. c o m*/ * Agent.properties???UDP?24005??????????(releaseLatch) * ????ReceiveTopic????Topic???? */ public void waitAwakeAgent() { final int BUFSIZE = 1; byte[] buf = new byte[BUFSIZE]; InetAddress cAddr; // ??IP int cPort; // ??? DatagramSocket sock = null; boolean flag = true; int port = 24005; int awakeDelay = 1000; try { String awakeDelayStr = AgentProperties.getProperty("awake.delay", Integer.toString(1000)); awakeDelay = Integer.parseInt(awakeDelayStr); m_log.info("awake.delay = " + awakeDelay + " msec"); } catch (NumberFormatException e) { m_log.error("awake.delay", e); } while (true) { /* * UDP???flag?true?? * ?????flag?false?????getTopic(releaseLatch)? * * UDP???????getTopic???????? * ?????? */ try { if (sock != null && port != awakePort) { sock.close(); sock = null; } if (sock == null || !sock.isBound()) { port = awakePort; sock = new DatagramSocket(port); sock.setSoTimeout(awakeDelay); } DatagramPacket recvPacket = new DatagramPacket(buf, BUFSIZE); sock.receive(recvPacket); cAddr = recvPacket.getAddress(); cPort = recvPacket.getPort(); flag = true; m_log.info("waitAwakeAgent (" + cAddr.getHostAddress() + " onPort=" + cPort + ") buf.length=" + buf.length); } catch (SocketTimeoutException e) { if (flag) { m_log.info("waitAwakeAgent packet end"); m_receiveTopic.releaseLatch(); flag = false; } } catch (Exception e) { String msg = "waitAwakeAgent port=" + awakePort + ", " + e.getClass().getSimpleName() + ", " + e.getMessage(); if (e instanceof BindException) { m_log.warn(msg); } else { m_log.warn(msg, e); } try { Thread.sleep(60 * 1000); } catch (InterruptedException e1) { m_log.warn(e1, e1); } } } }
From source file:pl.umk.mat.zawodyweb.compiler.classes.LanguageLA.java
@Override public TestOutput runTest(String path, TestInput input) { TestOutput result = new TestOutput(null); Random random = new Random(); String login = properties.getProperty("livearchive.login"); String password = properties.getProperty("livearchive.password"); int inQueue;/*from w ww. j a v a 2 s.co m*/ try { inQueue = Integer.parseInt(properties.getProperty("livearchive.inqueue")); } catch (NumberFormatException e) { inQueue = 4; } long maxTime; try { maxTime = Long.parseLong(properties.getProperty("livearchive.max_time")); } catch (NumberFormatException e) { maxTime = 10L * 60; } prepareHttpClient(); try { logIn(login, password); logger.info("Logged to LA-ACM"); for (int i = 3; i >= 0; --i) { if (checkInQueueStatus(input.getInputText()) > inQueue) { if (i == 0) { result.setStatus(ResultsStatusEnum.UNDEF.getCode()); result.setNotes( "More than " + inQueue + " submissions of " + input.getInputText() + " in queue."); result.setOutputText("LA-ACM judge broken?"); logger.info(result.getNotes()); return result; } else { Thread.sleep(10000 + (Math.abs(random.nextInt()) % 5000)); continue; } } break; } String msg = sendSolution(path, input); int id = 0; try { id = Integer.parseInt(msg); } catch (NumberFormatException ex) { } if (id == 0) { result.setStatus(ResultsStatusEnum.RV.getCode()); result.setNotes(msg); return result; } else { logger.info("LA-ACM Submit id = " + id); checkResults(id, maxTime, input, result); } logOut(); logger.info("Logged out from LA-ACM"); } catch (Exception e) { logger.info("Exception: ", e); result.setStatus(ResultsStatusEnum.UNDEF.getCode()); result.setNotes(e.getMessage()); result.setOutputText(e.getClass().getName()); return result; } return result; }
From source file:pl.umk.mat.zawodyweb.compiler.classes.LanguageUVA.java
@Override public TestOutput runTest(String path, TestInput input) { TestOutput result = new TestOutput(null); Random random = new Random(); String login = properties.getProperty("uva.login"); String password = properties.getProperty("uva.password"); int inQueue;/*from w ww . j av a2 s. c om*/ try { inQueue = Integer.parseInt(properties.getProperty("uva.inqueue")); } catch (NumberFormatException e) { inQueue = 4; } long maxTime; try { maxTime = Long.parseLong(properties.getProperty("uva.max_time")); } catch (NumberFormatException e) { maxTime = 10L * 60; } prepareHttpClient(); try { logIn(login, password); logger.info("Logged to UVa-ACM"); for (int i = 3; i >= 0; --i) { if (checkInQueueStatus(input.getInputText()) > inQueue) { if (i == 0) { result.setStatus(ResultsStatusEnum.UNDEF.getCode()); result.setNotes( "More than " + inQueue + " submissions of " + input.getInputText() + " in queue."); result.setOutputText("UVa-ACM judge broken?"); logger.info(result.getNotes()); return result; } else { Thread.sleep(10000 + (Math.abs(random.nextInt()) % 5000)); continue; } } break; } String msg = sendSolution(path, input); int id = 0; try { id = Integer.parseInt(msg); } catch (NumberFormatException ex) { logger.info("Unable to get UVa-ACM Submit id. Message: " + msg); } if (id == 0) { result.setStatus(ResultsStatusEnum.RV.getCode()); result.setNotes(msg); return result; } else { logger.info("UVa-ACM Submit id = " + id); checkResults(id, maxTime, input, result); } logOut(); logger.info("Logged out from UVa-ACM"); } catch (Exception e) { logger.info("Exception: ", e); result.setStatus(ResultsStatusEnum.UNDEF.getCode()); result.setNotes(e.getMessage()); result.setOutputText(e.getClass().getName()); return result; } return result; }
From source file:org.apache.shindig.gadgets.http.BasicHttpFetcher.java
public HttpResponse fetch(org.apache.shindig.gadgets.http.HttpRequest request) throws GadgetException { HttpUriRequest httpMethod = null;/*from ww w. ja v a2 s . c om*/ Preconditions.checkNotNull(request); final String methodType = request.getMethod(); final org.apache.http.HttpResponse response; final long started = System.currentTimeMillis(); // Break the request Uri to its components: Uri uri = request.getUri(); if (StringUtils.isEmpty(uri.getAuthority())) { throw new GadgetException(GadgetException.Code.INVALID_USER_DATA, "Missing domain name for request: " + uri, HttpServletResponse.SC_BAD_REQUEST); } if (StringUtils.isEmpty(uri.getScheme())) { throw new GadgetException(GadgetException.Code.INVALID_USER_DATA, "Missing schema for request: " + uri, HttpServletResponse.SC_BAD_REQUEST); } String[] hostparts = StringUtils.splitPreserveAllTokens(uri.getAuthority(), ':'); int port = -1; // default port if (hostparts.length > 2) { throw new GadgetException(GadgetException.Code.INVALID_USER_DATA, "Bad host name in request: " + uri.getAuthority(), HttpServletResponse.SC_BAD_REQUEST); } if (hostparts.length == 2) { try { port = Integer.parseInt(hostparts[1]); } catch (NumberFormatException e) { throw new GadgetException(GadgetException.Code.INVALID_USER_DATA, "Bad port number in request: " + uri.getAuthority(), HttpServletResponse.SC_BAD_REQUEST); } } String requestUri = uri.getPath(); // Treat path as / if set as null. if (uri.getPath() == null) { requestUri = "/"; } if (uri.getQuery() != null) { requestUri += '?' + uri.getQuery(); } // Get the http host to connect to. HttpHost host = new HttpHost(hostparts[0], port, uri.getScheme()); try { if ("POST".equals(methodType) || "PUT".equals(methodType)) { HttpEntityEnclosingRequestBase enclosingMethod = ("POST".equals(methodType)) ? new HttpPost(requestUri) : new HttpPut(requestUri); if (request.getPostBodyLength() > 0) { enclosingMethod .setEntity(new InputStreamEntity(request.getPostBody(), request.getPostBodyLength())); } httpMethod = enclosingMethod; } else if ("GET".equals(methodType)) { httpMethod = new HttpGet(requestUri); } else if ("HEAD".equals(methodType)) { httpMethod = new HttpHead(requestUri); } else if ("DELETE".equals(methodType)) { httpMethod = new HttpDelete(requestUri); } for (Map.Entry<String, List<String>> entry : request.getHeaders().entrySet()) { httpMethod.addHeader(entry.getKey(), StringUtils.join(entry.getValue(), ',')); } // Disable following redirects. if (!request.getFollowRedirects()) { httpMethod.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); } // HttpClient doesn't handle all cases when breaking url (specifically '_' in domain) // So lets pass it the url parsed: response = FETCHER.execute(host, httpMethod); if (response == null) { throw new IOException("Unknown problem with request"); } long now = System.currentTimeMillis(); if (now - started > slowResponseWarning) { slowResponseWarning(request, started, now); } return makeResponse(response); } catch (Exception e) { long now = System.currentTimeMillis(); // Find timeout exceptions, respond accordingly if (TIMEOUT_EXCEPTIONS.contains(e.getClass())) { LOG.info("Timeout for " + request.getUri() + " Exception: " + e.getClass().getName() + " - " + e.getMessage() + " - " + (now - started) + "ms"); return HttpResponse.timeout(); } LOG.log(Level.INFO, "Got Exception fetching " + request.getUri() + " - " + (now - started) + "ms", e); // Separate shindig error from external error throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e, HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } finally { // cleanup any outstanding resources.. if (httpMethod != null) try { httpMethod.abort(); } catch (UnsupportedOperationException e) { // ignore } } }
From source file:org.openhab.binding.modbus.internal.ModbusBindingConfig.java
private void parseExtended(String config) throws BindingConfigParseException { List<String> definitions = new SimpleTokenizer(']').parse(config); for (String origBindingDefinition : definitions) { String bindingDefinition = origBindingDefinition.trim(); if (bindingDefinition.isEmpty()) { // end of string continue; } else if (bindingDefinition.startsWith(",")) { bindingDefinition = bindingDefinition.substring(1).trim(); }/* ww w .j a v a2 s .c o m*/ try { String[] colonSplitted = bindingDefinition.split(Pattern.quote("["), 2)[1].split(":", 3); boolean read = bindingDefinition.charAt(0) == '<'; String slaveName = colonSplitted[0].trim(); int index; try { index = Integer.valueOf(colonSplitted[1].trim()); } catch (NumberFormatException e) { throw new BindingConfigParseException(String.format( "Could not parse '%s' as number. Please check item config syntax.", colonSplitted[1])); } List<String> tokens = colonSplitted.length > 2 ? new SimpleTokenizer(',').parse(colonSplitted[2].trim()) : Arrays.asList(); assertValidConfigurationKeys(tokens); IOType type; // Only "default" type supported. In the future we could add support to it a la mqtt (see constructor). String typeString = "default"; // findValueMatchingKey(tokens, "type", "default").trim().toUpperCase(); if ("default".equalsIgnoreCase(typeString)) { if (read) { type = IOType.STATE; } else { type = IOType.COMMAND; } } else { try { type = IOType.valueOf(typeString); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(String.format("Could not convert '%s' to one of %s", findValueMatchingKey(tokens, "type", "default"), Arrays.toString(IOType.values())), e); } } String trigger = findValueMatchingKey(tokens, "trigger", ItemIOConnection.TRIGGER_DEFAULT).trim(); String transformationString = StringEscapeUtils.unescapeJava( findValueMatchingKey(tokens, "transformation", Transformation.TRANSFORM_DEFAULT).trim()); Transformation transformation = new Transformation(transformationString); String valueType = findValueMatchingKey(tokens, "valueType", ItemIOConnection.VALUETYPE_DEFAULT); if (!"default".equalsIgnoreCase(valueType) && !Arrays.asList(ModbusBindingProvider.VALUE_TYPES).contains(valueType)) { throw new BindingConfigParseException( String.format("valuetype '%s' does not match expected: '%s or 'default'", valueType, String.join(", ", ModbusBindingProvider.VALUE_TYPES))); } ItemIOConnection connection = new ItemIOConnection(slaveName, index, type, trigger, transformation, valueType); if (read) { readConnections.add(connection); } else { writeConnections.add(connection); } logger.debug("Parsed IOConnection (read={}) for item '{}': {}", read, itemName, connection); } catch (Exception e) { String msg = String.format( "Parsing of item '%s' configuration '%s]' (as part of the whole config '%s') failed: %s %s", itemName, origBindingDefinition, config, e.getClass().getName(), e.getMessage()); logger.error(msg, e); BindingConfigParseException exception = new BindingConfigParseException(msg); exception.initCause(e); throw exception; } } }
From source file:org.signserver.client.cli.validationservice.ValidateCertificateCommand.java
@Override public int execute(String... args) throws IllegalCommandArgumentsException, CommandFailureException { int result = RETURN_BADARGUMENT; try {//from w w w.j a v a 2 s . c o m SignServerUtil.installBCProvider(); CommandLineParser parser = new GnuParser(); try { CommandLine cmd = parser.parse(options, args); if (cmd.hasOption(OPTION_HELP)) { printUsage(); return RETURN_BADARGUMENT; } silentMode = cmd.hasOption(OPTION_SILENT); derFlag = cmd.hasOption(OPTION_DER); pemFlag = cmd.hasOption(OPTION_PEM); if (derFlag && pemFlag) { err.println("Error, only one of -pem and -der options can be specified."); printUsage(); return RETURN_BADARGUMENT; } if (!derFlag) { pemFlag = true; } if (cmd.hasOption(OPTION_SERVICE) && cmd.getOptionValue(OPTION_SERVICE) != null) { service = cmd.getOptionValue(OPTION_SERVICE); } else { err.println("Error, an name or id of the validation service must be specified with the -" + OPTION_SERVICE + " option."); printUsage(); return RETURN_BADARGUMENT; } if (cmd.hasOption(OPTION_TRUSTSTORE)) { trustStorePath = cmd.getOptionValue(OPTION_TRUSTSTORE); if (trustStorePath != null) { File f = new File(trustStorePath); if (!f.exists() || !f.canRead() || f.isDirectory()) { err.println("Error, a path to the truststore must point to a readable JKS file."); printUsage(); return RETURN_BADARGUMENT; } } else { err.println("Error, a path to the truststore must be supplied to the -" + OPTION_TRUSTSTORE + " option."); printUsage(); return RETURN_BADARGUMENT; } } if (cmd.hasOption(OPTION_TRUSTSTOREPWD)) { trustStorePwd = cmd.getOptionValue(OPTION_TRUSTSTOREPWD); if (trustStorePwd == null) { err.println("Error, a truststore password must be supplied to the -" + OPTION_TRUSTSTOREPWD + " option."); printUsage(); return RETURN_BADARGUMENT; } } if (trustStorePath == null ^ trustStorePwd == null) { err.println("Error, if HTTPS is going to be used must both the options -" + OPTION_TRUSTSTORE + " and -" + OPTION_TRUSTSTOREPWD + " be specified"); printUsage(); return RETURN_BADARGUMENT; } useSSL = trustStorePath != null; if (cmd.hasOption(OPTION_HOSTS) && cmd.getOptionValue(OPTION_HOSTS) != null) { hosts = cmd.getOptionValue(OPTION_HOSTS).split(","); } else { err.println("Error, at least one validation service host must be specified."); printUsage(); return RETURN_BADARGUMENT; } if (cmd.hasOption(OPTION_PORT)) { String portString = cmd.getOptionValue(OPTION_PORT); if (portString != null) { try { port = Integer.parseInt(portString); } catch (NumberFormatException e) { err.println("Error, port value must be an integer for option -" + OPTION_PORT + "."); printUsage(); return RETURN_BADARGUMENT; } } else { err.println("Error, a port value must be supplied to the -" + OPTION_PORT + " option."); printUsage(); return RETURN_BADARGUMENT; } } else { if (useSSL) { port = DEFAULT_SSLPORT; } else { port = DEFAULT_PORT; } } if (cmd.hasOption(OPTION_CERTPURPOSES)) { if (cmd.getOptionValue(OPTION_CERTPURPOSES) != null) { usages = cmd.getOptionValue(OPTION_CERTPURPOSES); } else { err.println("Error, at least one usage must be specified with the -" + OPTION_CERTPURPOSES + " option."); printUsage(); return RETURN_BADARGUMENT; } } if (cmd.hasOption(OPTION_CERT) && cmd.getOptionValue(OPTION_CERT) != null) { certPath = new File(cmd.getOptionValue(OPTION_CERT)); if (!certPath.exists() || !certPath.canRead() || certPath.isDirectory()) { err.println("Error, the certificate file must exist and be readable by the user."); printUsage(); return RETURN_BADARGUMENT; } } else { err.println("Error, the certificate to validate must be specified with the -" + OPTION_CERT + " option."); printUsage(); return RETURN_BADARGUMENT; } // set the default servlet URL value servlet = SignServerWSClientFactory.DEFAULT_WSDL_URL; if (cmd.hasOption(OPTION_SERVLET) && cmd.getOptionValue(OPTION_SERVLET) != null) { servlet = cmd.getOptionValue(OPTION_SERVLET); } if (cmd.hasOption(OPTION_PROTOCOL)) { protocol = Protocol.valueOf(cmd.getOptionValue(OPTION_PROTOCOL)); // override default servlet URL (if not set manually) for HTTP if (Protocol.HTTP.equals(protocol) && !cmd.hasOption(OPTION_SERVLET)) { servlet = "/signserver/process"; } } else { protocol = Protocol.WEBSERVICES; } } catch (ParseException e) { err.println("Error occurred when parsing options. Reason: " + e.getMessage()); printUsage(); return RETURN_BADARGUMENT; } if (args.length < 1) { printUsage(); return RETURN_BADARGUMENT; } result = run(); } catch (Exception e) { if (!e.getClass().getSimpleName().equals("ExitException")) { err.println("Error occured during validation : " + e.getClass().getName()); if (e.getMessage() != null) { err.println(" Message : " + e.getMessage()); } result = RETURN_ERROR; } } return result; }
From source file:com.twinsoft.convertigo.engine.Engine.java
/** * Retrieves the XML document according to the given context. * //from w w w . j a v a 2s . c o m * @param requester * the calling requester. * @param context * the request context. * * @return the generated XML document. */ public Document getDocument(Requester requester, Context context) throws EngineException { Document outputDom = null; String t = context.statistics.start(EngineStatistics.GET_DOCUMENT); try { Engine.logContext.trace("Engine.getDocument: started"); // Are we in the studio context? if (isStudioMode()) { Engine.logContext.debug("The requested object will be processed in the studio context."); } // Checking whether the asynchronous mode has been requested. if ((context.isAsync) && (JobManager.jobExists(context.contextID))) { Engine.logContext .debug("The requested object is working and is asynchronous; requesting job status..."); HttpServletRequest request = (HttpServletRequest) requester.inputData; if (request.getParameter(Parameter.Abort.getName()) != null) { Engine.logContext.debug("Job abortion has been required"); return JobManager.abortJob(context.contextID); } return JobManager.getJobStatus(context.contextID); } // Loading project if (context.projectName == null) throw new EngineException("The project name has been specified!"); // Checking whether the asynchronous mode has been requested. if ((context.isAsync) && (JobManager.jobExists(context.contextID))) { Engine.logContext .debug("The requested object is working and is asynchronous; requesting job status..."); HttpServletRequest request = (HttpServletRequest) requester.inputData; if (request.getParameter(Parameter.Abort.getName()) != null) { Engine.logContext.debug("Job abortion has been required"); return JobManager.abortJob(context.contextID); } return JobManager.getJobStatus(context.contextID); } // Loading project if (context.projectName == null) throw new EngineException("The project name has been specified!"); Project currentProject; if (isStudioMode()) { if (objectsProvider == null) { throw new EngineException( "Is the Projects view opened in the Studio? Failed to load: " + context.projectName); } currentProject = objectsProvider.getProject(context.projectName); if (currentProject == null) { throw new EngineException( "No project has been opened in the Studio. A project should be opened in the Studio in order that the Convertigo engine can work."); } else if (!currentProject.getName().equalsIgnoreCase(context.projectName)) { throw new EngineException("The requested project (\"" + context.projectName + "\") does not match with the opened project (\"" + currentProject.getName() + "\") in the Studio.\nYou cannot make a request on a different project than the one opened in the Studio."); } Engine.logContext.debug("Using project from Studio"); context.project = currentProject; } else { if ((context.project == null) || (context.isNewSession)) { Engine.logEngine.debug("New project requested: '" + context.projectName + "'"); context.project = Engine.theApp.databaseObjectsManager.getProjectByName(context.projectName); Engine.logContext.debug("Project loaded: " + context.project.getName()); } } context.project.checkSymbols(); String corsOrigin = context.project.getCorsOrigin(); if (StringUtils.isNotBlank(corsOrigin)) { context.setResponseHeader("Access-Control-Allow-Origin", corsOrigin); } // Loading sequence if (context.sequenceName != null) { context.loadSequence(); } else { // Loading connector context.loadConnector(); // Loading transaction // Load default transaction if no overidden transaction is // provided if (context.transactionName == null) { context.requestedObject = context.getConnector().getDefaultTransaction(); context.transaction = (Transaction) context.requestedObject; context.transactionName = context.requestedObject.getName(); Engine.logContext.debug("Default transaction loaded: " + context.transactionName); } // Try to load overriden transaction else { context.requestedObject = context.getConnector().getTransactionByName(context.transactionName); context.transaction = (Transaction) context.requestedObject; if (context.requestedObject == null) { throw new EngineException("Unknown transaction \"" + context.transactionName + "\""); } Engine.logContext.debug("Transaction loaded: " + context.requestedObject.getName()); } context.transaction.checkSymbols(); if (context.getConnector().isTasAuthenticationRequired()) { if (context.tasSessionKey == null) { throw new EngineException( "A Carioca authentication is required in order to process the transaction."); } else { // Checking VIC information if needed if (context.isRequestFromVic) { Engine.logContext.debug("[Engine.getDocument()] Checking VIC session key"); String s = Crypto2.decodeFromHexString(context.tasSessionKey); int i = s.indexOf(','); if (i == -1) throw new EngineException("Unable to decrypt the VIC session key (reason: #1)!"); try { long t0 = Long.parseLong(s.substring(0, i)); Engine.logContext.debug("[VIC key check] t0=" + t0); long t1 = System.currentTimeMillis(); Engine.logContext.debug("[VIC key check] t1=" + t1); long d = Math.abs(t1 - t0); Engine.logContext.debug("[VIC key check] d=" + d); String user = s.substring(i + 1); Engine.logContext.debug("[VIC key check] user: " + user); long deltaT = 1000 * 60 * 10; if (d > deltaT) throw new EngineException("The VIC session key has expired."); if (!user.equals(context.tasUserName)) throw new EngineException("Wrong user name!"); } catch (NumberFormatException e) { throw new EngineException("Unable to decrypt the VIC session key (reason: #2)!"); } Engine.logContext.debug("[VIC key check] VIC session key OK"); } // Checking Carioca session key else if (context.isTrustedRequest) { if (!context.tasSessionKeyVerified) { Engine.logContext.debug("[Engine.getDocument()] Checking Carioca session key"); TWSKey twsKey = new TWSKey(); twsKey.CreateKey(1); int cariocaSessionKeyLifeTime = 60; try { cariocaSessionKeyLifeTime = Integer .parseInt(EnginePropertiesManager.getProperty( EnginePropertiesManager.PropertyName.CARIOCA_SESSION_KEY_LIFE_TIME)); } catch (NumberFormatException e) { Engine.logContext.warn( "The Carioca session key life time value is not valid (not a number)! Setting default to 60s."); } Engine.logContext.debug("Carioca session key lifetime: " + cariocaSessionKeyLifeTime + " second(s)"); String result = checkCariocaSessionKey(context, context.tasSessionKey, context.tasServiceCode, 0, cariocaSessionKeyLifeTime); if (result != null) throw new EngineException(result); Engine.logContext.debug("[ContextManager] Carioca session key OK"); context.tasSessionKeyVerified = true; } } } } } // Check requestable accessibility requester.checkAccessibility(); // Check requestable access policy requester.checkSecuredConnection(); // Check authenticated context requirement requester.checkAuthenticatedContext(); requester.checkParentContext(); RequestableObject requestedObject = context.requestedObject; String contextResponseExpiryDate = (String) context.get(Parameter.ResponseExpiryDate.getName()); String oldResponseExpiryDate = null; if (contextResponseExpiryDate != null) { oldResponseExpiryDate = requestedObject.getResponseExpiryDate(); requestedObject.setResponseExpiryDate(contextResponseExpiryDate); context.remove(Parameter.ResponseExpiryDate.getName()); } try { if (context.isAsync) { outputDom = JobManager.addJob(cacheManager, requestedObject, requester, context); } else { outputDom = cacheManager.getDocument(requester, context); } } finally { if (oldResponseExpiryDate != null) { requestedObject.setResponseExpiryDate(oldResponseExpiryDate); } } Element documentElement = outputDom.getDocumentElement(); documentElement.setAttribute("version", Version.fullProductVersion); documentElement.setAttribute("context", context.name); documentElement.setAttribute("contextId", context.contextID); DatabaseObject lastDetectedObject = (DatabaseObject) context.lastDetectedObject; if (lastDetectedObject != null) { documentElement.setAttribute("screenclass", lastDetectedObject.getName()); // TODO : // documentElement.setAttribute(lastDetectedObject.getClass().getName().toLowerCase(), // lastDetectedObject.getName()); } context.documentSignatureSent = System.currentTimeMillis(); documentElement.setAttribute("signature", Long.toString(context.documentSignatureSent)); // Add the user reference if any if (context.userReference != null) { documentElement.setAttribute("userReference", context.userReference); } fireDocumentGenerated(new RequestableEngineEvent(outputDom, context.projectName, context.sequenceName, context.connectorName)); } catch (EngineException e) { String message = "[Engine.getDocument()] Context ID#" + context.contextID + " - Unable to build the XML document."; message += "\n[" + e.getClass().getName() + "] " + e.getMessage(); if (System.getProperty("java.specification.version").compareTo("1.4") >= 0) { Throwable eCause = e; while ((eCause = eCause.getCause()) != null) { if (!(eCause instanceof ConvertigoException)) { message += "\n" + Log.getStackTrace(eCause); } else { message += "\n[" + eCause.getClass().getName() + "] " + eCause.getMessage(); } } } Engine.logContext.error(message); // Just re-throw the exception throw (EngineException) e; } catch (Throwable e) { Engine.logEngine.error("Context ID#" + context.contextID + " - An unexpected error has occured while building the XML document.", e); throw new EngineException("An unexpected error has occured while building the XML document." + "Please contact Convertigo support, providing the following information:", e); } finally { if (context.requestedObject != null) { Engine.logContext.debug("Requested object is billable: " + context.requestedObject.isBillable()); if (context.requestedObject.isBillable()) { // Fix regression for Teamlog's billing abortion! // In case of exception thrown, outputDom is Null so use // context.outputDocument! if (context.outputDocument == null) { Engine.logContext.warn("Billing aborted because the generated XML document is null"); } else { String billingClassName = context.getConnector().getBillingClassName(); try { Engine.logContext.debug("Billing class name required: " + billingClassName); AbstractBiller biller = (AbstractBiller) Class.forName(billingClassName).newInstance(); Engine.logContext.debug("Executing the biller"); biller.insertBilling(context); } catch (Throwable e) { Engine.logContext.warn("Unable to execute the biller (the billing is thus ignored): [" + e.getClass().getName() + "] " + e.getMessage()); } } } } context.statistics.stop(t); if (context.requestedObject != null) { try { Engine.theApp.billingManager.insertBilling(context); } catch (Exception e) { Engine.logContext.warn("Unable to insert billing ticket (the billing is thus ignored): [" + e.getClass().getName() + "] " + e.getMessage()); } } Engine.logContext.trace("Engine.getDocument: finished"); } XMLUtils.logXml(outputDom, Engine.logContext, "Generated XML"); return outputDom; }
From source file:caarray.client.test.gui.GuiMain.java
public GuiMain() throws Exception { JFrame frame = new JFrame("API Test Suite"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout(6, 6)); JPanel topPanel = new JPanel(); /* ###### Text fields for modifiable parameters ##### */ final JTextField javaHostText = new JTextField(TestProperties.getJavaServerHostname(), 20); JLabel javaHostLabel = new JLabel("Java Service Host"); JButton save = new JButton("Save"); save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { TestProperties.setJavaServerHostname(javaHostText.getText()); }/*from ww w. j a va 2 s. co m*/ }); JLabel javaPortLabel = new JLabel("Java Port"); final JTextField javaPortText = new JTextField(Integer.toString(TestProperties.getJavaServerJndiPort()), 5); JButton save2 = new JButton("Save"); save2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { int port = Integer.parseInt(javaPortText.getText()); TestProperties.setJavaServerJndiPort(port); } catch (NumberFormatException e) { System.out.println(javaPortText.getText() + " is not a valid port number."); } } }); JLabel gridHostLabel = new JLabel("Grid Service Host"); final JTextField gridHostText = new JTextField(TestProperties.getGridServerHostname(), 20); JButton save3 = new JButton("Save"); save3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { TestProperties.setGridServerHostname(gridHostText.getText()); } }); JLabel gridPortLabel = new JLabel("Grid Service Port"); final JTextField gridPortText = new JTextField(Integer.toString(TestProperties.getGridServerPort()), 5); JButton save4 = new JButton("Save"); save4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { int port = Integer.parseInt(gridPortText.getText()); TestProperties.setGridServerPort(port); } catch (NumberFormatException e) { System.out.println(gridPortText.getText() + " is not a valid port number."); } } }); JLabel excludeLabel = new JLabel("Exclude test cases (comma-separated list):"); final JTextField excludeText = new JTextField("", 30); JButton save5 = new JButton("Save"); save5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String testString = excludeText.getText(); if (testString != null) { String[] testCases = testString.split(","); if (testCases != null && testCases.length > 0) { List<Float> tests = new ArrayList<Float>(); for (String test : testCases) { try { tests.add(Float.parseFloat(test)); } catch (NumberFormatException e) { System.out.println(test + " is not a valid test case."); } } TestProperties.setExcludedTests(tests); } } } }); JLabel includeLabel = new JLabel("Include only (comma-separated list):"); final JTextField includeText = new JTextField("", 30); JButton save6 = new JButton("Save"); save6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String testString = includeText.getText(); if (testString != null) { String[] testCases = testString.split(","); if (testCases != null && testCases.length > 0) { List<Float> tests = new ArrayList<Float>(); for (String test : testCases) { try { tests.add(Float.parseFloat(test)); } catch (NumberFormatException e) { System.out.println(test + " is not a valid test case."); } } TestProperties.setIncludeOnlyTests(tests); } } } }); JLabel threadLabel = new JLabel("Number of threads:"); final JTextField threadText = new JTextField(Integer.toString(TestProperties.getNumThreads()), 5); JButton save7 = new JButton("Save"); save7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { int threads = Integer.parseInt(threadText.getText()); TestProperties.setNumThreads(threads); } catch (NumberFormatException e) { System.out.println(threadText.getText() + " is not a valid thread number."); } } }); GridBagLayout topLayout = new GridBagLayout(); topPanel.setLayout(topLayout); JLabel[] labels = new JLabel[] { javaHostLabel, javaPortLabel, gridHostLabel, gridPortLabel, excludeLabel, includeLabel, threadLabel }; JTextField[] textFields = new JTextField[] { javaHostText, javaPortText, gridHostText, gridPortText, excludeText, includeText, threadText }; JButton[] buttons = new JButton[] { save, save2, save3, save4, save5, save6, save7 }; for (int i = 0; i < labels.length; i++) { GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.NONE; c.gridx = 0; c.gridy = i; topPanel.add(labels[i], c); c.gridx = 1; topPanel.add(textFields[i], c); c.gridx = 2; topPanel.add(buttons[i], c); } frame.getContentPane().add(topPanel, BorderLayout.PAGE_START); GridLayout bottomLayout = new GridLayout(0, 4); selectionPanel.setLayout(bottomLayout); JCheckBox selectAll = new JCheckBox("Select/Deselect All"); selectAll.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JCheckBox box = (JCheckBox) e.getSource(); selectAll(box.isSelected()); } }); selectionPanel.add(selectAll); JCheckBox excludeLongTests = new JCheckBox("Exclude Long Tests"); excludeLongTests.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JCheckBox box = (JCheckBox) e.getSource(); if (box.isSelected()) { TestProperties.excludeLongTests(); } else { TestProperties.removeExcludedLongTests(); } } }); TestProperties.excludeLongTests(); excludeLongTests.setSelected(true); selectionPanel.add(excludeLongTests); //Initialize check boxes corresponding to test categories initializeTests(); centerPanel.setLayout(new GridLayout(0, 1)); centerPanel.add(selectionPanel); //Redirect System messages to gui JScrollPane textScroll = new JScrollPane(); textScroll.setViewportView(textDisplay); System.setOut(new PrintStream(new JTextAreaOutputStream(textDisplay))); System.setErr(new PrintStream(new JTextAreaOutputStream(textDisplay))); centerPanel.add(textScroll); JScrollPane scroll = new JScrollPane(centerPanel); frame.getContentPane().add(scroll, BorderLayout.CENTER); JButton runButton = new JButton("Run Tests"); runButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { runTests(); } catch (Exception e) { System.out.println("An error occured executing the tests: " + e.getClass() + ". Check error log for details."); log.error("Exception encountered:", e); } } }); JPanel bottomPanel = new JPanel(); bottomPanel.add(runButton); frame.getContentPane().add(bottomPanel, BorderLayout.PAGE_END); frame.pack(); frame.setVisible(true); }
From source file:edu.ucsd.library.xdre.web.CollectionOperationController.java
public static String handleProcesses(Map<String, String[]> paramsMap, HttpSession session) throws Exception { String message = ""; String returnMessage = ""; DAMSClient damsClient = null;/*from w w w . j av a2 s.com*/ String collectionId = getParameter(paramsMap, "category"); boolean[] operations = new boolean[20]; operations[0] = getParameter(paramsMap, "validateFileCount") != null; operations[1] = getParameter(paramsMap, "validateChecksums") != null; operations[2] = getParameter(paramsMap, "rdfImport") != null; operations[3] = getParameter(paramsMap, "createDerivatives") != null; operations[4] = getParameter(paramsMap, "collectionRelease") != null; operations[5] = getParameter(paramsMap, "externalImport") != null; operations[6] = getParameter(paramsMap, "marcModsImport") != null || getParameter(paramsMap, "excelImport") != null; operations[7] = getParameter(paramsMap, "luceneIndex") != null || getParameter(paramsMap, "solrDump") != null || getParameter(paramsMap, "solrRecordsDump") != null; operations[8] = getParameter(paramsMap, "sendToCDL") != null; operations[9] = getParameter(paramsMap, "dataConvert") != null; operations[10] = getParameter(paramsMap, "ingest") != null; operations[11] = getParameter(paramsMap, "serialize") != null; operations[12] = getParameter(paramsMap, "tsSyn") != null; operations[13] = getParameter(paramsMap, "createJson") != null; operations[14] = getParameter(paramsMap, "cacheJson") != null; operations[15] = getParameter(paramsMap, "fileUpload") != null; operations[16] = getParameter(paramsMap, "jsonDiffUpdate") != null; operations[17] = getParameter(paramsMap, "validateManifest") != null; operations[18] = getParameter(paramsMap, "metadataExport") != null; operations[19] = getParameter(paramsMap, "jhoveReport") != null; int submissionId = (int) System.currentTimeMillis(); String logLink = "https://" + (Constants.CLUSTER_HOST_NAME.indexOf("localhost") >= 0 ? "localhost:8443" : Constants.CLUSTER_HOST_NAME.indexOf("lib-ingest") >= 0 ? Constants.CLUSTER_HOST_NAME + ".ucsd.edu:8443" : Constants.CLUSTER_HOST_NAME + ".ucsd.edu") + "/damsmanager/downloadLog.do?submissionId=" + submissionId; String dataLink = ""; String ds = getParameter(paramsMap, "ts"); String dsDest = null; if ((ds == null || (ds = ds.trim()).length() == 0) && !(operations[15] || operations[16])) ds = Constants.DEFAULT_TRIPLESTORE; else if (operations[12]) { dsDest = getParameter(paramsMap, "dsDest"); if (dsDest == null) throw new ServletException("No destination triplestore data source provided..."); else if (ds.equals(dsDest) || !dsDest.startsWith("ts/")) throw new ServletException("Can't sync triplestore from " + ds + " to destination " + dsDest + "."); } String fileStore = getParameter(paramsMap, "fs"); damsClient = new DAMSClient(Constants.DAMS_STORAGE_URL); damsClient.setTripleStore(ds); damsClient.setFileStore(fileStore); damsClient.setUser((String) session.getAttribute("user")); String clientVersion = session.getServletContext().getInitParameter("src-version"); String clientTool = "Custom"; if (message.length() == 0) { int userId = -1; String userIdAttr = (String) session.getAttribute("employeeId"); if (userIdAttr != null && userIdAttr.length() > 0) { try { userId = Integer.parseInt(userIdAttr); } catch (NumberFormatException e) { userId = -1; } } CollectionHandler handler = null; OutputStream fileOut = null; try { boolean successful = true; for (int i = 0; i < operations.length; i++) { handler = null; String exeInfo = ""; if (operations[i]) { String opMessage = "Preparing procedure "; RequestOrganizer.setProgressPercentage(session, 0); message = ""; if (i == 0) { session.setAttribute("status", opMessage + "File Count Validation for FileStore " + fileStore + " ..."); boolean ingestFile = getParameter(paramsMap, "ingestFile") != null; boolean dams4FileRename = getParameter(paramsMap, "dams4FileRename") != null; handler = new FileCountValidaionHandler(damsClient, collectionId); ((FileCountValidaionHandler) handler).setDams4FileRename(dams4FileRename); if (ingestFile) { String[] filesPaths = getParameter(paramsMap, "filesLocation").split(";"); List<String> ingestFiles = new ArrayList<String>(); for (int j = 0; j < filesPaths.length; j++) ingestFiles.add(new File(Constants.DAMS_STAGING + "/" + filesPaths[j]) .getAbsolutePath()); ((FileCountValidaionHandler) handler).setIngestFile(ingestFile); ((FileCountValidaionHandler) handler) .setFilesPaths(ingestFiles.toArray(new String[ingestFiles.size()])); } } else if (i == 1) { session.setAttribute("status", opMessage + "Checksum Validation for FileStore " + fileStore + " ..."); handler = new ChecksumsHandler(damsClient, collectionId, null); } else if (i == 2) { session.setAttribute("status", opMessage + "Importing metadata ..."); String dataFormat = getParameter(paramsMap, "dataFormat"); String importMode = getParameter(paramsMap, "importMode"); handler = new MetadataImportHandler(damsClient, collectionId, getParameter(paramsMap, "data"), dataFormat, importMode); } else if (i == 3) { session.setAttribute("status", opMessage + "Derivatives Creation ..."); boolean derReplace = getParameter(paramsMap, "derReplace") == null ? false : true; String reqSize = getParameter(paramsMap, "size"); String[] sizes = null; if (reqSize != null && reqSize.length() > 0) sizes = reqSize.split(","); handler = new DerivativeHandler(damsClient, collectionId, sizes, derReplace); } else if (i == 4) { session.setAttribute("status", opMessage + " release collection " + collectionId + " ..."); String releaseState = getParameter(paramsMap, "releaseState"); String releaseOption = getParameter(paramsMap, "releaseOption"); String collectionToMerge = getParameter(paramsMap, "collectionToMerge"); log.info("Collection release: category =>" + collectionId + ", releaseState => " + releaseState + ", releaseOption => " + releaseOption + ", collectionToMerge => " + collectionToMerge); handler = new CollectionReleaseHandler(damsClient, collectionId, releaseState, releaseOption); ((CollectionReleaseHandler) handler).setCollectionToMerge(collectionToMerge); } else if (i == 5) { session.setAttribute("status", opMessage + "Importing objects ..."); String[] dataPaths = getParameter(paramsMap, "dataPath").split(";"); String[] filesPaths = getParameter(paramsMap, "filesPath").split(";"); String importOption = getParameter(paramsMap, "importOption"); boolean replace = getParameter(paramsMap, "externalImportReplace") != null; List<File> dFiles = new ArrayList<File>(); for (int j = 0; j < dataPaths.length; j++) { String dataPath = dataPaths[j]; if (dataPath != null && (dataPath = dataPath.trim()).length() > 0) { File file = new File(Constants.DAMS_STAGING + "/" + dataPath); CollectionHandler.listFiles(dFiles, file); } } List<String> ingestFiles = new ArrayList<String>(); for (int j = 0; j < filesPaths.length; j++) { if ((filesPaths[j] = filesPaths[j].trim()).length() > 0) ingestFiles.add(new File(Constants.DAMS_STAGING + "/" + filesPaths[j]) .getAbsolutePath()); } String[] excelExts = { "xls", "xlsx" }; List<File> excelFiles = FileUtils.filterFiles(dFiles, excelExts); if (excelFiles.size() > 0) { // Remove the Excel source that need conversion from the file list dFiles.removeAll(excelFiles); // Pre-processing boolean preprocessing = importOption.equalsIgnoreCase("pre-processing"); Element rdfPreview = null; StringBuilder errorMessage = new StringBuilder(); StringBuilder duplicatRecords = new StringBuilder(); List<String> ids = new ArrayList<String>(); if (preprocessing) { Document doc = new DocumentFactory().createDocument(); rdfPreview = TabularRecord.createRdfRoot(doc); } handler = new MetadataImportHandler(damsClient, null); handler.setSubmissionId(submissionId); handler.setSession(session); handler.setUserId(userId); // Directory to hold the converted rdf/xml File tmpDir = new File(Constants.TMP_FILE_DIR + File.separatorChar + "converted"); if (!tmpDir.exists()) tmpDir.mkdir(); // Convert Excel source files to DAMS4 rdf/xml int filesCount = 0; for (File f : excelFiles) { filesCount++; RecordSource src = new ExcelSource(f); for (Record rec = null; (rec = src.nextRecord()) != null;) { String id = rec.recordID(); handler.logMessage("Pre-processing record with ID " + id + " ... "); if (ids.indexOf(id) < 0) { ids.add(id); } else { duplicatRecords.append(id + ", "); handler.logError("Found duplicated record with ID " + id + "."); } try { Document doc = rec.toRDFXML(); if (duplicatRecords.length() == 0 && errorMessage.length() == 0) { if (preprocessing) { // preview when there are no error reported rdfPreview.add(rec.toRDFXML().selectSingleNode("//dams:Object") .detach()); } else { File convertedFile = new File(tmpDir.getAbsolutePath(), id.replaceAll("[\\//:.*]+", "") + ".rdf.xml"); try { writeXml(convertedFile, doc.asXML()); } finally { convertedFile.deleteOnExit(); if (dFiles.indexOf(convertedFile) < 0) { dFiles.add(convertedFile); handler.logMessage("Added converted RDF/XML file " + convertedFile.getAbsolutePath()); } } } } } catch (Exception e) { log.warn("Excel Input Stream error", e); errorMessage.append("-" + e.getMessage() + "\n"); handler.logMessage(e.getMessage() + "\n"); } } handler.setProgressPercentage(filesCount * 100 / excelFiles.size()); } if (errorMessage.length() == 0 && duplicatRecords.length() == 0) { if (preprocessing) { File destFile = new File(Constants.TMP_FILE_DIR, "preview-" + submissionId + "-rdf.xml"); writeXml(destFile, rdfPreview.getDocument().asXML()); successful = true; message = "\nPre-processing passed. "; message += "\nThe converted RDF/XML is ready for <a href=\"" + logLink + "&file=" + destFile.getName() + "\">download</a>."; //handler.logMessage(message); handler.release(); handler = null; } else { handler.release(); // Initiate the ingest task for Excel AND/OR RDF/XML files handler = new RDFDAMS4ImportTsHandler(damsClient, dFiles.toArray(new File[dFiles.size()]), importOption); ((RDFDAMS4ImportTsHandler) handler) .setFilesPaths(ingestFiles.toArray(new String[ingestFiles.size()])); ((RDFDAMS4ImportTsHandler) handler).setReplace(replace); } } else { successful = false; message = "\nPre-processing issues found:"; if (duplicatRecords.length() > 0) message += "\nDuplicated records: " + duplicatRecords .substring(0, duplicatRecords.length() - 2).toString(); if (errorMessage.length() > 0) message += "\nOther Errors: \n" + errorMessage.toString(); //handler.logMessage(message); handler.release(); handler = null; } } else { // Ingest for RDF/XML files handler = new RDFDAMS4ImportTsHandler(damsClient, dFiles.toArray(new File[dFiles.size()]), importOption); ((RDFDAMS4ImportTsHandler) handler) .setFilesPaths(ingestFiles.toArray(new String[ingestFiles.size()])); ((RDFDAMS4ImportTsHandler) handler).setReplace(replace); } } else if (i == 6) { session.setAttribute("status", opMessage + "Importing from Standard Input Stream source ..."); log.info(opMessage + "Importing from Standard Input Stream source ..."); String unit = getParameter(paramsMap, "unit"); String source = getParameter(paramsMap, "source"); String bibNumber = getParameter(paramsMap, "bibInput"); String modsXml = getParameter(paramsMap, "modsInput"); String copyrightStatus = getParameter(paramsMap, "copyrightStatus"); String copyrightJurisdiction = getParameter(paramsMap, "countryCode"); String copyrightOwner = getParameter(paramsMap, "copyrightOwner"); String program = getParameter(paramsMap, "program"); String access = getParameter(paramsMap, "accessOverride"); String beginDate = getParameter(paramsMap, "licenseBeginDate"); String endDate = getParameter(paramsMap, "licenseEndDate"); String[] dataPaths = getParameter(paramsMap, "dataPath").split(";"); String[] filesPaths = getParameter(paramsMap, "filesPath").split(";"); String importOption = getParameter(paramsMap, "importOption"); List<String> ingestFiles = new ArrayList<String>(); for (int j = 0; j < filesPaths.length; j++) { if ((filesPaths[j] = filesPaths[j].trim()).length() > 0) ingestFiles.add(new File(Constants.DAMS_STAGING + "/" + filesPaths[j]) .getAbsolutePath()); } List<File> dataFiles = new ArrayList<File>(); for (int j = 0; j < dataPaths.length; j++) { String dataPath = dataPaths[j]; if (dataPath != null && (dataPath = dataPath.trim()).length() > 0) { File file = new File(Constants.DAMS_STAGING + "/" + dataPath); CollectionHandler.listFiles(dataFiles, file); } } // initiate the source metadata List<Object> sources = new ArrayList<Object>(); if (source != null && source.equalsIgnoreCase("bib")) { String[] bibs = bibNumber.split(","); for (int j = 0; j < bibs.length; j++) { if (bibs[j] != null && (bibs[j] = bibs[j].trim()).length() > 0) sources.add(bibs[j]); } } else { List<String> filters = new ArrayList<>(); if (getParameter(paramsMap, "excelImport") != null) { // Excel Input Stream source = "excel"; filters.add("xls"); filters.add("xlsx"); } else { // MARC/MODS source filters.add("xml"); } dataFiles = FileUtils.filterFiles(dataFiles, filters.toArray(new String[filters.size()])); sources.addAll(dataFiles); dataFiles.clear(); } // Handling pre-processing request Element rdfPreview = null; StringBuilder duplicatRecords = new StringBuilder(); List<String> ids = new ArrayList<String>(); boolean preprocessing = importOption.equalsIgnoreCase("pre-processing"); boolean ingestWithFiles = importOption.equalsIgnoreCase("metadataAndFiles"); if (preprocessing) { Document doc = new DocumentFactory().createDocument(); rdfPreview = TabularRecord.createRdfRoot(doc); } boolean preSuccessful = true; StringBuilder proMessage = new StringBuilder(); if (source != null && (source.equalsIgnoreCase("bib") || source.equalsIgnoreCase("mods") || source.equalsIgnoreCase("excel"))) { // Initiate the logging handler handler = new MetadataImportHandler(damsClient, null); handler.setSubmissionId(submissionId); handler.setSession(session); handler.setUserId(userId); Map<String, String> collections = new HashMap<String, String>(); if (StringUtils.isNotBlank(collectionId)) { String collType = damsClient.getCollectionType(collectionId); collections.put(collectionId, collType); } for (int j = 0; j < sources.size(); j++) { InputStream in = null; String sourceID = null; Object srcRecord = sources.get(j); sourceID = (srcRecord instanceof File ? ((File) srcRecord).getName() : srcRecord.toString()); if (preprocessing) handler.setStatus("Pre-processing record " + sourceID + " ... "); else handler.setStatus("Processing record " + sourceID + " ... "); RecordSource recordSource = null; InputStreamRecord record = null; try { if (source.equalsIgnoreCase("excel")) { clientTool = "Excel"; // Handling Excel Input Stream records recordSource = new ExcelSource((File) srcRecord); // Report for Excel column name validation List<String> invalidColumns = ((ExcelSource) recordSource) .getInvalidColumns(); if (invalidColumns != null && invalidColumns.size() > 0) { successful = false; preSuccessful = false; proMessage.append("Excel source " + sourceID + " - failed - " + CollectionHandler.damsDateFormat.format(new Date()) + ": \n"); if (invalidColumns != null && invalidColumns.size() > 0) { // Report invalid columns proMessage.append("* Found the following invalid column name" + (invalidColumns.size() > 1 ? "s" : "") + ": "); for (int k = 0; k < invalidColumns.size(); k++) { proMessage.append(invalidColumns.get(k)); if (k == invalidColumns.size() - 1) proMessage.append("\n"); else proMessage.append("; "); } } } } else { // Handling AT/Roger records try { if (source.equalsIgnoreCase("bib")) { clientTool = "MARC"; String url = Constants.DAMS_STORAGE_URL.substring(0, Constants.DAMS_STORAGE_URL.indexOf("/dams/")) + "/jollyroger/get?type=bib&mods=true&ns=true&value=" + sourceID; log.info("Getting MARC XML for Roger record " + sourceID + " from URL: " + url); HttpGet req = new HttpGet(url); Document doc = damsClient.getXMLResult(req); modsXml = doc.asXML(); in = new ByteArrayInputStream(modsXml.getBytes("UTF-8")); } else { // METS/MODS XML from staging area clientTool = "AT"; File srcFile = (File) sources.get(j); in = new FileInputStream(srcFile); } File xsl = new File(session.getServletContext() .getRealPath("files/mets2dams.xsl")); recordSource = new XsltSource(xsl, sourceID.replaceAll("\\..*", ""), in); } finally { CollectionHandler.close(in); in = null; } } } catch (Exception e) { e.printStackTrace(); successful = false; preSuccessful = false; String error = e.getMessage() != null ? e.getMessage() : e.getCause() != null ? e.getCause().getMessage() : e.getClass().getName(); handler.setStatus(error); log.error("Error metadata source " + sourceID + ": " + error); proMessage.append(sourceID + " - failed - " + CollectionHandler.damsDateFormat.format(new Date()) + " - " + error); } String id = ""; String info = ""; if (recordSource != null && preSuccessful) { for (Record rec = null; (rec = recordSource.nextRecord()) != null;) { String objTitle = ""; id = rec.recordID(); StringBuilder errorMessage = new StringBuilder(); try { record = new InputStreamRecord(rec, collections, unit, copyrightStatus, copyrightJurisdiction, copyrightOwner, program, access, beginDate, endDate); objTitle = getTitle(record.toRDFXML()); info = "Pre-processing record with ID " + id + " ... "; handler.setStatus(info); log.info(info); if (ids.indexOf(id) < 0) { ids.add(id); } else { duplicatRecords.append(rec + ", "); String error = "Duplicated record with ID " + id; handler.setStatus(error); log.error(info); errorMessage.append("\n* " + error); } // Add master file(s) for the bib/Roger record: a PDF or a TIFF, or a PDF + ZIP List<File> filesToIngest = null; if (source.equalsIgnoreCase("bib") && ingestWithFiles) { filesToIngest = getRogerFiles((String) srcRecord, ingestFiles); // Processing the master file(s) with error report. if (filesToIngest.size() == 0) { errorMessage.append("\n* Roger record " + srcRecord + " has no master file(s) for \"Ingest metadata and files\" option."); } else if (filesToIngest.size() > 2 || (filesToIngest.size() == 2 && !filesToIngest.get(1) .getName().endsWith(".zip"))) { errorMessage .append("\n* Unexpected file(s) for Roger record " + srcRecord + ": "); for (File file : filesToIngest) { errorMessage.append( (filesToIngest.indexOf(file) > 0 ? ", " : "") + file.getName()); } } else { // Handle the use property for the file(s) Map<String, String> fileUseMap = getFileUse(filesToIngest); record.addFiles(0, filesToIngest, fileUseMap); } } else if (source.equalsIgnoreCase("excel")) { // Report for invalid Excel control values validation List<Map<String, String>> invalidValues = ((ExcelSource) recordSource) .getInvalidValues(); if (invalidValues != null && invalidValues.size() > 0) { // process to retrieve control values errors for the record since it will parse the row for the next record StringBuilder cvErrors = new StringBuilder(); for (int k = 0; k < invalidValues.size(); k++) { Map<String, String> m = invalidValues.get(k); if (m.containsKey(TabularRecord.OBJECT_ID) && m.get(TabularRecord.OBJECT_ID) .equals(String.valueOf(id))) { cvErrors.append( "* Row index " + m.get("row") + " ["); // don't count for the row number and the record id m.remove("row"); m.remove(TabularRecord.OBJECT_ID); int l = 0; for (String key : m.keySet()) { if (l++ > 0) cvErrors.append(" | "); cvErrors.append(key + " => " + m.get(key)); } cvErrors.append("]\n"); } } if (cvErrors.length() > 0) { errorMessage.append("Invalid control value(s)" + " - \n" + cvErrors.toString()); } } } } catch (Exception e) { e.printStackTrace(); info = "Error: " + e.getMessage(); handler.setStatus(info); log.warn(info); errorMessage.append("\n* " + e.getMessage()); } objTitle = StringUtils.isEmpty(objTitle) ? "[Object]" : objTitle; if (errorMessage.length() == 0) { info = objTitle + " - " + id + " - " + " successful - " + CollectionHandler.damsDateFormat.format(new Date()); proMessage.append("\n\n" + info); log.info(info); if (preprocessing) { // Pre-processing with rdf preview rdfPreview.add(record.toRDFXML() .selectSingleNode("//dams:Object").detach()); } else { // Write the converted rdf/xml to file system File tmpDir = new File(Constants.TMP_FILE_DIR + File.separatorChar + "converted"); if (!tmpDir.exists()) tmpDir.mkdir(); File convertedFile = new File(tmpDir.getAbsolutePath(), id.replaceAll("[\\//:.*]+", "") + ".rdf.xml"); try { writeXml(convertedFile, record.toRDFXML().asXML()); } finally { convertedFile.deleteOnExit(); dataFiles.add(convertedFile); } } } else { preSuccessful = false; info = objTitle + " - " + id + " - " + " failed - " + CollectionHandler.damsDateFormat.format(new Date()) + " - " + errorMessage.toString(); proMessage.append("\n\n" + info); log.error(info); } handler.setProgressPercentage(j * 100 / sources.size()); } } } // Logging the result for pre-processing if (preprocessing || !preSuccessful) { message = "\nPre-processing " + (preSuccessful ? "successful" : "failed") + ": \n" + (proMessage.length() == 0 ? "" : "\n " + proMessage.toString()); handler.logMessage(message); } handler.release(); handler = null; if (preSuccessful) { // Write the converted RDF/xml for preview if (preprocessing) { File destFile = new File(Constants.TMP_FILE_DIR, "preview-" + submissionId + "-rdf.xml"); writeXml(destFile, rdfPreview.getDocument().asXML()); dataLink = "\nThe converted RDF/XML is ready for <a href=\"" + logLink + "&file=" + destFile.getName() + "\">download</a>.\n"; } else { // Ingest the converted RDF/XML files handler = new RDFDAMS4ImportTsHandler(damsClient, dataFiles.toArray(new File[dataFiles.size()]), importOption); ((RDFDAMS4ImportTsHandler) handler) .setFilesPaths(ingestFiles.toArray(new String[ingestFiles.size()])); ((RDFDAMS4ImportTsHandler) handler).setReplace(true); } } else { successful = false; } } else { successful = false; message += "\nUnknown source type: " + source; } } else if (i == 7) { session.setAttribute("status", opMessage + "SOLR Index ..."); boolean update = getParameter(paramsMap, "indexReplace") != null; if (getParameter(paramsMap, "solrRecordsDump") != null) { // Handle single records submission List<String> items = new ArrayList<String>(); String txtInput = getParameter(paramsMap, "textInput"); String fileInputValue = getParameter(paramsMap, "data"); if (txtInput != null && (txtInput = txtInput.trim()).length() > 0) { String[] subjects = txtInput.split(","); for (String subject : subjects) { subject = subject.trim(); if (subject.length() > 0) { items.add(subject); } } } // Handle records submitted in file with csv format, in lines or mixed together if (fileInputValue != null && (fileInputValue = fileInputValue.trim()).length() > 0) { // Handle record with line input String[] lines = fileInputValue.split("\n"); for (String line : lines) { // Handle CSV encoding records and records delimited by comma, whitespace etc. if (line != null && (line = line.trim().replace("\"", "")).length() > 0) { String[] tokens = line.split(","); for (String token : tokens) { String[] records = token.split(" "); for (String record : records) { record = record.trim(); if (record.length() > 0) { items.add(record); } } } } } } // Initiate SOLRIndexHandler to index the records handler = new SOLRIndexHandler(damsClient, null, update); handler.setItems(items); handler.setCollectionTitle("SOLR Records"); } else { // Handle solr update for collections if (collectionId.indexOf(",") > 0) { String collIDs = collectionId; String[] collArr = collectionId.split(","); List<String> items = new ArrayList<String>(); String collNames = ""; for (int j = 0; j < collArr.length; j++) { if (collArr[j] != null && (collArr[j] = collArr[j].trim()).length() > 0) { collectionId = collArr[j]; if (collectionId.equalsIgnoreCase("all")) { items.addAll(damsClient.listAllRecords()); collNames += "All Records (" + items.size() + "), "; } else { try { handler = new SOLRIndexHandler(damsClient, collectionId); items.addAll(handler.getItems()); collNames += handler.getCollectionTitle() + "(" + handler.getFilesCount() + "), "; if (j > 0 && j % 5 == 0) collNames += "\n"; } finally { if (handler != null) { handler.release(); handler = null; } } } } } handler = new SOLRIndexHandler(damsClient, null, update); handler.setItems(items); handler.setCollectionTitle(collNames.substring(0, collNames.lastIndexOf(","))); handler.setCollectionId(collIDs); } else { if (collectionId.equalsIgnoreCase("all")) { handler = new SOLRIndexHandler(damsClient, null, update); handler.setItems(damsClient.listAllRecords()); } else handler = new SOLRIndexHandler(damsClient, collectionId, update); } } } /*else if (i == 8){ //session.setAttribute("status", opMessage + "CDL Sending ..."); int operationType = 0; boolean resend = getParameter(paramsMap, "cdlResend") != null; if(resend){ operationType = 1; }else{ resend = getParameter(paramsMap, "cdlResendMets") != null; if(resend) operationType = 2; } //handler = new CdlIngestHandler(tsUtils, collectionId, userId, operationType); String feeder = getParameter(paramsMap, "feeder"); session.setAttribute("status", opMessage + "CDL " + feeder.toUpperCase() + " METS feeding ..."); boolean includeEmbargoed = (getParameter(paramsMap, "includeEmbargoed")!=null); if(feeder.equals("merritt")){ String account = getParameter(paramsMap, "account"); String password = getParameter(paramsMap, "password"); //String accessGroupId = getParameter(paramsMap, "accessGroup"); handler = new CdlIngestHandler(damsClient, collectionId, userId, operationType, feeder, account, password); }else handler = new CdlIngestHandler(damsClient, collectionId, userId, operationType); if(!includeEmbargoed) handler.excludeEmbargoedObjects(); }else if (i == 9){ session.setAttribute("status", opMessage + "Metadata Converting and populating ..."); String tsOperation = getParameter(paramsMap, "sipOption"); if(tsOperation == null || tsOperation.length() == 0) tsOperation = "tsNew"; int operationType = MetadataImportController.getOperationId(tsOperation); String srcFile = (String) session.getAttribute("source"); String srcFormat = (String) session.getAttribute("format"); String pathMap = (String) session.getAttribute("pathMap"); int sheetNo = 0; if(session.getAttribute("sheetNo") != null) sheetNo = ((Integer)session.getAttribute("sheetNo")).intValue(); String rdfFileToWrite = Constants.TMP_FILE_DIR + "tmpRdf_" + session.getId() + ".xml"; if("excel".equalsIgnoreCase(srcFormat)){ handler = new ExcelConverter(damsClient, collectionId, srcFile, sheetNo, pathMap, operationType); ExcelConverter converter = (ExcelConverter)handler; converter.setUseArk(true); converter.setRdfFileToWrite(rdfFileToWrite); }else throw new ServletException("Unsupported data format: " + srcFormat); }*/else if (i == 10) { session.setAttribute("status", opMessage + "Stage Ingesting ..."); String unit = getParameter(paramsMap, "unit"); String arkSetting = getParameter(paramsMap, "arkSetting").trim(); String filePath = getParameter(paramsMap, "filePath").trim(); String fileFilter = getParameter(paramsMap, "fileFilter").trim(); String preferedOrder = getParameter(paramsMap, "preferedOrder"); String fileSuffixes = getParameter(paramsMap, "fileSuffixes"); String fileUse = getParameter(paramsMap, "fileUse"); if (fileSuffixes != null && fileSuffixes.length() > 0) fileSuffixes = fileSuffixes.trim(); String coDelimiter = "p"; if (arkSetting.equals("1")) { if (preferedOrder == null || preferedOrder.equalsIgnoreCase("cofDelimiter")) { coDelimiter = getParameter(paramsMap, "cofDelimiter").trim(); } else if (preferedOrder.equals("suffix")) coDelimiter = getParameter(paramsMap, "coDelimiter").trim(); else coDelimiter = null; } else { if (arkSetting.equals("5")) { coDelimiter = getParameter(paramsMap, "coDelimiter").trim(); } } String[] fileOrderSuffixes = null; if (fileSuffixes != null && fileSuffixes.length() > 0) fileOrderSuffixes = fileSuffixes.split(","); String[] fileUses = null; if (fileUse != null && (fileUse = fileUse.trim()).length() > 0) { fileUses = fileUse.split(","); for (int j = 0; j < fileUses.length; j++) { if (fileUses[j] != null) fileUses[j] = fileUses[j].trim(); } } session.setAttribute("category", collectionId); session.setAttribute("unit", unit); session.setAttribute("arkSetting", arkSetting); session.setAttribute("filePath", filePath); session.setAttribute("fileFilter", fileFilter); session.setAttribute("preferedOrder", preferedOrder); session.setAttribute("fileSuffixes", fileSuffixes); session.setAttribute("fileUse", fileUse); String[] dirArr = filePath.split(";"); List<String> fileList = new ArrayList<String>(); String dir = null; for (int j = 0; j < dirArr.length; j++) { dir = dirArr[j]; if (dir != null && (dir = dir.trim()).length() > 0) { if ((dir.startsWith("/") || dir.startsWith("\\")) && (Constants.DAMS_STAGING.endsWith("/") || Constants.DAMS_STAGING.endsWith("\\"))) dir = dir.substring(1); fileList.add(Constants.DAMS_STAGING + dir); } } handler = new FileIngestionHandler(damsClient, fileList, Integer.parseInt(arkSetting), collectionId, fileFilter, coDelimiter); ((FileIngestionHandler) handler).setFileOrderSuffixes(fileOrderSuffixes); ((FileIngestionHandler) handler).setPreferedOrder(preferedOrder); ((FileIngestionHandler) handler).setUnit(unit); ((FileIngestionHandler) handler).setFileUses(fileUses); } else if (i == 11) { session.setAttribute("status", opMessage + "Serialize records as RDF/XML to filestore ..."); if (collectionId.indexOf(",") > 0) { String collIDs = collectionId; String[] collArr = collectionId.split(","); List<String> items = new ArrayList<String>(); String collNames = ""; for (int j = 0; j < collArr.length; j++) { if (collArr[j] != null && (collArr[j] = collArr[j].trim()).length() > 0) { collectionId = collArr[j]; if (collectionId.equalsIgnoreCase("all")) { items.addAll(damsClient.listAllRecords()); collNames += "All Records (" + items.size() + "), "; } else { try { handler = new SOLRIndexHandler(damsClient, collectionId); items.addAll(handler.getItems()); collNames += handler.getCollectionTitle() + "(" + handler.getFilesCount() + "), "; if (j > 0 && j % 5 == 0) collNames += "\n"; } finally { if (handler != null) { handler.release(); handler = null; } } } } } handler = new FilestoreSerializationHandler(damsClient, null); handler.setItems(items); handler.setCollectionTitle(collNames.substring(0, collNames.lastIndexOf(","))); handler.setCollectionId(collIDs); } else { if (collectionId.equalsIgnoreCase("all")) { handler = new FilestoreSerializationHandler(damsClient, null); handler.setItems(damsClient.listAllRecords()); } else handler = new FilestoreSerializationHandler(damsClient, collectionId); } } else if (i == 15) { session.setAttribute("status", opMessage + "Uploading files from dams-staging to " + damsClient.getFileStore() + " ..."); Map<String, String> filesMap = new TreeMap<String, String>(); for (Iterator<String> it = paramsMap.keySet().iterator(); it.hasNext();) { String key = it.next(); if (key.startsWith("f-")) { String file = paramsMap.get(key)[0]; String fileURI = paramsMap.get(key.replaceFirst("f-", "fid-"))[0]; if (fileURI != null && fileURI.startsWith(Constants.DAMS_ARK_URL_BASE)) filesMap.put(file, fileURI.trim()); else message += "Invalid fileURL for file " + file + " (" + fileURI + "). \n"; } } handler = new FileUploadHandler(damsClient, filesMap); handler.setItems(Arrays.asList(filesMap.keySet().toArray(new String[filesMap.size()]))); } else if (i == 18) { boolean components = getParameter(paramsMap, "exComponents") == null; String exFormat = getParameter(paramsMap, "exportFormat"); String xslSource = getParameter(paramsMap, "xsl"); if (xslSource == null || (xslSource = xslSource.trim()).length() == 0) { xslSource = "/pub/data1/import/apps/glossary/xsl/dams/convertToCSV.xsl"; if (!new File(xslSource).exists()) xslSource = Constants.CLUSTER_HOST_NAME + "glossary/xsl/dams/convertToCSV.xsl"; } session.setAttribute("status", opMessage + (exFormat.equalsIgnoreCase("csv") ? "CSV" : exFormat.equalsIgnoreCase("N-TRIPLE") ? "N-TRIPLE" : "RDF XML ") + " Metadata Export ..."); File outputFile = new File(Constants.TMP_FILE_DIR, "export-" + DAMSClient.stripID(collectionId) + "-" + System.currentTimeMillis() + "-rdf.xml"); String nsInput = getParameter(paramsMap, "nsInput"); List<String> nsInputs = new ArrayList<String>(); boolean componentsIncluded = true; if (nsInput != null && (nsInput = nsInput.trim()).length() > 0) { String[] nsInputArr = nsInput.split(","); for (int j = 0; j < nsInputArr.length; j++) { if (nsInputArr[j] != null && (nsInputArr[j] = nsInputArr[j].trim()).length() > 0) nsInputs.add(nsInputArr[j]); } } fileOut = new FileOutputStream(outputFile); handler = new MetadataExportHandler(damsClient, collectionId, nsInputs, componentsIncluded, exFormat, fileOut); ((MetadataExportHandler) handler).setFileUri(logLink + "&file=" + outputFile.getName()); ((MetadataExportHandler) handler).setComponents(components); } else if (i == 19) { session.setAttribute("status", opMessage + "Jhove report ..."); boolean bytestreamFilesOnly = getParameter(paramsMap, "bsJhoveReport") != null; boolean update = getParameter(paramsMap, "bsJhoveUpdate") != null; handler = new JhoveReportHandler(damsClient, collectionId, bytestreamFilesOnly); if (update) ((JhoveReportHandler) handler) .setJhoveUpdate(getParameter(paramsMap, "jhoveUpdate")); } else throw new ServletException("Unhandle operation index: " + i); if (handler != null) { try { damsClient.setClientInfo(clientTool + (StringUtils.isNotBlank(clientVersion) ? " " + clientVersion : "")); handler.setSubmissionId(submissionId); handler.setDamsClient(damsClient); handler.setSession(session); handler.setUserId(userId); if (handler.getCollectionId() == null && (collectionId != null && collectionId.length() > 0)) handler.setCollectionId(collectionId); successful = handler.execute(); } catch (InterruptedException e) { successful = false; exeInfo += e.getMessage(); e.printStackTrace(); } catch (Exception e) { successful = false; exeInfo += "\n" + e.getMessage(); e.printStackTrace(); } finally { String collectionName = handler.getCollectionId(); if (collectionName != null && collectionName.length() > 0 && logLink.indexOf("&category=") < 0) logLink += "&category=" + collectionName.replace(" ", ""); handler.setExeResult(successful); exeInfo += handler.getExeInfo(); handler.release(); if (fileOut != null) { CollectionHandler.close(fileOut); fileOut = null; } } } } else continue; message += exeInfo; if (!successful) { String errors = "Execution failed:\n" + message + "\n"; returnMessage += errors; break; } else { returnMessage += "\n" + message; } } } catch (Exception e) { e.printStackTrace(); returnMessage += e.getMessage(); } finally { if (damsClient != null) damsClient.close(); if (fileOut != null) { CollectionHandler.close(fileOut); fileOut = null; } } } else returnMessage = message; String logMessage = "For details, please download " + "<a href=\"" + logLink + "\">log</a>" + "."; if (returnMessage.length() > 1000) { returnMessage = returnMessage.substring(0, 1000); int idx = returnMessage.lastIndexOf("\n"); if (idx > 0) returnMessage = returnMessage.substring(0, idx); else { idx = returnMessage.lastIndexOf("</a>"); if (idx < returnMessage.lastIndexOf("<a ")) returnMessage = returnMessage.substring(0, idx); } returnMessage = "\n" + returnMessage + "\n ... "; } returnMessage += "\n" + dataLink + "\n" + logMessage; RequestOrganizer.addResultMessage(session, returnMessage.replace("\n", "<br />") + "<br />"); return returnMessage; }