List of usage examples for javax.servlet.http HttpServletRequest getRemoteUser
public String getRemoteUser();
null
if the user has not been authenticated. From source file:org.eclipse.orion.server.git.servlets.GitCloneHandlerV1.java
private boolean handlePost(HttpServletRequest request, HttpServletResponse response, String pathString) throws IOException, JSONException, ServletException, URISyntaxException, CoreException, NoHeadException, NoMessageException, ConcurrentRefUpdateException, WrongRepositoryStateException { // make sure required fields are set JSONObject toAdd = OrionServlet.readJSONRequest(request); if (toAdd.optBoolean(GitConstants.KEY_PULL, false)) { GitUtils.createGitCredentialsProvider(toAdd); GitCredentialsProvider cp = GitUtils.createGitCredentialsProvider(toAdd); boolean force = toAdd.optBoolean(GitConstants.KEY_FORCE, false); return pull(request, response, cp, pathString, force); }// w ww.j a v a 2 s . c o m Clone clone = new Clone(); String url = toAdd.optString(GitConstants.KEY_URL, null); // method handles repository clone or just repository init // decision is based on existence of GitUrl argument boolean initOnly; if (url == null || url.isEmpty()) initOnly = true; else { initOnly = false; if (!validateCloneUrl(url, request, response)) return true; clone.setUrl(new URIish(url)); } String cloneName = toAdd.optString(ProtocolConstants.KEY_NAME, null); if (cloneName == null) cloneName = request.getHeader(ProtocolConstants.HEADER_SLUG); // expected path /workspace/{workspaceId} String workspacePath = ServletResourceHandler.toOrionLocation(request, toAdd.optString(ProtocolConstants.KEY_LOCATION, null)); // expected path /file/{workspaceId}/{projectName}[/{path}] String filePathString = ServletResourceHandler.toOrionLocation(request, toAdd.optString(ProtocolConstants.KEY_PATH, null)); IPath filePath = filePathString == null ? null : new Path(filePathString); if (filePath != null && filePath.segmentCount() < 3) filePath = null; if (filePath == null && workspacePath == null) { String msg = NLS.bind("Either {0} or {1} should be provided: {2}", new Object[] { ProtocolConstants.KEY_PATH, ProtocolConstants.KEY_LOCATION, toAdd }); return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); } // only during init operation filePath or cloneName must be provided // during clone operation, name can be obtained from URL if (initOnly && filePath == null && cloneName == null) { String msg = NLS.bind("Either {0} or {1} should be provided: {2}", new Object[] { ProtocolConstants.KEY_PATH, GitConstants.KEY_NAME, toAdd }); return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); } if (!validateCloneName(cloneName, request, response)) return true; // prepare the WebClone object, create a new project if necessary ProjectInfo project = null; boolean webProjectExists = false; if (filePath != null) { //path format is /file/{workspaceId}/{projectName}/[filePath] clone.setId(filePath.toString()); project = GitUtils.projectFromPath(filePath); //workspace path format needs to be used if project does not exist if (project == null) { String msg = NLS.bind("Specified project does not exist: {0}", filePath.segment(2)); return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); } webProjectExists = true; clone.setContentLocation( project.getProjectStore().getFileStore(filePath.removeFirstSegments(3)).toURI()); if (cloneName == null) cloneName = filePath.segmentCount() > 2 ? filePath.lastSegment() : project.getFullName(); } else if (workspacePath != null) { IPath path = new Path(workspacePath); // TODO: move this to CloneJob // if so, modify init part to create a new project if necessary final IMetaStore metaStore = OrionConfiguration.getMetaStore(); WorkspaceInfo workspace = metaStore.readWorkspace(path.segment(1)); if (cloneName == null) cloneName = new URIish(url).getHumanishName(); cloneName = getUniqueProjectName(workspace, cloneName); webProjectExists = false; project = new ProjectInfo(); project.setFullName(cloneName); project.setWorkspaceId(workspace.getUniqueId()); try { //creating project in the backing store will assign a project id metaStore.createProject(project); } catch (CoreException e) { return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error persisting project state", e)); } try { WorkspaceResourceHandler.computeProjectLocation(request, project, null, false); metaStore.updateProject(project); } catch (CoreException e) { //delete the project so we don't end up with a project in a bad location try { metaStore.deleteProject(workspace.getUniqueId(), project.getFullName()); } catch (CoreException e1) { //swallow secondary error LogHelper.log(e1); } //we are unable to write in the platform location! String msg = NLS.bind("Failed to create project: {0}", project.getFullName()); return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e)); } URI baseLocation = getURI(request); baseLocation = new URI(baseLocation.getScheme(), baseLocation.getUserInfo(), baseLocation.getHost(), baseLocation.getPort(), workspacePath, baseLocation.getQuery(), baseLocation.getFragment()); clone.setId(GitUtils.pathFromProject(workspace, project).toString()); clone.setContentLocation(project.getProjectStore().toURI()); } clone.setName(cloneName); clone.setBaseLocation(getURI(request)); JSONObject cloneObject = clone.toJSON(); String cloneLocation = cloneObject.getString(ProtocolConstants.KEY_LOCATION); String gitUserName = toAdd.optString(GitConstants.KEY_NAME, null); String gitUserMail = toAdd.optString(GitConstants.KEY_MAIL, null); Boolean initProject = toAdd.optBoolean(GitConstants.KEY_INIT_PROJECT, false); if (initOnly) { // git init InitJob job = new InitJob(clone, TaskJobHandler.getUserId(request), request.getRemoteUser(), cloneLocation, gitUserName, gitUserMail); return TaskJobHandler.handleTaskJob(request, response, job, statusHandler); } // git clone // prepare creds GitCredentialsProvider cp = GitUtils.createGitCredentialsProvider(toAdd); cp.setUri(new URIish(clone.getUrl())); // if all went well, clone CloneJob job = new CloneJob(clone, TaskJobHandler.getUserId(request), cp, request.getRemoteUser(), cloneLocation, webProjectExists ? null : project /* used for cleaning up, so null when not needed */, gitUserName, gitUserMail, initProject); return TaskJobHandler.handleTaskJob(request, response, job, statusHandler); }
From source file:com.openkm.servlet.admin.BenchmarkServlet.java
/** * Generate documents into repository (OpenKM API) *//*from w w w . j a va2s .com*/ private void okmApiHighGenerate(HttpServletRequest request, HttpServletResponse response, String base) throws IOException { log.debug("okmApiHighGenerate({}, {}, {})", new Object[] { request, response }); int maxDocuments = WebUtils.getInt(request, "param1"); int maxFolder = WebUtils.getInt(request, "param2"); int maxDepth = WebUtils.getInt(request, "param3"); int maxIterations = WebUtils.getInt(request, "param4"); PrintWriter out = response.getWriter(); PrintWriter results = new PrintWriter(Config.HOME_DIR + File.separator + base + ".csv"); long tBegin = 0, tEnd = 0, pBegin = 0, pEnd = 0; Benchmark bm = null; response.setContentType(MimeTypeConfig.MIME_HTML); header(out, "OpenKM generate documents (API HIGH)", breadcrumb); out.flush(); try { bm = new Benchmark(maxDocuments, maxFolder, maxDepth); out.println("<b>- Documents:</b> " + bm.getMaxDocuments() + "<br/>"); out.println("<b>- Folders:</b> " + bm.getMaxFolders() + "<br/>"); out.println("<b>- Depth:</b> " + bm.getMaxDepth() + "<br/>"); out.println("<b>- Calibration:</b> " + bm.runCalibration() + " ms<br/>"); out.println("<b>- Calculated foldes:</b> " + bm.calculateFolders() + "<br/>"); out.println("<b>- Calculated documents:</b> " + bm.calculateDocuments() + "<br/><br/>"); results.print("\"Date\","); results.print("\"Time\","); results.print("\"Seconds\","); results.print("\"Folders\","); results.print("\"Documents\","); results.print("\"Size\"\n"); results.flush(); tBegin = System.currentTimeMillis(); for (int i = 0; i < maxIterations; i++) { out.println("<h2>Iteration " + i + "</h2>"); out.println("<table class=\"results\" width=\"80%\">"); out.println( "<tr><th>Date</th><th>Partial time</th><th>Partial miliseconds</th><th>Total folders</th><th>Total documents</th><th>Total size</th></tr>"); out.flush(); Folder fld = OKMRepository.getInstance().getRootFolder(null); fld.setPath(fld.getPath() + "/" + base); if (!OKMRepository.getInstance().hasNode(null, fld.getPath())) { fld = OKMFolder.getInstance().create(null, fld); } PrintWriter pResults = new PrintWriter(Config.HOME_DIR + File.separator + base + "_" + i + ".csv"); pResults.print("\"Date\","); pResults.print("\"Time\","); pResults.print("\"Seconds\","); pResults.print("\"Folders\","); pResults.print("\"Documents\","); pResults.print("\"Size\"\n"); pResults.flush(); pBegin = System.currentTimeMillis(); bm.okmApiHighPopulate(null, fld, out, pResults); pEnd = System.currentTimeMillis(); pResults.close(); out.println("</table>"); results.print("\"" + FormatUtil.formatDate(Calendar.getInstance()) + "\","); results.print("\"" + FormatUtil.formatSeconds(pEnd - pBegin) + "\","); results.print("\"" + (pEnd - pBegin) + "\","); results.print("\"" + bm.getTotalFolders() + "\","); results.print("\"" + bm.getTotalDocuments() + "\","); results.print("\"" + FormatUtil.formatSize(bm.getTotalSize()) + "\"\n"); results.flush(); out.println("<table class=\"results\" width=\"80%\">"); out.println( "<tr><th>Date</th><th>Partial time</th><th>Total folders</th><th>Total documents</th><th>Total size</th></tr>"); out.println("<td>" + FormatUtil.formatDate(Calendar.getInstance()) + "</td>"); out.println("<td>" + FormatUtil.formatSeconds(pEnd - pBegin) + "</td>"); out.println("<td>" + bm.getTotalFolders() + "</td>"); out.println("<td>" + bm.getTotalDocuments() + "</td>"); out.println("<td>" + FormatUtil.formatSize(bm.getTotalSize()) + "</td>"); out.println("</tr>"); out.println("</table>"); out.flush(); } tEnd = System.currentTimeMillis(); } catch (FileNotFoundException e) { out.println("<div class=\"warn\">FileNotFoundException: " + e.getMessage() + "</div>"); out.flush(); } catch (PathNotFoundException e) { out.println("<div class=\"warn\">PathNotFoundException: " + e.getMessage() + "</div>"); out.flush(); } catch (ItemExistsException e) { out.println("<div class=\"warn\">ItemExistsException: " + e.getMessage() + "</div>"); out.flush(); } catch (AccessDeniedException e) { out.println("<div class=\"warn\">AccessDeniedException: " + e.getMessage() + "</div>"); out.flush(); } catch (RepositoryException e) { out.println("<div class=\"warn\">RepositoryException: " + e.getMessage() + "</div>"); out.flush(); } catch (DatabaseException e) { out.println("<div class=\"warn\">DatabaseException: " + e.getMessage() + "</div>"); out.flush(); } catch (UserQuotaExceededException e) { out.println("<div class=\"warn\">UserQuotaExceededException: " + e.getMessage() + "</div>"); out.flush(); } catch (InputMismatchException e) { out.println("<div class=\"warn\">InputMismatchException: " + e.getMessage() + "</div>"); out.flush(); } catch (UnsupportedMimeTypeException e) { out.println("<div class=\"warn\">UnsupportedMimeTypeException: " + e.getMessage() + "</div>"); out.flush(); } catch (FileSizeExceededException e) { out.println("<div class=\"warn\">FileSizeExceededException: " + e.getMessage() + "</div>"); out.flush(); } catch (VirusDetectedException e) { out.println("<div class=\"warn\">VirusDetectedException: " + e.getMessage() + "</div>"); out.flush(); } catch (ExtensionException e) { out.println("<div class=\"warn\">ExtensionException: " + e.getMessage() + "</div>"); out.flush(); } catch (AutomationException e) { out.println("<div class=\"warn\">AutomationException: " + e.getMessage() + "</div>"); out.flush(); } long elapse = tEnd - tBegin; if (bm != null) { out.println("<hr/>"); out.println("<b>Total size:</b> " + FormatUtil.formatSize(bm.getTotalSize()) + "<br/>"); out.println("<b>Total folders:</b> " + bm.getTotalFolders() + "<br/>"); out.println("<b>Total documents:</b> " + bm.getTotalDocuments() + "<br/>"); out.println("<b>Total time:</b> " + FormatUtil.formatSeconds(elapse) + "<br/>"); out.println("<b>Documents per second:</b> " + bm.getTotalDocuments() / (elapse / 1000) + "<br/>"); } footer(out); out.flush(); out.close(); results.close(); // Activity log UserActivity.log(request.getRemoteUser(), "ADMIN_BENCHMARK_OKM_API_HIGH", null, null, "Size: " + FormatUtil.formatSize(bm == null ? 0 : bm.getTotalSize()) + ", Folders: " + (bm == null ? 0 : bm.getTotalFolders()) + ", Documents: " + (bm == null ? 0 : bm.getTotalDocuments()) + ", Time: " + FormatUtil.formatSeconds(elapse)); log.debug("okmApiHighGenerate: void"); }
From source file:com.gtwm.pb.model.manageData.DataManagement.java
public void removeRecord(HttpServletRequest request, SessionDataInfo sessionData, DatabaseInfo databaseDefn, TableInfo table, int rowId, boolean cascade) throws SQLException, ObjectNotFoundException, CodingErrorException, CantDoThatException, DisallowedException, DataDependencyException { Map<TableInfo, String> recordDependencies = new LinkedHashMap<TableInfo, String>(); Set<TableInfo> tables = this.authManager.getCompanyForLoggedInUser(request).getTables(); Connection conn = null;/* w ww.ja v a 2s. c o m*/ try { conn = this.dataSource.getConnection(); conn.setAutoCommit(false); recordDependencies = getRecordsDependencies(conn, tables, sessionData, databaseDefn, table, rowId, recordDependencies); } finally { if (conn != null) { conn.close(); } } // check for cascading deletes: if (recordDependencies.size() > 0) { // check if user has permissions on all dependent tables: for (TableInfo dependentTable : recordDependencies.keySet()) { if (dependentTable.equals(table)) continue; if (this.authManager.getAuthenticator().loggedInUserAllowedTo(request, PrivilegeType.EDIT_TABLE_DATA, dependentTable)) continue; if (this.authManager.getAuthenticator().loggedInUserAllowedTo(request, PrivilegeType.MANAGE_TABLE, dependentTable)) continue; throw new CantDoThatException( // TODO: This should probably really // be a DisallowedException "Unable to delete record as you are not permitted to delete from the dependent table " + dependentTable); } // user has permissions on all dependent data. however, deletion // should only continue if the user has opted to cascade deletion if (!cascade) { String warning = ""; for (Map.Entry<TableInfo, String> dependency : recordDependencies.entrySet()) { warning += dependency.getKey().getSimpleName() + " - " + dependency.getValue() + "\n"; } throw new DataDependencyException(warning); } } // First get all the content of the row we're deleting, for logging // purposes Map<BaseField, BaseValue> deletedRow = this.getTableDataRow(null, table, rowId, false); String SQLCode = "DELETE FROM " + table.getInternalTableName() + " WHERE " + table.getPrimaryKey().getInternalFieldName() + "=?"; try { conn = this.dataSource.getConnection(); conn.setAutoCommit(false); PreparedStatement statement = conn.prepareStatement(SQLCode); TableDataInfo tableData = new TableData(table); if (tableData.isRecordLocked(conn, sessionData, rowId)) { throw new CantDoThatException( "Record " + rowId + " can't be removed from " + table + " because it is locked"); } statement.setInt(1, rowId); int numRowsDeleted = statement.executeUpdate(); statement.close(); // Make sure we don't delete more than one row if (numRowsDeleted > 1) { conn.rollback(); throw new ObjectNotFoundException( String.valueOf(numRowsDeleted) + " records deleted for row id " + String.valueOf(rowId)); } else { // Delete comments for the record Set<String> internalFieldNamesSet = new HashSet<String>(); for (BaseField field : table.getFields()) { if (!field.getHidden()) { internalFieldNamesSet.add("'" + field.getInternalFieldName() + "'"); } } String internalFieldNamesCsv = StringUtils.join(internalFieldNamesSet, ","); SQLCode = "DELETE FROM dbint_comments WHERE rowid=? AND internalfieldname IN (" + internalFieldNamesCsv + ")"; statement = conn.prepareStatement(SQLCode); statement.setInt(1, rowId); int numCommentsDeleted = statement.executeUpdate(); if (numCommentsDeleted > 0) { logger.info("" + numCommentsDeleted + " comments deleted when deleting " + table + " record " + rowId); } statement.close(); conn.commit(); } } finally { if (conn != null) { conn.close(); } } this.removeUploadedFiles(table, rowId); this.logLastDataChangeTime(request); logLastTableDataChangeTime(table); UsageLogger usageLogger = new UsageLogger(dataSource); AppUserInfo user = this.authManager.getUserByUserName(request, request.getRemoteUser()); usageLogger.logDataChange(user, table, null, AppAction.REMOVE_RECORD, rowId, "Deleted data: " + deletedRow); UsageLogger.startLoggingThread(usageLogger); }
From source file:de.innovationgate.wgpublisher.WGACore.java
/** * opens a content db based upon request.getRemoteUser and request.getUserPrincipal() * if request.getRemoteUser is 'null' and request.getUserPrincipal()!=null, WGDatabase.UNKNOWN_REMOTE_USER is given to the authmodule * @param db/*from w ww.java2 s. co m*/ * @param request * @return * @throws WGAPIException * @throws ClientAccessException */ private WGDatabase openContentDBRequestBased(WGDatabase db, HttpServletRequest request, String accessFilter) throws WGException { if (request == null) { return prepareDB(db, request); } String user = request.getRemoteUser(); Principal credentials = request.getUserPrincipal(); if (user == null) { user = credentials == null ? WGDatabase.ANONYMOUS_USER : WGDatabase.UNKNOWN_REMOTE_USER; } db.openSession(user, credentials, accessFilter, request); if (db.isSessionOpen()) { updateLoginInfo(db, request, DBLoginInfo.AuthType.REQUEST); } return prepareDB(db, request); }
From source file:com.gtwm.pb.servlets.AppController.java
/** * The main control function, called for every HTTP GET or POST request. * Basically performs three functions:// w ww .j a v a 2s . com * * 1) Sets any session variables posted from the user interface, if any * * 2) Performs any actions requested by the UI, if any * * 3) Parses and returns the template requested by the UI */ public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context context) { // Start timing request long handleRequestStartTime = System.currentTimeMillis(); // Cache multipart form data so don't have to reparse it all the time List<FileItem> multipartItems = ServletUtilMethods.getMultipartItems(request); logger.debug("Request: " + ServletUtilMethods.getRequestQuery(request)); ResponseReturnType returnType = setReturnType(request, response, multipartItems); response.setCharacterEncoding("ISO-8859-1"); HttpSession session = request.getSession(); SessionDataInfo sessionData = (SessionDataInfo) session.getAttribute("com.gtwm.pb.servlets.sessionData"); if (sessionData == null) { try { // Set up a session for a newly logged in user sessionData = new SessionData(this.databaseDefn, this.relationalDataSource, request); } catch (SQLException sqlex) { ServletUtilMethods.logException(sqlex, request, "SQL error creating session data object: " + sqlex); sessionData = new SessionData(); } catch (AgileBaseException pbex) { ServletUtilMethods.logException(pbex, request, "Error creating session data object: " + pbex); sessionData = new SessionData(); } // Set session cookie expiry date // String id = request.getSession().getId(); // long expireTimestamp = System.currentTimeMillis() + (12 * 60 * 60 // * 1000); // 12 hours ahead // String expireDate = new // SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z").format(new // Date(expireTimestamp)); // response.setHeader("Set-Cookie", // String.format("JSESSIONID=%s;Expires=%s;Path=/agileBase", id, // expireDate)); } String templateName = ServletUtilMethods.getParameter(request, "return", multipartItems); try { carryOutSessionActions(request, sessionData, this.databaseDefn, context, session, multipartItems); } catch (AgileBaseException pbex) { ServletUtilMethods.logException(pbex, request, "Error setting session data"); if (returnType.equals(ResponseReturnType.XML)) { return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, pbex, multipartItems); } } catch (NumberFormatException nfex) { ServletUtilMethods.logException(nfex, request, "Non-numeric value specified for numeric parameter"); if (returnType.equals(ResponseReturnType.XML)) { return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, nfex, multipartItems); } } catch (RuntimeException rtex) { ServletUtilMethods.logException(rtex, request, "Runtime error setting session data"); if (returnType.equals(ResponseReturnType.XML)) { return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, rtex, multipartItems); } } catch (SQLException sqlex) { ServletUtilMethods.logException(sqlex, request, "SQL error setting session data"); // override to return the error template templateName = "report_error"; return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, sqlex, multipartItems); } catch (Exception ex) { ServletUtilMethods.logException(ex, request, "General error setting session data"); if (returnType.equals(ResponseReturnType.XML)) { return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, ex, multipartItems); } else { // override to return the error template templateName = "report_error"; return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, ex, multipartItems); } } // Use StringBuffer to get a mutable string that can be altered by // carryOutAppActions StringBuffer appActionName = new StringBuffer(""); try { carryOutAppActions(request, sessionData, this.databaseDefn, multipartItems, appActionName); } catch (MissingParametersException mpex) { ServletUtilMethods.logException(mpex, request, "Required parameters missing for action " + appActionName); return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, mpex, multipartItems); } catch (DisallowedException dex) { ServletUtilMethods.logException(dex, request, "No privileges to perform action " + appActionName); return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, dex, multipartItems); } catch (SQLException sqlex) { ServletUtilMethods.logException(sqlex, request, "Error accessing relational database while performing action" + appActionName); return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, sqlex, multipartItems); } catch (ObjectNotFoundException onfex) { ServletUtilMethods.logException(onfex, request, "An object mis-referenced while performing action " + appActionName); return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, onfex, multipartItems); } catch (InputRecordException irex) { ServletUtilMethods.logException(irex, request, "Error saving data during " + appActionName); return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, irex, multipartItems); } catch (Exception ex) { ServletUtilMethods.logException(ex, request, "General error performing action " + appActionName); return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, ex, multipartItems); } // Set any new values for the session variables after carrying out main // app actions EnumSet<SessionAction> sessionActions = EnumSet.allOf(SessionAction.class); try { carryOutPostSessionActions(request, sessionData, this.databaseDefn, sessionActions); } catch (ObjectNotFoundException onfex) { ServletUtilMethods.logException(onfex, request, "Error setting session data"); if (returnType.equals(ResponseReturnType.XML)) { return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, onfex, multipartItems); } } catch (NumberFormatException nfex) { ServletUtilMethods.logException(nfex, request, "Non-numeric value specified for numeric parameter"); if (returnType.equals(ResponseReturnType.XML)) { return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, nfex, multipartItems); } } catch (MissingParametersException mpex) { ServletUtilMethods.logException(mpex, request, "Error setting session data"); if (returnType.equals(ResponseReturnType.XML)) { return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, mpex, multipartItems); } } catch (RuntimeException rtex) { ServletUtilMethods.logException(rtex, request, "Runtime error setting session data"); if (returnType.equals(ResponseReturnType.XML)) { return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, rtex, multipartItems); } } catch (Exception ex) { ServletUtilMethods.logException(ex, request, "General error setting session data"); if (returnType.equals(ResponseReturnType.XML)) { return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, ex, multipartItems); } } // Log long request times - doesn't include template rendering time: see // mergeTemplate for those float secondsToHandleRequest = (System.currentTimeMillis() - handleRequestStartTime) / ((float) 1000); if (secondsToHandleRequest > AppProperties.longProcessingTime) { String warnMessage = "Long server request processing time of " + String.valueOf(secondsToHandleRequest) + " seconds for URL " + ServletUtilMethods.getRequestQuery(request) + "\r\n"; warnMessage += "Logged in user: " + request.getRemoteUser(); logger.warn(warnMessage); } // Don't do HibernateUtil.closeSession() here, leave it open for the // view to display // in mergeTemplate() // Create ViewMethods object and return the requested template return getUserInterfaceTemplate(request, response, templateName, context, session, sessionData, null, multipartItems); }
From source file:com.gtwm.pb.model.manageData.DataManagement.java
public int importCSV(HttpServletRequest request, TableInfo table, boolean updateExistingRecords, BaseField recordIdentifierField, boolean generateRowIds, char separator, char quotechar, int numHeaderLines, boolean useRelationDisplayValues, boolean importSequenceValues, boolean requireExactRelationMatches, boolean trim, boolean merge, List<FileItem> multipartItems, String csvContent) throws SQLException, InputRecordException, IOException, CantDoThatException, ObjectNotFoundException, DisallowedException, CodingErrorException { if (!FileUpload.isMultipartContent(new ServletRequestContext(request))) { if (csvContent == null) { throw new CantDoThatException( "To import CSV content, a file must be uploaded (form posted as multi-part) or csv_content specified"); }/*from www . j a va2 s .co m*/ } int numImportedRecords = 0; // get field set to import into. LinkedHashSet to ensure order is // retained so the right values are imported into the right fields LinkedHashSet<BaseField> fields = new LinkedHashSet<BaseField>(table.getFields()); // if row IDs aren't included in the data to import, remove ID from the // field set BaseField primaryKey = table.getPrimaryKey(); if (recordIdentifierField == null) { recordIdentifierField = primaryKey; } if (generateRowIds || (updateExistingRecords && !recordIdentifierField.equals(primaryKey))) { fields.remove(primaryKey); } Map<RelationField, Map<String, String>> relationLookups = new HashMap<RelationField, Map<String, String>>(); // Remove fields which shouldn't be modified during the import // For serial fields, if we need to set serial values explicitly, this // will have to be dealt with later for (BaseField field : table.getFields()) { if (field instanceof SequenceField && (!field.equals(primaryKey)) && (!importSequenceValues)) { fields.remove(field); } else if (field.getHidden()) { if (field.getFieldName().equals(HiddenFields.VIEW_COUNT.getFieldName()) || field.getFieldName().equals(HiddenFields.COMMENTS_FEED.getFieldName())) { fields.remove(field); } else if (updateExistingRecords) { if (field.getFieldName().equals(HiddenFields.DATE_CREATED.getFieldName()) || field.getFieldName().equals(HiddenFields.CREATED_BY.getFieldName())) { fields.remove(field); } } } else if (!field.getFieldCategory().savesData()) { fields.remove(field); } // Also, if importing relations by display value, look up // display/internal value mappings if (useRelationDisplayValues && field instanceof RelationField) { Map<String, String> displayToInternalValue = ((RelationFieldDefn) field).getItems(true, false); relationLookups.put((RelationField) field, displayToInternalValue); } } // Prepare SQL String insertSQLCode = null; String updateSQLCode = null; String logCreationSQLCode = null; // If updating, we'll need a record ID value. Depending on what the // identifier field is, this could be one of a couple of different types String recordIdentifierString = null; Integer recordIdentifierInteger = null; int recordIdentifierFieldNum = 0; DatabaseFieldType identifierFieldDbType = null; if (updateExistingRecords) { identifierFieldDbType = recordIdentifierField.getDbType(); if (!identifierFieldDbType.equals(DatabaseFieldType.VARCHAR) && !identifierFieldDbType.equals(DatabaseFieldType.INTEGER) && !identifierFieldDbType.equals(DatabaseFieldType.SERIAL)) { throw new CantDoThatException("The record identifier field has to be text or a whole number, " + recordIdentifierField + " is a " + identifierFieldDbType); } updateSQLCode = "UPDATE " + table.getInternalTableName() + " SET "; int fieldNum = 0; for (BaseField field : fields) { fieldNum += 1; if (merge) { // Update database only if there's a non-null value from the // spreadsheet updateSQLCode += field.getInternalFieldName() + " = COALESCE(?," + field.getInternalFieldName() + "), "; } else { updateSQLCode += field.getInternalFieldName() + " = ?, "; } if (field.equals(recordIdentifierField)) { recordIdentifierFieldNum = fieldNum; } } if (recordIdentifierFieldNum == 0) { throw new CantDoThatException("Can't find the field specified as record identifier (" + recordIdentifierField + ") in the list of table fields " + fields + " in table " + table); } updateSQLCode = updateSQLCode.substring(0, updateSQLCode.length() - 2); updateSQLCode += " WHERE " + recordIdentifierField.getInternalFieldName() + "=?"; logCreationSQLCode = "UPDATE " + table.getInternalTableName() + " SET " + table.getField(HiddenFields.DATE_CREATED.getFieldName()).getInternalFieldName() + "=?, " + table.getField(HiddenFields.CREATED_BY.getFieldName()).getInternalFieldName() + "=? WHERE " + primaryKey.getInternalFieldName() + "=?"; } insertSQLCode = "INSERT INTO " + table.getInternalTableName() + "("; String placeholders = ""; for (BaseField field : fields) { insertSQLCode += field.getInternalFieldName() + ", "; placeholders += "?, "; } placeholders = placeholders.substring(0, placeholders.length() - 2); insertSQLCode = insertSQLCode.substring(0, insertSQLCode.length() - 2) + ") VALUES (" + placeholders + ")"; // Find content to import Reader inputStreamReader = null; if (csvContent != null) { inputStreamReader = new StringReader(csvContent); } else { for (FileItem item : multipartItems) { // if item is a file if (!item.isFormField()) { if (item.getName().toLowerCase().endsWith(".xls")) { throw new CantDoThatException( "You need to upload as a CSV to import, Excel files can't be imported directly"); } inputStreamReader = new InputStreamReader(item.getInputStream()); break; } } } if (inputStreamReader == null) { throw new CantDoThatException("No file uploaded"); } CSVReader csvReader = new CSVReader(inputStreamReader, separator, quotechar, numHeaderLines); // returns a list of String arrays List<String[]> csvLines = (List<String[]>) csvReader.readAll(); // do db inserts Connection conn = null; PreparedStatement statement = null; // backupInsertStatement is for when an update returns 0 rows affected, // i.e. there's no matching row. In this case, do an insert PreparedStatement backupInsertStatement = null; PreparedStatement logCreationStatement = null; // These two variables used in exception handling int importLine = 0; BaseField fieldImported = null; Timestamp importTime = new Timestamp(System.currentTimeMillis()); AppUserInfo loggedInUser = authManager.getUserByUserName(request, request.getRemoteUser()); String fullname = loggedInUser.getForename() + " " + loggedInUser.getSurname() + " (" + loggedInUser.getUserName() + ")"; try { conn = this.dataSource.getConnection(); conn.setAutoCommit(false); if (updateExistingRecords) { statement = conn.prepareStatement(updateSQLCode); backupInsertStatement = conn.prepareStatement(insertSQLCode); logCreationStatement = conn.prepareStatement(logCreationSQLCode); } else { statement = conn.prepareStatement(insertSQLCode); } CSVLINE: for (String[] csvLineArray : csvLines) { // convert to an object rather than a primitive array - // easier to work with List<String> lineValues = Arrays.asList(csvLineArray); importLine++; // skip blank lines if (lineValues.size() == 1) { if (lineValues.get(0).length() == 0) { continue CSVLINE; } } int fieldNum = 0; for (BaseField field : fields) { fieldImported = field; fieldNum++; if (field.getHidden()) { String fieldName = field.getFieldName(); if (fieldName.equals(HiddenFields.LOCKED.getFieldName())) { statement.setBoolean(fieldNum, false); if (updateExistingRecords) { backupInsertStatement.setBoolean(fieldNum, false); } } else if (fieldName.equals(HiddenFields.DATE_CREATED.getFieldName()) || fieldName.equals(HiddenFields.LAST_MODIFIED.getFieldName())) { statement.setTimestamp(fieldNum, importTime); if (updateExistingRecords) { backupInsertStatement.setTimestamp(fieldNum, importTime); } } else if (fieldName.equals(HiddenFields.CREATED_BY.getFieldName()) || fieldName.equals(HiddenFields.MODIFIED_BY.getFieldName())) { statement.setString(fieldNum, fullname); if (updateExistingRecords) { backupInsertStatement.setString(fieldNum, fullname); } } } else if (fieldNum > lineValues.size()) { // booleans have a not null constraint if (field.getDbType().equals(Types.BOOLEAN)) { statement.setBoolean(fieldNum, false); if (updateExistingRecords) { backupInsertStatement.setBoolean(fieldNum, false); } } else { statement.setNull(fieldNum, Types.NULL); if (updateExistingRecords) { backupInsertStatement.setNull(fieldNum, Types.NULL); } } } else { String lineValue = lineValues.get(fieldNum - 1); if (lineValue != null) { if (trim) { lineValue = lineValue.trim(); } if (lineValue.equals("")) { // booleans have a not null constraint if (field.getDbType().equals(Types.BOOLEAN)) { statement.setBoolean(fieldNum, false); if (updateExistingRecords) { backupInsertStatement.setBoolean(fieldNum, false); } } else { statement.setNull(fieldNum, Types.NULL); if (updateExistingRecords) { backupInsertStatement.setNull(fieldNum, Types.NULL); } } } else { if ((field instanceof FileField) && (generateRowIds)) { throw new CantDoThatException( "Cannot generate row ids when importing file names. See line " + importLine + ", field '" + field.getFieldName() + "' with value '" + lineValue + "'"); } switch (field.getDbType()) { case VARCHAR: statement.setString(fieldNum, lineValue); if (updateExistingRecords) { backupInsertStatement.setString(fieldNum, lineValue); if (field.equals(recordIdentifierField)) { recordIdentifierString = lineValue; } } break; case TIMESTAMP: // deal with month and year // resolution dates exported if (lineValue.matches("^[a-zA-Z]{3}\\s\\d{2,4}$")) { lineValue = "01 " + lineValue; } else if (lineValue.matches("^\\d{2,4}")) { lineValue = "01 Jan " + lineValue; } try { Calendar calValue = CalendarParser.parse(lineValue, CalendarParser.DD_MM_YY); statement.setTimestamp(fieldNum, new Timestamp(calValue.getTimeInMillis())); if (updateExistingRecords) { backupInsertStatement.setTimestamp(fieldNum, new Timestamp(calValue.getTimeInMillis())); } } catch (CalendarParserException cpex) { throw new InputRecordException("Error importing line " + importLine + ", field " + field + ": " + cpex.getMessage(), field, cpex); } break; case FLOAT: lineValue = lineValue.trim().replaceAll("[^\\d\\.\\+\\-eE]", ""); statement.setDouble(fieldNum, Double.valueOf(lineValue)); if (updateExistingRecords) { backupInsertStatement.setDouble(fieldNum, Double.valueOf(lineValue)); } break; case INTEGER: if ((field instanceof RelationField) && (useRelationDisplayValues)) { // find key value for display value RelationField relationField = (RelationField) field; Map<String, String> valueKeyMap = relationLookups.get(relationField); String internalValueString = valueKeyMap.get(lineValue); if (internalValueString == null) { if (!requireExactRelationMatches) { // A very basic fuzzy matching // algorithm String potentialDisplayValue = null; String lineValueLowerCase = lineValue.toLowerCase(); FUZZYMATCH: for (Map.Entry<String, String> entry : valueKeyMap .entrySet()) { potentialDisplayValue = entry.getKey(); if (potentialDisplayValue.toLowerCase() .contains(lineValueLowerCase)) { internalValueString = entry.getValue(); break FUZZYMATCH; } } } if (internalValueString == null) { throw new CantDoThatException("Error importing line " + importLine + ", field " + relationField + ": Can't find a related '" + relationField.getRelatedTable() + "' for " + relationField.getDisplayField() + " '" + lineValue + "'. "); } } int keyValue = Integer.valueOf(internalValueString); statement.setInt(fieldNum, keyValue); if (updateExistingRecords) { backupInsertStatement.setInt(fieldNum, keyValue); if (field.equals(recordIdentifierField)) { recordIdentifierInteger = keyValue; } } } else { lineValue = lineValue.trim().replaceAll("[^\\d\\.\\+\\-eE]", ""); int keyValue = Integer.valueOf(lineValue); statement.setInt(fieldNum, keyValue); if (updateExistingRecords) { backupInsertStatement.setInt(fieldNum, keyValue); if (field.equals(recordIdentifierField)) { recordIdentifierInteger = keyValue; } } } break; case SERIAL: lineValue = lineValue.trim().replaceAll("[^\\d\\.\\+\\-eE]", ""); int keyValue = Integer.valueOf(lineValue); statement.setInt(fieldNum, keyValue); if (updateExistingRecords) { backupInsertStatement.setInt(fieldNum, keyValue); if (field.equals(recordIdentifierField)) { recordIdentifierInteger = keyValue; } } break; case BOOLEAN: boolean filterValueIsTrue = Helpers.valueRepresentsBooleanTrue(lineValue); statement.setBoolean(fieldNum, filterValueIsTrue); if (updateExistingRecords) { backupInsertStatement.setBoolean(fieldNum, filterValueIsTrue); } break; } } } else { // booleans have a not null constraint if (field.getDbType().equals(Types.BOOLEAN)) { statement.setBoolean(fieldNum, false); if (updateExistingRecords) { backupInsertStatement.setBoolean(fieldNum, false); } } else { statement.setNull(fieldNum, Types.NULL); if (updateExistingRecords) { backupInsertStatement.setNull(fieldNum, Types.NULL); } } } } } if (updateExistingRecords) { // for potential error messages String recordIdentifierDescription = null; if (identifierFieldDbType.equals(DatabaseFieldType.INTEGER) || identifierFieldDbType.equals(DatabaseFieldType.SERIAL)) { if (recordIdentifierInteger == null) { throw new InputRecordException( "Can't find a record identifier value at line " + importLine, recordIdentifierField); } recordIdentifierDescription = recordIdentifierField.getFieldName() + " = " + recordIdentifierInteger; // Set the 'WHERE recordIdentifier = ?' clause statement.setInt(fields.size() + 1, recordIdentifierInteger); } else { if (recordIdentifierString == null) { throw new InputRecordException( "Can't find a record identifier value at line " + importLine, recordIdentifierField); } recordIdentifierDescription = recordIdentifierField.getFieldName() + " = '" + recordIdentifierString + "'"; // Set the 'WHERE recordIdentifier = ?' clause statement.setString(fields.size() + 1, recordIdentifierString); } int rowsAffected = statement.executeUpdate(); if (rowsAffected == 0) { // If can't find a match to update, insert a record // instead backupInsertStatement.executeUpdate(); // NB Postgres specific code to find Row ID of newly // inserted record, not cross-db compatible String newRowIdSQLCode = "SELECT currval('" + table.getInternalTableName() + "_" + primaryKey.getInternalFieldName() + "_seq')"; PreparedStatement newRowIdStatement = conn.prepareStatement(newRowIdSQLCode); ResultSet newRowIdResults = newRowIdStatement.executeQuery(); if (newRowIdResults.next()) { int newRowId = newRowIdResults.getInt(1); // Add creation metadata to the new row logCreationStatement.setTimestamp(1, importTime); logCreationStatement.setString(2, fullname); logCreationStatement.setInt(3, newRowId); int creationLogRowsAffected = logCreationStatement.executeUpdate(); if (creationLogRowsAffected == 0) { throw new SQLException( "Unable to update creation metadata of newly inserted record, using query " + logCreationStatement); } } else { newRowIdResults.close(); newRowIdStatement.close(); throw new SQLException("Row ID not found for the newly inserted record. '" + newRowIdStatement + "' didn't work"); } newRowIdResults.close(); newRowIdStatement.close(); } else if (rowsAffected > 1) { throw new InputRecordException("Error importing line " + importLine + ". The record identifier field " + recordIdentifierDescription + " should match only 1 record in the database but it actually matches " + rowsAffected, recordIdentifierField); } // reset to null for the next line recordIdentifierString = null; recordIdentifierInteger = null; } else { statement.executeUpdate(); } numImportedRecords += 1; } statement.close(); if (backupInsertStatement != null) { backupInsertStatement.close(); } if (logCreationStatement != null) { logCreationStatement.close(); } // reset the primary key ID sequence so new records can be added resetSequence((SequenceField) primaryKey, conn); // and any other sequence fields if (importSequenceValues) { for (BaseField field : table.getFields()) { if ((!field.equals(primaryKey)) && field instanceof SequenceField) { resetSequence((SequenceField) field, conn); } } } // ANALYZE the table after import if (numImportedRecords > 1000) { Statement analyzeStatement = conn.createStatement(); analyzeStatement.execute("ANALYZE " + table.getInternalTableName()); analyzeStatement.close(); } conn.commit(); } catch (SQLException sqlex) { String databaseErrorMessage = Helpers.replaceInternalNames(sqlex.getMessage(), table.getDefaultReport()); logger.warn("Import failed, statement is " + statement); logger.warn("Backup insert statement is " + backupInsertStatement); String errorMessage = "Error importing CSV line " + importLine; if (!fieldImported.getHidden()) { errorMessage += ", field '" + fieldImported + "'"; } errorMessage += ": " + databaseErrorMessage; throw new InputRecordException(errorMessage, fieldImported, sqlex); } catch (NumberFormatException nfex) { String causeMessage = nfex.getMessage(); causeMessage = causeMessage.replaceAll("For input string", "value"); String errorMessage = "Error parsing number when importing CSV line " + importLine; if (!fieldImported.getHidden()) { errorMessage += ", field '" + fieldImported + "'"; } errorMessage += ": " + causeMessage; throw new InputRecordException(errorMessage, fieldImported, nfex); } finally { if (conn != null) { conn.close(); } } this.logLastDataChangeTime(request); logLastTableDataChangeTime(table); UsageLogger usageLogger = new UsageLogger(this.dataSource); String logMessage = "" + numImportedRecords; if (updateExistingRecords) { logMessage += " records imported"; } else { logMessage += " new records imported"; } if (csvContent != null) { logMessage += " from file"; } usageLogger.logDataChange(loggedInUser, table, null, AppAction.CSV_IMPORT, -1, logMessage); UsageLogger.startLoggingThread(usageLogger); return numImportedRecords; }
From source file:edu.ucsd.library.dams.api.DAMSAPIServlet.java
/** * HTTP GET methods to retrieve objects and datastream metadata and files. * Calls to GET should not change state in any way. **//*from ww w . jav a 2 s. c o m*/ public void doGet(HttpServletRequest req, HttpServletResponse res) { long start = System.currentTimeMillis(); Map info = null; boolean outputRequired = true; // set to false to suppress status output FileStore fs = null; TripleStore ts = null; TripleStore es = null; try { // parse request URI String[] path = path(req); fs = FileStoreUtil.getFileStore(props, fsDefault); // GET /collections if (path.length == 2 && path[1].equals("collections")) { ts = triplestore(req); info = collectionListAll(ts); } // GET /collections/bb1234567x else if (path.length == 3 && path[1].equals("collections")) { ts = triplestore(req); info = collectionListObjects(path[2], ts); } // GET /collections/bb1234567x/count else if (path.length == 4 && path[1].equals("collections") && path[3].equals("count")) { ts = triplestore(req); info = collectionCount(path[2], ts); } // GET /collections/bb1234567x/embargo else if (path.length == 4 && path[1].equals("collections") && path[3].equals("embargo")) { ts = triplestore(req); info = collectionEmbargo(path[2], ts); } // GET /collections/bb1234567x/files else if (path.length == 4 && path[1].equals("collections") && path[3].equals("files")) { ts = triplestore(req); info = collectionListFiles(path[2], ts); } // GET /units else if (path.length == 2 && path[1].equals("units")) { ts = triplestore(req); info = unitListAll(ts); } // GET /units/bb1234567x else if (path.length == 3 && path[1].equals("units")) { ts = triplestore(req); info = unitListObjects(path[2], ts); } // GET /units/bb1234567x/count else if (path.length == 4 && path[1].equals("units") && path[3].equals("count")) { ts = triplestore(req); info = unitCount(path[2], ts); } // GET /units/bb1234567x/embargo else if (path.length == 4 && path[1].equals("units") && path[3].equals("embargo")) { ts = triplestore(req); info = unitEmbargo(path[2], ts); } // GET /units/bb1234567x/files else if (path.length == 4 && path[1].equals("units") && path[3].equals("files")) { ts = triplestore(req); info = unitListFiles(path[2], ts); } // GET /events/bb1234567x else if (path.length == 3 && path[1].equals("events")) { es = events(req); info = objectShow(path[2], es, null); if (info.get("obj") != null) { DAMSObject obj = (DAMSObject) info.get("obj"); output(obj, true, req.getParameterMap(), req.getPathInfo(), res); outputRequired = false; } } // GET /events else if (path.length == 2 && path[1].equals("events")) { es = events(req); info = eventsListAll(es); } // GET /records else if (path.length == 2 && path[1].equals("records")) { ts = triplestore(req); recordsList(ts, req.getParameterMap(), null, req.getPathInfo(), res); outputRequired = false; // streaming output } // GET /records/[type] else if (path.length == 3 && path[1].equals("records") && !path[2].equals("")) { ts = triplestore(req); recordsList(ts, req.getParameterMap(), path[2], req.getPathInfo(), res); outputRequired = false; // streaming output } // GET /sparql else if (path.length == 2 && path[1].equals("sparql")) { Map<String, String[]> params = req.getParameterMap(); ts = triplestore(params); String query = getParamString(params, "query", null); sparqlQuery(query, ts, params, req.getPathInfo(), res); outputRequired = false; } // GET /objects else if (path.length == 2 && path[1].equals("objects")) { ts = triplestore(req); info = objectsListAll(ts); } // GET /objects/export?id=bb1111111x&id=bb2222222y&id=bb3333333z else if (path.length == 3 && path[1].equals("objects") && path[2].equals("export")) { ts = triplestore(req); es = events(req); String[] ids = req.getParameterValues("id"); List<String> objids = new ArrayList<String>(); for (int i = 0; i < ids.length; i++) { if (ids[i] != null && !ids[i].trim().equals("")) { objids.add(ids[i].trim()); } } try { String xml = objectBatch(objids, ts, es); output(SC_OK, xml, "application/xml", res); outputRequired = false; } catch (Exception ex) { info = error("Error processing batch retrieval", ex); } } // GET /objects/bb1234567x else if (path.length == 3 && path[1].equals("objects")) { ts = triplestore(req); es = events(req); info = objectShow(path[2], ts, es); if (info.get("obj") != null) { DAMSObject obj = (DAMSObject) info.get("obj"); output(obj, false, req.getParameterMap(), req.getPathInfo(), res); outputRequired = false; } } // GET /objects/bb1234567x/export else if (path.length == 4 && path[1].equals("objects") && path[3].equals("export")) { ts = triplestore(req); es = events(req); info = objectShow(path[2], ts, es); if (info.get("obj") != null) { DAMSObject obj = (DAMSObject) info.get("obj"); output(obj, true, req.getParameterMap(), req.getPathInfo(), res); outputRequired = false; } } // GET /objects/bb1234567x/exists else if (path.length == 4 && path[1].equals("objects") && path[3].equals("exists")) { ts = triplestore(req); info = objectExists(path[2], ts); } // GET /objects/bb1234567x/files else if (path.length == 4 && path[1].equals("objects") && path[3].equals("files")) { ts = triplestore(req); info = objectListFiles(path[2], ts); } // GET /objects/bb1234567x/transform else if (path.length == 4 && path[1].equals("objects") && path[3].equals("transform")) { ts = triplestore(req); String xsl = getParamString(req, "xsl", null); boolean export = getParamBool(req, "recursive", false); objectTransform(path[2], null, null, export, ts, null, xsl, null, null, req.getParameterMap(), req.getPathInfo(), res); outputRequired = false; } // GET /objects/bb1234567x/validate else if (path.length == 4 && path[1].equals("objects") && path[3].equals("validate")) { ts = triplestore(req); es = events(req); info = objectValidate(path[2], fs, ts, es); } // GET /files/image-service else if (path.length == 3 && path[1].equals("files")) { ts = triplestore(req); info = useListFiles("\"" + path[2] + "\"", ts); } // GET /files/bb1234567x/1.tif else if (path.length == 4 && path[1].equals("files")) { fileShow(path[2], null, path[3], req, res); outputRequired = false; } // GET /files/bb1234567x/1/1.tif else if (path.length == 5 && path[1].equals("files") && isNumber(path[3])) { fileShow(path[2], path[3], path[4], req, res); outputRequired = false; } // GET /files/bb1234567x/1.tif/characterize else if (path.length == 5 && path[1].equals("files") && path[4].equals("characterize")) { info = fileCharacterize(path[2], null, path[3], false, fs, null, null, new HashMap<String, String[]>()); } // GET /files/bb1234567x/1/1.tif/characterize else if (path.length == 6 && path[1].equals("files") && isNumber(path[3]) && path[5].equals("characterize")) { info = fileCharacterize(path[2], path[3], path[4], false, fs, null, null, new HashMap<String, String[]>()); } // GET /files/bb1234567x/1.tif/exists else if (path.length == 5 && path[1].equals("files") && path[4].equals("exists")) { info = fileExists(path[2], null, path[3], fs); } // GET /files/bb1234567x/1/1.tif/exists else if (path.length == 6 && path[1].equals("files") && path[5].equals("exists") && isNumber(path[3])) { info = fileExists(path[2], path[3], path[4], fs); } // GET /files/bb1234567x/1.tif/fixity else if (path.length == 5 && path[1].equals("files") && path[4].equals("fixity")) { ts = triplestore(req); es = events(req); info = fileFixity(path[2], null, path[3], fs, ts, es); } // GET /files/bb1234567x/1/1.tif/fixity else if (path.length == 6 && path[1].equals("files") && path[5].equals("fixity") && isNumber(path[3])) { ts = triplestore(req); es = events(req); info = fileFixity(path[2], path[3], path[4], fs, ts, es); } // GET /files/bb1234567x/1.tif/text else if (path.length == 5 && path[1].equals("files") && path[4].equals("text")) { info = extractText(path[2], null, path[3], fs); } // GET /files/bb1234567x/1/1.tif/text else if (path.length == 6 && path[1].equals("files") && path[5].equals("text") && isNumber(path[3])) { info = extractText(path[2], path[3], path[4], fs); } // GET /client/info else if (path.length == 3 && path[1].equals("client") && path[2].equals("info")) { String ip = getParamString(req, "ip", req.getRemoteAddr()); String user = getParamString(req, "user", req.getRemoteUser()); info = clientInfo(ip, user); } // GET /system/config else if (path.length == 3 && path[1].equals("system") && path[2].equals("config")) { String err = config(getServletContext()); if (err == null) { info = status("Configuration reloaded"); } else { info = error(err, null); } } // GET /system/info else if (path.length == 3 && path[1].equals("system") && path[2].equals("info")) { info = systemInfo(req); } // GET /system/predicates else if (path.length == 3 && path[1].equals("system") && path[2].equals("predicates")) { ts = triplestore(req); info = predicateList(ts); } // GET /system/filestores else if (path.length == 3 && path[1].equals("system") && path[2].equals("filestores")) { List<String> filestores = list(props, "fs.", ".className"); info = new LinkedHashMap(); info.put("filestores", filestores); info.put("defaultFilestore", fsDefault); } // GET /system/times else if (path.length == 3 && path[1].equals("system") && path[2].equals("times")) { info = times(); } // GET /system/triplestores else if (path.length == 3 && path[1].equals("system") && path[2].equals("triplestores")) { List<String> triplestores = list(props, "ts.", ".className"); info = new LinkedHashMap(); info.put("triplestores", triplestores); info.put("defaultTriplestore", tsDefault); info.put("eventsTriplestore", tsEvents); } // GET /system/version else if (path.length == 3 && path[1].equals("system") && path[2].equals("version")) { info = new LinkedHashMap(); info.put("appVersion", appVersion); info.put("srcVersion", srcVersion); info.put("buildTimestamp", buildTimestamp); } else { info = error(SC_BAD_REQUEST, "Invalid request", null); } // output if (outputRequired) { // make sure a status code is set if (info.get("statusCode") == null) { info.put("statusCode", SC_OK); } output(info, req.getParameterMap(), req.getPathInfo(), res); } } catch (Exception ex2) { log.error("Error processing GET request", ex2); } finally { // cleanup cleanup(fs, ts, es); } getTimes.add(System.currentTimeMillis() - start); }
From source file:com.gtwm.pb.model.manageData.DataManagement.java
/** * Used by both the public saveRecord and globalEdit methods *///from ww w. jav a 2 s . c o m private void saveRecord(HttpServletRequest request, TableInfo table, LinkedHashMap<BaseField, BaseValue> dataToSave, boolean newRecord, Set<Integer> rowIds, SessionDataInfo sessionData, List<FileItem> multipartItems) throws InputRecordException, ObjectNotFoundException, SQLException, CantDoThatException, CodingErrorException, DisallowedException, MissingParametersException { if ((dataToSave.size() == 0) && (!newRecord)) { // Note: this does actually happen quite a lot, from two particular // users, therefore I've commented out the log warning. // Haven't tracked down the cause but it doesn't seem to be creating // a problem. // logger.warn("Call to saveRecord with no data to save. User = " // + request.getRemoteUser() + ", table = " + table + ", rowIds = " // + rowIds); return; } this.setHiddenFieldValues(request, table, dataToSave, newRecord); boolean globalEdit = false; int rowId = -1; if (rowIds.size() > 1) { globalEdit = true; } else if (rowIds.size() == 1) { rowId = (new LinkedList<Integer>(rowIds)).getFirst(); } else { throw new ObjectNotFoundException("Row ID list " + rowIds + " is invalid"); } StringBuilder SQLCodeBuilder = new StringBuilder(); // Generate CSV of fields and placeholders to use in update/insert SQL // string StringBuilder fieldsCsvBuilder = new StringBuilder(); StringBuilder fieldsAndPlaceholdersCsvBuilder = new StringBuilder(); StringBuilder valuePlaceholdersCsvBuilder = new StringBuilder(); for (BaseField field : dataToSave.keySet()) { fieldsCsvBuilder.append(field.getInternalFieldName()); fieldsCsvBuilder.append(", "); valuePlaceholdersCsvBuilder.append("?, "); fieldsAndPlaceholdersCsvBuilder.append(field.getInternalFieldName()); fieldsAndPlaceholdersCsvBuilder.append("=?, "); } // Used if doing an INSERT String fieldsCsv = fieldsCsvBuilder.toString(); String valuePlaceholdersCsv = valuePlaceholdersCsvBuilder.toString(); // Used if doing an UPDATE String fieldsAndPlaceholdersCsv = fieldsAndPlaceholdersCsvBuilder.toString(); if (!fieldsCsv.equals("")) { fieldsCsv = fieldsCsv.substring(0, fieldsCsv.length() - 2); valuePlaceholdersCsv = valuePlaceholdersCsv.substring(0, valuePlaceholdersCsv.length() - 2); fieldsAndPlaceholdersCsv = fieldsAndPlaceholdersCsv.substring(0, fieldsAndPlaceholdersCsv.length() - 2); } if (newRecord) { SQLCodeBuilder.append("INSERT INTO " + table.getInternalTableName()); if (fieldsCsv.equals("")) { SQLCodeBuilder.append(" VALUES(default)"); } else { SQLCodeBuilder.append("(" + fieldsCsv + ") VALUES (" + valuePlaceholdersCsv + ")"); } } else { SQLCodeBuilder.append("UPDATE " + table.getInternalTableName() + " SET " + fieldsAndPlaceholdersCsv); if (globalEdit) { // add filter for various row ids SQLCodeBuilder.append(" WHERE " + table.getPrimaryKey().getInternalFieldName() + " in (?"); for (int i = 1; i < rowIds.size(); i++) { SQLCodeBuilder.append(",?"); } SQLCodeBuilder.append(")"); } else { // add filter for single row id SQLCodeBuilder.append(" WHERE " + table.getPrimaryKey().getInternalFieldName() + "=?"); } } Connection conn = null; int fieldNumber = 0; // Will be set if we're inserting a record int newRowId = -1; TableDataInfo tableData = new TableData(table); try { conn = this.dataSource.getConnection(); conn.setAutoCommit(false); PreparedStatement statement = conn.prepareStatement(SQLCodeBuilder.toString()); for (BaseField field : dataToSave.keySet()) { // If an exception is raised, currentField will be the field // which caused it // currentField = field; fieldNumber++; BaseValue fieldValue = dataToSave.get(field); if (field instanceof FileField) { if (fieldValue.isNull() || fieldValue.toString().equals("")) { throw new InputRecordException("No file specified for the upload", field); } } if (fieldValue.isNull()) { statement.setNull(fieldNumber, Types.NULL); } else { if (fieldValue instanceof TextValue) { String textValue = ((TextValue) fieldValue).toXmlString(); statement.setString(fieldNumber, textValue); } else if (fieldValue instanceof IntegerValue) { // if no related value, set relation field to null if (field instanceof RelationField && (((IntegerValue) fieldValue).getValueInteger() == -1) || (fieldValue.isNull())) { statement.setNull(fieldNumber, Types.NULL); } else { statement.setInt(fieldNumber, ((IntegerValue) fieldValue).getValueInteger()); } } else if (fieldValue instanceof DurationValue) { statement.setString(fieldNumber, ((DurationValue) fieldValue).getSqlFormatInterval()); } else if (fieldValue instanceof DecimalValue) { statement.setDouble(fieldNumber, ((DecimalValue) fieldValue).getValueFloat()); } else if (fieldValue instanceof DateValue) { if (((DateValue) fieldValue).getValueDate() != null) { java.util.Date javaDateValue = ((DateValue) fieldValue).getValueDate().getTime(); java.sql.Timestamp sqlTimestampValue = new java.sql.Timestamp(javaDateValue.getTime()); statement.setTimestamp(fieldNumber, sqlTimestampValue); } else { statement.setTimestamp(fieldNumber, null); } } else if (fieldValue instanceof CheckboxValue) { statement.setBoolean(fieldNumber, ((CheckboxValue) fieldValue).getValueBoolean()); } else if (fieldValue instanceof FileValue) { statement.setString(fieldNumber, ((FileValue) fieldValue).toString()); } else { throw new CodingErrorException("Field value " + fieldValue + " is of unknown type " + fieldValue.getClass().getSimpleName()); } } } // We've finished setting individual fields, if an SQL error occurs // after here we won't know which // field caused it without looking for it by other means // currentField = null; if (!newRecord) { if (globalEdit) { // Fill in the 'WHERE [row id field] in (?,..,?)' for use in // the UPDATE statement for (Integer aRowId : rowIds) { if (tableData.isRecordLocked(conn, sessionData, aRowId)) { throw new CantDoThatException( "Record " + aRowId + " from table " + table + " is locked to prevent editing"); } statement.setInt(++fieldNumber, aRowId); } } else { // Fill in the 'WHERE [row id field]=?' for use in the // UPDATE statement if (tableData.isRecordLocked(conn, sessionData, rowId)) { throw new CantDoThatException( "Record " + rowId + " from table " + table + " is locked to prevent editing"); } statement.setInt(fieldNumber + 1, rowId); } } int numRowsAffected = statement.executeUpdate(); statement.close(); if ((numRowsAffected != 1) && (!globalEdit)) { conn.rollback(); if (numRowsAffected > 0) { throw new ObjectNotFoundException(String.valueOf(numRowsAffected) + " records would be altered during a single record save"); } else { throw new ObjectNotFoundException( "The current record can't be found to edit - perhaps someone else has deleted it"); } } if (newRecord) { // Find the newly inserted Row ID // postgres-specific code, not database independent String SQLCode = "SELECT currval('" + table.getInternalTableName() + "_" + table.getPrimaryKey().getInternalFieldName() + "_seq')"; statement = conn.prepareStatement(SQLCode); ResultSet results = statement.executeQuery(); if (results.next()) { newRowId = results.getInt(1); } else { results.close(); statement.close(); throw new SQLException( "Row ID not found for the newly inserted record. '" + SQLCodeBuilder + "' didn't work"); } results.close(); statement.close(); } conn.commit(); } catch (SQLException sqlex) { // Find out which field caused the error by looking for internal // field names in the error message String errorMessage = sqlex.getMessage(); for (BaseField possibleCauseField : dataToSave.keySet()) { if (errorMessage.contains(possibleCauseField.getInternalFieldName())) { if (errorMessage.contains("check constraint")) { errorMessage = "The value " + dataToSave.get(possibleCauseField) + " falls outside the allowed range"; } else if (errorMessage.contains("not-null constraint")) { errorMessage = "No value entered"; } else if (errorMessage.contains("unique constraint")) { errorMessage = "Value " + dataToSave.get(possibleCauseField) + " is already in the database and cannot be entered again"; } else if (errorMessage.contains("foreign key constraint") && possibleCauseField instanceof RelationField) { errorMessage = "Please select a valid " + ((RelationField) possibleCauseField).getRelatedTable() + " record first"; } else { errorMessage = "Value " + dataToSave.get(possibleCauseField) + " not allowed (" + Helpers.replaceInternalNames(errorMessage, table.getDefaultReport()) + ")"; } throw new InputRecordException(errorMessage, possibleCauseField, sqlex); } } // Not able to find field errorMessage = Helpers.replaceInternalNames(errorMessage, table.getDefaultReport()); throw new InputRecordException(errorMessage, null, sqlex); } finally { if (conn != null) { conn.close(); } } // If any fields were files to upload, do the actual uploads. // Do this after the commit in case the uploads take a long time and // time out the SQL connection. for (BaseField field : dataToSave.keySet()) { if (field instanceof FileField) { try { if (newRecord) { this.uploadFile(request, (FileField) field, (FileValue) dataToSave.get(field), newRowId, multipartItems); } else { this.uploadFile(request, (FileField) field, (FileValue) dataToSave.get(field), rowId, multipartItems); } } catch (CantDoThatException cdtex) { throw new InputRecordException("Error uploading file: " + cdtex.getMessage(), field, cdtex); } catch (FileUploadException fuex) { throw new InputRecordException("Error uploading file: " + fuex.getMessage(), field, fuex); } } } if (newRecord) { sessionData.setRowId(table, newRowId); } this.logLastDataChangeTime(request); logLastTableDataChangeTime(table); UsageLogger usageLogger = new UsageLogger(this.dataSource); AppUserInfo user = null; if (request.getRemoteUser() == null) { user = ServletUtilMethods.getPublicUserForRequest(request, this.authManager.getAuthenticator()); } else { user = this.authManager.getUserByUserName(request, request.getRemoteUser()); } // Send websocket notification // UsageLogger.sendNotification(user, table, sessionData.getReport(), // rowId, "edit", "Record saved: " + dataToSave); // Log everything apart from hidden (auto set) fields Map<BaseField, BaseValue> dataToLog = new LinkedHashMap<BaseField, BaseValue>(); for (Map.Entry<BaseField, BaseValue> entrySet : dataToSave.entrySet()) { BaseField field = entrySet.getKey(); if (!field.getHidden()) { BaseValue value = entrySet.getValue(); dataToLog.put(field, value); } } if (newRecord) { usageLogger.logDataChange(user, table, null, AppAction.SAVE_NEW_RECORD, newRowId, dataToLog.toString()); } else if (globalEdit) { // TODO: need better logging of global edits usageLogger.logDataChange(user, table, null, AppAction.GLOBAL_EDIT, rowId, dataToLog.toString()); } else { BaseField fieldUpdated = null; Set<BaseField> fieldSet = new TreeSet<BaseField>(); for (BaseField field : dataToSave.keySet()) { if (!field.getHidden()) { fieldSet.add(field); } } if (fieldSet.size() == 1) { fieldUpdated = new LinkedList<BaseField>(fieldSet).getFirst(); } usageLogger.logDataChange(user, table, fieldUpdated, AppAction.UPDATE_RECORD, rowId, dataToLog.toString()); } UsageLogger.startLoggingThread(usageLogger); }
From source file:org.ecocean.servlet.EncounterForm.java
@Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); HashMap fv = new HashMap(); //IMPORTANT - processingNotes can be used to add notes on data handling (e.g., poorly formatted dates) that can be reconciled later by the reviewer //Example usage: processingNotes.append("<p>Error encountered processing this date submitted by user: "+getVal(fv, "datepicker")+"</p>"); StringBuffer processingNotes = new StringBuffer(); HttpSession session = request.getSession(true); String context = "context0"; context = ServletUtilities.getContext(request); Shepherd myShepherd = new Shepherd(context); myShepherd.setAction("EncounterForm.class"); System.out.println("in context " + context); //request.getSession()getServlet().getServletContext().getRealPath("/")); String rootDir = getServletContext().getRealPath("/"); System.out.println("rootDir=" + rootDir); /*/*from w ww . j av a2s .co m*/ Vector<String> fbImages = new Vector<String>(); int fbi = 0; while (request.getParameter("socialphoto_" + fbi) != null) { fbImages.add(request.getParameter("socialphoto_" + fbi)); fbi++; } System.out.println(fbImages); if (fbImages.size() > 0) { FacebookClient fbclient = null; try { fbclient = SocialAuth.getFacebookClient(context); } catch (Exception ex) { System.out.println("SocialAuth.getFacebookClient threw exception " + ex.toString()); } WebContext ctx = new J2EContext(request, response); //String callbackUrl = "http://localhost.wildme.org/a/SocialConnect?type=facebook"; String callbackUrl = "http://" + CommonConfiguration.getURLLocation(request) + "/XXXSocialConnect?type=facebook"; if (request.getParameter("disconnect") != null) callbackUrl += "&disconnect=1"; fbclient.setCallbackUrl(callbackUrl); OAuthCredentials credentials = null; try { credentials = fbclient.getCredentials(ctx); } catch (Exception ex) { System.out.println("caught exception on facebook credentials: " + ex.toString()); } if (credentials != null) { FacebookProfile facebookProfile = fbclient.getUserProfile(credentials, ctx); User fbuser = myShepherd.getUserBySocialId("facebook", facebookProfile.getId()); System.out.println("getId() = " + facebookProfile.getId() + " -> user = " + fbuser); if (fbuser != null) System.out.println("user = " + user.getUsername() + "; fbuser = " + fbuser.getUsername()); if ((fbuser != null) && (fbuser.getUsername().equals(user.getUsername())) && (request.getParameter("disconnect") != null)) { fbuser.unsetSocial("facebook"); //myShepherd.getPM().makePersistent(user); session.setAttribute("message", "disconnected from facebook"); response.sendRedirect("myAccount.jsp"); return; } else if (fbuser != null) { session.setAttribute("error", "looks like this account is already connected to an account"); response.sendRedirect("myAccount.jsp"); return; } else { //lets do this user.setSocial("facebook", facebookProfile.getId()); //myShepherd.getPM().makePersistent(user); session.setAttribute("message", "connected to facebook"); response.sendRedirect("myAccount.jsp"); return; } } else { System.out.println("*** trying redirect?"); try { fbclient.redirect(ctx, false, false); } catch (Exception ex) { System.out.println("caught exception on facebook processing: " + ex.toString()); } return; } } */ //private Map<String, Object> measurements = new HashMap<String, Object>(); //Map<String, Object> metalTags = new HashMap<String, Object>(); /* private String acousticTagSerial = ""; private String acousticTagId = ""; private String satelliteTagSerial = ""; private String satelliteTagArgosPttNumber = ""; private String satelliteTagName = ""; */ //set up for response response.setContentType("text/html"); PrintWriter out = response.getWriter(); boolean locked = false; String fileName = "None"; String username = "None"; String fullPathFilename = ""; boolean fileSuccess = false; //kinda pointless now as we just build sentFiles list now at this point (do file work at end) String doneMessage = ""; List<String> filesOK = new ArrayList<String>(); HashMap<String, String> filesBad = new HashMap<String, String>(); List<FileItem> formFiles = new ArrayList<FileItem>(); List<File> socialFiles = new ArrayList<File>(); //Calendar date = Calendar.getInstance(); long maxSizeMB = CommonConfiguration.getMaxMediaSizeInMegabytes(context); long maxSizeBytes = maxSizeMB * 1048576; if (ServletFileUpload.isMultipartContent(request)) { try { ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory()); upload.setHeaderEncoding("UTF-8"); List<FileItem> multiparts = upload.parseRequest(request); //List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); for (FileItem item : multiparts) { if (item.isFormField()) { //plain field fv.put(item.getFieldName(), ServletUtilities.preventCrossSiteScriptingAttacks(item.getString("UTF-8").trim())); //TODO do we want trim() here??? -jon //System.out.println("got regular field (" + item.getFieldName() + ")=(" + item.getString("UTF-8") + ")"); } else if (item.getName().startsWith("socialphoto_")) { System.out.println(item.getName() + ": " + item.getString("UTF-8")); } else { //file //System.out.println("content type???? " + item.getContentType()); TODO note, the helpers only check extension if (item.getSize() > maxSizeBytes) { filesBad.put(item.getName(), "file is larger than " + maxSizeMB + "MB"); } else if (myShepherd.isAcceptableImageFile(item.getName()) || myShepherd.isAcceptableVideoFile(item.getName())) { formFiles.add(item); filesOK.add(item.getName()); } else { filesBad.put(item.getName(), "invalid type of file"); } } } doneMessage = "File Uploaded Successfully"; fileSuccess = true; } catch (Exception ex) { doneMessage = "File Upload Failed due to " + ex; } } else { doneMessage = "Sorry this Servlet only handles file upload request"; } if (fv.get("social_files_id") != null) { //TODO better checking of files (size, type etc) File socDir = new File( ServletUtilities.dataDir(context, rootDir) + "/social_files/" + fv.get("social_files_id")); for (File sf : socDir.listFiles()) { socialFiles.add(sf); filesOK.add(sf.getName()); } filesBad = new HashMap<String, String>(); fileSuccess = true; } session.setAttribute("filesOKMessage", (filesOK.isEmpty() ? "none" : Arrays.toString(filesOK.toArray()))); String badmsg = ""; for (String key : filesBad.keySet()) { badmsg += key + " (" + getVal(filesBad, key) + ") "; } if (badmsg.equals("")) { badmsg = "none"; } session.setAttribute("filesBadMessage", badmsg); if (fileSuccess) { //////////////////////////////////////////// START //{submitterID=tomcat, submitterProject=, photographerEmail=, metalTag(left)=, sex=unknown, measurement(weight)=34234, location=, acousticTagId=, behavior=yow behavior..., measurement(weightunits)=kilograms, acousticTagSerial=, photographerName=, lifeStage=sub-adult, submitterAddress=, satelliteTagSerial=, releaseDate=, photographerPhone=, measurement(lengthunits)=meters, measurement(weightsamplingProtocol)=samplingProtocol0, measurement(length)=, submitterOrganization=, photographerAddress=, longitude=, year=2014, lat=, measurement(lengthsamplingProtocol)=samplingProtocol0, submitterEmail=, minutes=00, elevation=, measurement(height)=, measurement(heightsamplingProtocol)=samplingProtocol0, scars=None, submitterPhone=, submitterName=tomcat, hour=-1, livingStatus=alive, depth=, country=, satelliteTagName=Wild Life Computers, metalTag(right)=, month=1, measurement(heightunits)=meters, Submit=Send encounter report, informothers=, day=0, satelliteTagArgosPttNumber=, comments=} //check for spamBots TODO possibly move this to Util for general/global usage? boolean spamBot = false; String[] spamFieldsToCheck = new String[] { "submitterPhone", "submitterName", "photographerName", "photographerPhone", "location", "comments", "behavior" }; StringBuffer spamFields = new StringBuffer(); for (int i = 0; i < spamFieldsToCheck.length; i++) { spamFields.append(getVal(fv, spamFieldsToCheck[i])); } if (spamFields.toString().toLowerCase().indexOf("porn") != -1) { spamBot = true; } if (spamFields.toString().toLowerCase().indexOf("href") != -1) { spamBot = true; } //else if(spamFields.toString().toLowerCase().indexOf("[url]")!=-1){spamBot=true;} //else if(spamFields.toString().toLowerCase().indexOf("url=")!=-1){spamBot=true;} //else if(spamFields.toString().toLowerCase().trim().equals("")){spamBot=true;} //else if((theForm.getSubmitterID()!=null)&&(theForm.getSubmitterID().equals("N%2FA"))) {spamBot=true;} String locCode = ""; System.out.println(" **** here is what i think locationID is: " + fv.get("locationID")); if ((fv.get("locationID") != null) && !fv.get("locationID").toString().equals("")) { locCode = fv.get("locationID").toString(); } //see if the location code can be determined and set based on the location String reported else if (fv.get("location") != null) { String locTemp = getVal(fv, "location").toLowerCase(); Properties props = new Properties(); try { props = ShepherdProperties.getProperties("submitActionClass.properties", "", context); Enumeration m_enum = props.propertyNames(); while (m_enum.hasMoreElements()) { String aLocationSnippet = ((String) m_enum.nextElement()).trim(); if (locTemp.indexOf(aLocationSnippet) != -1) { locCode = props.getProperty(aLocationSnippet); } } } catch (Exception props_e) { props_e.printStackTrace(); } } //end else //end location code setter fv.put("locCode", locCode); //TODO this should live somewhere else as constant? (e.g. to build in form as well) String[] scarType = new String[] { "None", "Tail (caudal) fin", "1st dorsal fin", "2nd dorsal fin", "Left pectoral fin", "Right pectoral fin", "Head", "Body" }; int scarNum = -1; try { scarNum = Integer.parseInt(getVal(fv, "scars")); } catch (NumberFormatException e) { scarNum = -1; } if ((scarNum < 0) || (scarNum > 7)) { scarNum = -1; } if (scarNum >= 0) { fv.put("scars", scarType[scarNum]); } //System.out.println("about to do int stuff"); //need some ints for day/month/year/hour (other stuff seems to be strings) int day = 0, month = -1, year = 0, hour = 0; String minutes = ""; //try { day = Integer.parseInt(getVal(fv, "day")); } catch (NumberFormatException e) { day = 0; } //try { month = Integer.parseInt(getVal(fv, "month")); } catch (NumberFormatException e) { month = 0; } //try { year = Integer.parseInt(getVal(fv, "year")); } catch (NumberFormatException e) { year = 0; } //switch to datepicker LocalDateTime dt = new LocalDateTime(); if ((getVal(fv, "datepicker") != null) && (!getVal(fv, "datepicker").trim().equals(""))) { //System.out.println("Trying to read date: "+getVal(fv, "datepicker").replaceAll(" ", "T")); //boolean badDate=false; try { DateTimeFormatter parser1 = ISODateTimeFormat.dateOptionalTimeParser(); LocalDateTime reportedDateTime = new LocalDateTime( parser1.parseMillis(getVal(fv, "datepicker").replaceAll(" ", "T"))); StringTokenizer str = new StringTokenizer(getVal(fv, "datepicker").replaceAll(" ", "T"), "-"); int numTokens = str.countTokens(); if (numTokens >= 1) { //try { year = reportedDateTime.getYear(); if (year > (dt.getYear() + 1)) { //badDate=true; year = 0; throw new Exception( " An unknown exception occurred during date processing in EncounterForm. The user may have input an improper format: " + year + " > " + dt.getYear()); } //} catch (Exception e) { year=-1;} } if (numTokens >= 2) { try { month = reportedDateTime.getMonthOfYear(); } catch (Exception e) { month = -1; } } else { month = -1; } //see if we can get a day, because we do want to support only yyy-MM too if (str.countTokens() >= 3) { try { day = reportedDateTime.getDayOfMonth(); } catch (Exception e) { day = 0; } } else { day = 0; } //see if we can get a time and hour, because we do want to support only yyy-MM too StringTokenizer strTime = new StringTokenizer(getVal(fv, "datepicker").replaceAll(" ", "T"), "T"); if (strTime.countTokens() > 1) { try { hour = reportedDateTime.getHourOfDay(); } catch (Exception e) { hour = -1; } try { minutes = (new Integer(reportedDateTime.getMinuteOfHour()).toString()); } catch (Exception e) { } } else { hour = -1; } //System.out.println("At the end of time processing I see: "+year+"-"+month+"-"+day+" "+hour+":"+minutes); } catch (Exception e) { System.out.println( " An unknown exception occurred during date processing in EncounterForm. The user may have input an improper format."); e.printStackTrace(); processingNotes.append("<p>Error encountered processing this date submitted by user: " + getVal(fv, "datepicker") + "</p>"); } } String guess = "no estimate provided"; if ((fv.get("guess") != null) && !fv.get("guess").toString().equals("")) { guess = fv.get("guess").toString(); } //let's handle genus and species for taxonomy String genus = null; String specificEpithet = null; try { //now we have to break apart genus species if (fv.get("genusSpecies") != null) { StringTokenizer tokenizer = new StringTokenizer(fv.get("genusSpecies").toString(), " "); if (tokenizer.countTokens() >= 2) { genus = tokenizer.nextToken(); //enc.setGenus(tokenizer.nextToken()); specificEpithet = tokenizer.nextToken().replaceAll(",", "").replaceAll("_", " "); //enc.setSpecificEpithet(tokenizer.nextToken().replaceAll(",","").replaceAll("_"," ")); } //handle malformed Genus Species formats else { throw new Exception( "The format of the submitted genusSpecies parameter did not have two tokens delimited by a space (e.g., \"Rhincodon typus\"). The submitted value was: " + fv.get("genusSpecies")); } } } catch (Exception le) { } System.out.println("about to do enc()"); Encounter enc = new Encounter(day, month, year, hour, minutes, guess, getVal(fv, "location"), getVal(fv, "submitterName"), getVal(fv, "submitterEmail"), null); boolean llSet = false; //Encounter enc = new Encounter(); //System.out.println("Submission detected date: "+enc.getDate()); String encID = enc.generateEncounterNumber(); enc.setEncounterNumber(encID); System.out.println("hey, i think i may have made an encounter, encID=" + encID); System.out.println("enc ?= " + enc.toString()); AssetStore astore = AssetStore.getDefault(myShepherd); ArrayList<Annotation> newAnnotations = new ArrayList<Annotation>(); for (FileItem item : formFiles) { JSONObject sp = astore.createParameters(new File(enc.subdir() + File.separator + item.getName())); sp.put("key", Util.hashDirectories(encID) + "/" + item.getName()); MediaAsset ma = new MediaAsset(astore, sp); File tmpFile = ma.localPath().toFile(); //conveniently(?) our local version to save ma.cacheLocal() from having to do anything? File tmpDir = tmpFile.getParentFile(); if (!tmpDir.exists()) tmpDir.mkdirs(); //System.out.println("attempting to write uploaded file to " + tmpFile); try { item.write(tmpFile); } catch (Exception ex) { System.out.println("Could not write " + tmpFile + ": " + ex.toString()); } if (tmpFile.exists()) { ma.addLabel("_original"); ma.copyIn(tmpFile); ma.updateMetadata(); newAnnotations.add(new Annotation(Util.taxonomyString(genus, specificEpithet), ma)); } else { System.out.println("failed to write file " + tmpFile); } } ///////////////////TODO social files also!!! if (fv.get("mediaAssetSetId") != null) { MediaAssetSet maSet = ((MediaAssetSet) (myShepherd.getPM().getObjectById( myShepherd.getPM().newObjectIdInstance(MediaAssetSet.class, fv.get("mediaAssetSetId")), true))); if ((maSet != null) && (maSet.getMediaAssets() != null) && (maSet.getMediaAssets().size() > 0)) { int num = maSet.getMediaAssets().size(); for (MediaAsset ma : maSet.getMediaAssets()) { newAnnotations.add(new Annotation(Util.taxonomyString(genus, specificEpithet), ma)); } session.setAttribute("filesOKMessage", num + " " + ((num == 1) ? "file" : "files")); } } enc.setAnnotations(newAnnotations); enc.setGenus(genus); enc.setSpecificEpithet(specificEpithet); /* String baseDir = ServletUtilities.dataDir(context, rootDir); ArrayList<SinglePhotoVideo> images = new ArrayList<SinglePhotoVideo>(); for (FileItem item : formFiles) { // this will actually write file to filesystem (or [FUTURE] wherever) // TODO: either (a) undo this if any failure of writing encounter; or (b) dont write til success of enc. try { //SinglePhotoVideo spv = new SinglePhotoVideo(encID, item, context, encDataDir); SinglePhotoVideo spv = new SinglePhotoVideo(enc, item, context, baseDir); //images.add(spv); enc.addSinglePhotoVideo(spv); } catch (Exception ex) { System.out.println("failed to save " + item.toString() + ": " + ex.toString()); } } for (File sf : socialFiles) { File encDir = new File(enc.dir(baseDir)); if (!encDir.exists()) encDir.mkdirs(); File targetFile = new File(encDir, sf.getName()); System.out.println("socialFile copy: " + sf.toString() + " ---> " + targetFile.toString()); Files.copy(sf.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); SinglePhotoVideo spv = new SinglePhotoVideo(encID, targetFile); enc.addSinglePhotoVideo(spv); } */ //now let's add our encounter to the database enc.setComments(getVal(fv, "comments").replaceAll("\n", "<br>")); if (fv.get("releaseDate") != null && fv.get("releaseDate").toString().length() > 0) { String dateFormatPattern = CommonConfiguration.getProperty("releaseDateFormat", context); try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormatPattern); enc.setReleaseDate(simpleDateFormat.parse(fv.get("releaseDate").toString()).getTime()); } catch (Exception e) { enc.addComments("<p>Reported release date was problematic: " + fv.get("releaseDate") + "</p>"); } } if (fv.get("behavior") != null && fv.get("behavior").toString().length() > 0) { enc.setBehavior(fv.get("behavior").toString()); } if (fv.get("alternateID") != null && fv.get("alternateID").toString().length() > 0) { enc.setAlternateID(fv.get("alternateID").toString()); } if (fv.get("lifeStage") != null && fv.get("lifeStage").toString().length() > 0) { enc.setLifeStage(fv.get("lifeStage").toString()); } List<MetalTag> metalTags = getMetalTags(fv); for (MetalTag metalTag : metalTags) { enc.addMetalTag(metalTag); } List<Measurement> measurements = getMeasurements(fv, encID, context); for (Measurement measurement : measurements) { enc.setMeasurement(measurement, myShepherd); } enc.setAcousticTag(getAcousticTag(fv)); enc.setSatelliteTag(getSatelliteTag(fv)); enc.setSex(getVal(fv, "sex")); enc.setLivingStatus(getVal(fv, "livingStatus")); if (fv.get("scars") != null) { enc.setDistinguishingScar(fv.get("scars").toString()); } int sizePeriod = 0; if ((fv.get("measureUnits") != null) && fv.get("measureUnits").toString().equals("Feet")) { if ((fv.get("depth") != null) && !fv.get("depth").toString().equals("")) { try { double tempDouble = (new Double(fv.get("depth").toString())).doubleValue() / 3.3; String truncDepth = (new Double(tempDouble)).toString(); sizePeriod = truncDepth.indexOf("."); truncDepth = truncDepth.substring(0, sizePeriod + 2); fv.put("depth", (new Double(truncDepth)).toString()); } catch (java.lang.NumberFormatException nfe) { enc.addComments( "<p>Reported depth was problematic: " + fv.get("depth").toString() + "</p>"); fv.put("depth", ""); } catch (NullPointerException npe) { fv.put("depth", ""); } } System.out.println("depth --> " + fv.get("depth").toString()); if ((fv.get("elevation") != null) && !fv.get("elevation").toString().equals("")) { try { double tempDouble = (new Double(fv.get("elevation").toString())).doubleValue() / 3.3; String truncElev = (new Double(tempDouble)).toString(); //String truncElev = ((new Double(elevation)) / 3.3).toString(); sizePeriod = truncElev.indexOf("."); truncElev = truncElev.substring(0, sizePeriod + 2); fv.put("elevation", (new Double(truncElev)).toString()); } catch (java.lang.NumberFormatException nfe) { enc.addComments("<p>Reported elevation was problematic: " + fv.get("elevation").toString() + "</p>"); fv.put("elevation", ""); } catch (NullPointerException npe) { fv.put("elevation", ""); } } if ((fv.get("size") != null) && !fv.get("size").toString().equals("")) { try { double tempDouble = (new Double(fv.get("size").toString())).doubleValue() / 3.3; String truncSize = (new Double(tempDouble)).toString(); //String truncSize = ((new Double(size)) / 3.3).toString(); sizePeriod = truncSize.indexOf("."); truncSize = truncSize.substring(0, sizePeriod + 2); fv.put("size", (new Double(truncSize)).toString()); } catch (java.lang.NumberFormatException nfe) { enc.addComments("<p>Reported size was problematic: " + fv.get("size").toString() + "</p>"); fv.put("size", ""); } catch (NullPointerException npe) { fv.put("size", ""); } } } //measureUnits if ((fv.get("size") != null) && !fv.get("size").toString().equals("")) { try { enc.setSize(new Double(fv.get("size").toString())); } catch (java.lang.NumberFormatException nfe) { enc.addComments("<p>Reported size was problematic: " + fv.get("size").toString() + "</p>"); fv.put("size", ""); } catch (NullPointerException npe) { fv.put("size", ""); } } if ((fv.get("elevation") != null) && !fv.get("elevation").toString().equals("")) { try { enc.setMaximumElevationInMeters(new Double(fv.get("elevation").toString())); } catch (java.lang.NumberFormatException nfe) { enc.addComments( "<p>Reported elevation was problematic: " + fv.get("elevation").toString() + "</p>"); fv.put("elevatoin", ""); } catch (NullPointerException npe) { fv.put("elevation", ""); } } if ((fv.get("depth") != null) && !fv.get("depth").toString().equals("")) { try { enc.setDepth(new Double(fv.get("depth").toString())); } catch (java.lang.NumberFormatException nfe) { enc.addComments("<p>Reported depth was problematic: " + fv.get("depth").toString() + "</p>"); fv.put("depth", ""); } catch (NullPointerException npe) { fv.put("depth", ""); } } //let's handle the GPS if ((fv.get("lat") != null) && (fv.get("longitude") != null) && !fv.get("lat").toString().equals("") && !fv.get("longitude").toString().equals("")) { //enc.setGPSLatitude(lat + "° " + gpsLatitudeMinutes + "\' " + gpsLatitudeSeconds + "\" " + latDirection); try { double degrees = (new Double(fv.get("lat").toString())).doubleValue(); double position = degrees; /* if (!gpsLatitudeMinutes.equals("")) { double minutes2 = ((new Double(gpsLatitudeMinutes)).doubleValue()) / 60; position += minutes2; } if (!gpsLatitudeSeconds.equals("")) { double seconds2 = ((new Double(gpsLatitudeSeconds)).doubleValue()) / 3600; position += seconds2; } if (latDirection.toLowerCase().equals("south")) { position = position * -1; }*/ enc.setDWCDecimalLatitude(position); double degrees2 = (new Double(fv.get("longitude").toString())).doubleValue(); double position2 = degrees2; enc.setDWCDecimalLongitude(position2); llSet = true; } catch (Exception e) { System.out.println("EncounterSetGPS: problem!"); e.printStackTrace(); } } //enc.setMeasureUnits("Meters"); enc.setSubmitterPhone(getVal(fv, "submitterPhone")); enc.setSubmitterAddress(getVal(fv, "submitterAddress")); enc.setSubmitterOrganization(getVal(fv, "submitterOrganization")); enc.setSubmitterProject(getVal(fv, "submitterProject")); enc.setPhotographerPhone(getVal(fv, "photographerPhone")); enc.setPhotographerAddress(getVal(fv, "photographerAddress")); enc.setPhotographerName(getVal(fv, "photographerName")); enc.setPhotographerEmail(getVal(fv, "photographerEmail")); enc.addComments("<p>Submitted on " + (new java.util.Date()).toString() + " from address: " + request.getRemoteHost() + "</p>"); //enc.approved = false; enc.addComments(processingNotes.toString()); if (CommonConfiguration.getProperty("encounterState0", context) != null) { enc.setState(CommonConfiguration.getProperty("encounterState0", context)); } if (request.getRemoteUser() != null) { enc.setSubmitterID(request.getRemoteUser()); } else { enc.setSubmitterID("N/A"); } if (!getVal(fv, "locCode").equals("")) { enc.setLocationCode(locCode); } if (!getVal(fv, "country").equals("")) { enc.setCountry(getVal(fv, "country")); } if (!getVal(fv, "informothers").equals("")) { enc.setInformOthers(getVal(fv, "informothers")); } // xxxxxxx //add research team for GAq if (!getVal(fv, "researchTeam").equals("")) { enc.setDynamicProperty("Research Team", (getVal(fv, "researchTeam"))); } if (!getVal(fv, "vessel").equals("")) { enc.setDynamicProperty("Vessel", (getVal(fv, "vessel"))); } if (!getVal(fv, "conditions").equals("")) { enc.setDynamicProperty("Conditions", (getVal(fv, "conditions"))); } if (!getVal(fv, "camera").equals("")) { enc.setDynamicProperty("Camera", (getVal(fv, "camera"))); } if (!getVal(fv, "lens").equals("")) { enc.setDynamicProperty("Lens", (getVal(fv, "lens"))); } if (!getVal(fv, "card").equals("")) { enc.setDynamicProperty("Card", (getVal(fv, "card"))); } if (!getVal(fv, "folder").equals("")) { enc.setDynamicProperty("Folder", (getVal(fv, "folder"))); } if (!getVal(fv, "numberOfBoats").equals("")) { enc.setDynamicProperty("Number of boats", (getVal(fv, "numberOfBoats"))); } if (!getVal(fv, "startTime").equals("")) { enc.setDynamicProperty("Start Time", (getVal(fv, "startTime"))); } if (!getVal(fv, "endTime").equals("")) { enc.setDynamicProperty("End Time", (getVal(fv, "endTime"))); } if (!getVal(fv, "endLongitude").equals("")) { enc.setDynamicProperty("End Longitude", (getVal(fv, "endLongitude"))); } if (!getVal(fv, "endLatitude").equals("")) { enc.setDynamicProperty("End Latitude", (getVal(fv, "endLatitude"))); } if (!getVal(fv, "startLongitude").equals("")) { enc.setDynamicProperty("Start Longitude", (getVal(fv, "startLongitude"))); } if (!getVal(fv, "startLatitude").equals("")) { enc.setDynamicProperty("Start Latitude", (getVal(fv, "startLatitude"))); } if (!getVal(fv, "beginWaypoint").equals("")) { enc.setDynamicProperty("Begin Waypoint", (getVal(fv, "beginWaypoint"))); } if (!getVal(fv, "endWaypoint").equals("")) { enc.setDynamicProperty("End Waypoint", (getVal(fv, "endWaypoint"))); } //xxxxxxxx String guid = CommonConfiguration.getGlobalUniqueIdentifierPrefix(context) + encID; //new additions for DarwinCore enc.setDWCGlobalUniqueIdentifier(guid); enc.setDWCImageURL((request.getScheme() + "://" + CommonConfiguration.getURLLocation(request) + "/encounters/encounter.jsp?number=" + encID)); //populate DarwinCore dates DateTimeFormatter fmt = ISODateTimeFormat.date(); String strOutputDateTime = fmt.print(dt); enc.setDWCDateAdded(strOutputDateTime); enc.setDWCDateAdded(new Long(dt.toDateTime().getMillis())); //System.out.println("I set the date as a LONG to: "+enc.getDWCDateAddedLong()); enc.setDWCDateLastModified(strOutputDateTime); //this will try to set from MediaAssetMetadata -- ymmv if (!llSet) enc.setLatLonFromAssets(); if (enc.getYear() < 1) enc.setDateFromAssets(); String newnum = ""; if (!spamBot) { newnum = myShepherd.storeNewEncounter(enc, encID); //enc.refreshAssetFormats(context, ServletUtilities.dataDir(context, rootDir)); enc.refreshAssetFormats(myShepherd); Logger log = LoggerFactory.getLogger(EncounterForm.class); log.info("New encounter submission: <a href=\"" + request.getScheme() + "://" + CommonConfiguration.getURLLocation(request) + "/encounters/encounter.jsp?number=" + encID + "\">" + encID + "</a>"); System.out.println("ENCOUNTER SAVED???? newnum=" + newnum); } if (newnum.equals("fail")) { request.setAttribute("number", "fail"); return; } //return a forward to display.jsp System.out.println("Ending data submission."); if (!spamBot) { response.sendRedirect(request.getScheme() + "://" + CommonConfiguration.getURLLocation(request) + "/confirmSubmit.jsp?number=" + encID); } else { response.sendRedirect( request.getScheme() + "://" + CommonConfiguration.getURLLocation(request) + "/spambot.jsp"); } } //end "if (fileSuccess) myShepherd.closeDBTransaction(); //return null; }
From source file:ub.botiga.ServletDispatcher.java
private void controlUser(HttpServletRequest request, HttpServletResponse response) { String user = request.getRemoteUser(); if (user != null) { User u = data.addUser(user);// w ww . j ava 2s . c om HttpSession s = request.getSession(); s.setAttribute("user", u); } }