Example usage for javax.servlet.http HttpServletRequest getScheme

List of usage examples for javax.servlet.http HttpServletRequest getScheme

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getScheme.

Prototype

public String getScheme();

Source Link

Document

Returns the name of the scheme used to make this request, for example, <code>http</code>, <code>https</code>, or <code>ftp</code>.

Usage

From source file:com.viewer.controller.ViewerController.java

@RequestMapping(value = "/ViewDocument", method = RequestMethod.POST, headers = {
        "Content-type=application/json" })
@ResponseBody/*ww w  .  ja  v a  2 s .c  o m*/
public ViewDocumentResponse viewDoc(@RequestBody ViewDocumentParameters params, HttpServletRequest request)
        throws Exception {
    // To Set License
    License lic = new License();
    lic.setLicense(_licensePath);
    params.setUseHtmlBasedEngine(true);
    if (params.getUseHtmlBasedEngine()) {

        DocumentInfoContainer docInfo = _htmlHandler.getDocumentInfo(new DocumentInfoOptions(params.getPath()));
        int maxWidth = 0;
        int maxHeight = 0;
        for (PageData pageData : docInfo.getPages()) {
            if (pageData.getHeight() > maxHeight) {
                maxHeight = pageData.getHeight();
                maxWidth = pageData.getWidth();
            }
        }
        FileData fileData = new FileData();

        fileData.setDateCreated(new Date());
        fileData.setDateModified(docInfo.getLastModificationDate());
        fileData.setPageCount(docInfo.getPages().size());
        fileData.setPages(docInfo.getPages());
        fileData.setMaxWidth(maxWidth);
        fileData.setMaxHeight(maxHeight);

        ViewDocumentResponse result = new ViewDocumentResponse();
        result.setPageCss(new String[0]);
        result.setLic(true);
        result.setPdfDownloadUrl(GetPdfDownloadUrl(params));
        result.setPdfPrintUrl(GetPdfPrintUrl(params));
        result.setUrl(GetFileUrl(params));
        result.setPath(params.getPath());
        result.setName(params.getPath());
        try {
            result.setDocumentDescription(
                    (new FileDataJsonSerializer(fileData, new FileDataOptions())).Serialize(false));
        } catch (ParseException x) {
            throw new ServletException(x);
        }
        result.setDocumentDescription(
                (new FileDataJsonSerializer(fileData, new FileDataOptions())).Serialize(false));
        result.setDocType(docInfo.getDocumentType());
        result.setFileType(docInfo.getFileType());

        HtmlOptions htmlOptions = new HtmlOptions();
        htmlOptions.setResourcesEmbedded(true);

        htmlOptions.setHtmlResourcePrefix("/GetResourceForHtml?documentPath=" + params.getPath()
                + "&pageNumber={page-number}&resourceName=");

        if (!DotNetToJavaStringHelper.isNullOrEmpty(params.getPreloadPagesCount().toString())
                && params.getPreloadPagesCount().intValue() > 0) {
            htmlOptions.setPageNumber(1);
            htmlOptions.setCountPagesToConvert(params.getPreloadPagesCount().intValue());
        }

        String[] cssList = null;

        RefObject<ArrayList<String>> tempRef_cssList = new RefObject<ArrayList<String>>(cssList);

        List<PageHtml> htmlPages = GetHtmlPages(params.getPath(), htmlOptions);
        cssList = tempRef_cssList.argValue;

        ArrayList<String> pagesContent = new ArrayList<String>();
        for (PageHtml page : htmlPages) {
            pagesContent.add(page.getHtmlContent());
        }
        String[] htmlContent = pagesContent.toArray(new String[0]);
        result.setPageHtml(htmlContent);
        result.setPageCss(new String[] { String.join(" ", temp_cssList) });

        for (int i = 0; i < result.getPageHtml().length; i++) {
            String html = result.getPageHtml()[i];
            int indexOfScript = html.indexOf("script");
            if (indexOfScript > 0) {
                result.getPageHtml()[i] = html.substring(0, indexOfScript);
            }
        }

        return result;

    } else {

        DocumentInfoContainer docInfo = _imageHandler
                .getDocumentInfo(new DocumentInfoOptions(params.getPath()));
        int maxWidth = 0;
        int maxHeight = 0;
        for (PageData pageData : docInfo.getPages()) {
            if (pageData.getHeight() > maxHeight) {
                maxHeight = pageData.getHeight();
                maxWidth = pageData.getWidth();
            }
        }
        FileData fileData = new FileData();

        fileData.setDateCreated(new Date());
        fileData.setDateModified(docInfo.getLastModificationDate());
        fileData.setPageCount(docInfo.getPages().size());
        fileData.setPages(docInfo.getPages());
        fileData.setMaxWidth(maxWidth);
        fileData.setMaxHeight(maxHeight);

        ViewDocumentResponse result = new ViewDocumentResponse();
        result.setPageCss(new String[0]);
        result.setLic(true);
        result.setPdfDownloadUrl(GetPdfDownloadUrl(params));
        result.setPdfPrintUrl(GetPdfPrintUrl(params));
        params.setUseHtmlBasedEngine(true);
        result.setUrl(GetFileUrl(params.getPath(), true, false, params.getFileDisplayName(),
                params.getWatermarkText(), params.getWatermarkColor(), params.getWatermarkPostion(),
                params.getWatermarkWidth(), params.getIgnoreDocumentAbsence(), params.getUseHtmlBasedEngine(),
                params.getSupportPageRotation()));
        result.setPath(params.getPath());
        result.setName(params.getPath());
        result.setDocumentDescription(
                (new FileDataJsonSerializer(fileData, new FileDataOptions())).Serialize(false));
        result.setDocType(docInfo.getDocumentType());
        result.setFileType(docInfo.getFileType());

        int[] pageNumbers = new int[docInfo.getPages().size()];
        int count = 0;
        for (PageData page : docInfo.getPages()) {

            pageNumbers[count] = page.getNumber();
            count++;
        }
        String applicationHost = request.getScheme() + "://" + request.getServerName() + ":"
                + request.getServerPort();
        String[] imageUrls = ImageUrlHelper.GetImageUrls(applicationHost, pageNumbers, params);

        result.setImageUrls(imageUrls);
        return result;

    }

}

From source file:com.concursive.connect.web.controller.hooks.SecurityHook.java

/**
 * Checks to see if a User session object exists, if not then the security
 * check fails.//from  w w w.  j a v  a  2s .c om
 *
 * @param servlet  Description of the Parameter
 * @param request  Description of the Parameter
 * @param response Description of the Parameter
 * @return Description of the Return Value
 */
public String beginRequest(Servlet servlet, HttpServletRequest request, HttpServletResponse response) {
    LOG.debug("Security request made");
    ServletConfig config = servlet.getServletConfig();
    ServletContext context = config.getServletContext();
    // Application wide preferences
    ApplicationPrefs prefs = (ApplicationPrefs) context.getAttribute(Constants.APPLICATION_PREFS);
    // Get the intended action, if going to the login module, then let it proceed
    String s = request.getServletPath();
    int slash = s.lastIndexOf("/");
    s = s.substring(slash + 1);
    // If not setup then go to setup
    if (!s.startsWith("Setup") && !prefs.isConfigured()) {
        return "NotSetupError";
    }
    // External calls
    if (s.startsWith("Service")) {
        return null;
    }
    // Set the layout relevant to this request
    if ("text".equals(request.getParameter("out")) || "text".equals(request.getAttribute("out"))) {
        // do not use a layout, send the raw output
    } else if ("true".equals(request.getParameter(Constants.REQUEST_PARAM_POPUP))) {
        request.setAttribute(Constants.REQUEST_PAGE_LAYOUT, "/layout1.jsp");
    } else if ("true".equals(request.getParameter(Constants.REQUEST_PARAM_STYLE))) {
        request.setAttribute(Constants.REQUEST_PAGE_LAYOUT, "/layout1.jsp");
    } else {
        request.setAttribute(Constants.REQUEST_PAGE_LAYOUT, context.getAttribute(Constants.REQUEST_TEMPLATE));
    }
    // If going to Setup then allow
    if (s.startsWith("Setup")) {
        request.setAttribute(Constants.REQUEST_PAGE_LAYOUT, "/layout1.jsp");
        return null;
    }
    // If going to Upgrade then allow
    if (s.startsWith("Upgrade")) {
        request.setAttribute(Constants.REQUEST_PAGE_LAYOUT, "/layout1.jsp");
        return null;
    }
    // Detect mobile users and mobile formatting
    //    ClientType clientType = (ClientType) request.getSession().getAttribute(Constants.SESSION_CLIENT_TYPE);
    // @todo introduce when mobile is implemented
    //    if (clientType.getMobile()) {
    //      request.setAttribute(Constants.REQUEST_PAGE_LAYOUT, "/layoutMobile.jsp");
    //    }

    // URL forwarding for MVC requests (*.do, etc.)
    String requestedPath = (String) request.getAttribute("requestedURL");
    if (requestedPath == null && "GET".equals(request.getMethod())
            && request.getParameter("redirectTo") == null) {
        // Save the requestURI to be used downstream
        String contextPath = request.getContextPath();
        String uri = request.getRequestURI();
        String queryString = request.getQueryString();
        requestedPath = uri.substring(contextPath.length()) + (queryString == null ? "" : "?" + queryString);
        LOG.debug("Requested path: " + requestedPath);
        request.setAttribute("requestedURL", requestedPath);

        // It's important the user is using the correct URL for accessing content;
        // The portal has it's own redirect scheme, this is a general catch all
        if (!s.startsWith("Portal") && !uri.contains(".shtml") && prefs.has(ApplicationPrefs.WEB_DOMAIN_NAME)) {
            // Check to see if an old domain name is used
            PortalBean bean = new PortalBean(request);
            String expectedDomainName = prefs.get(ApplicationPrefs.WEB_DOMAIN_NAME);
            if (expectedDomainName != null && requestedPath != null && !"127.0.0.1".equals(bean.getServerName())
                    && !"127.0.0.1".equals(expectedDomainName) && !"localhost".equals(bean.getServerName())
                    && !"localhost".equals(expectedDomainName)
                    && !bean.getServerName().equals(expectedDomainName)
                    && !bean.getServerName().startsWith("10.") && !bean.getServerName().startsWith("172.")
                    && !bean.getServerName().startsWith("192.")) {
                if (uri != null && !uri.substring(1).equals(prefs.get("PORTAL.INDEX"))) {
                    String newUrl = URLFactory.createURL(prefs.getPrefs()) + requestedPath;
                    request.setAttribute("redirectTo", newUrl);
                    LOG.debug("redirectTo: " + newUrl);
                    request.removeAttribute(Constants.REQUEST_PAGE_LAYOUT);
                    return "Redirect301";
                }
            }
        }
    }

    // Version check
    if (!s.startsWith("Login") && ApplicationVersion.isOutOfDate(prefs)) {
        return "UpgradeCheck";
    }
    // Get the user object from the Session Validation...
    if (sessionValidator == null) {
        sessionValidator = SessionValidatorFactory.getInstance()
                .getSessionValidator(servlet.getServletConfig().getServletContext(), request);
        LOG.debug("Found sessionValidator? " + (sessionValidator != null));
    }
    // Check cookie for user's previous location info
    SearchBean searchBean = (SearchBean) request.getSession().getAttribute(Constants.SESSION_SEARCH_BEAN);
    if (searchBean == null) {
        String searchLocation = CookieUtils.getCookieValue(request, Constants.COOKIE_USER_SEARCH_LOCATION);
        if (searchLocation != null) {
            LOG.debug("Setting search location from cookie: " + searchLocation);
            searchBean = new SearchBean();
            searchBean.setLocation(searchLocation);
            request.getSession().setAttribute(Constants.SESSION_SEARCH_BEAN, searchBean);
        }
    }
    // Continue with session creation...
    User userSession = sessionValidator.validateSession(context, request, response);
    ConnectionElement ceSession = (ConnectionElement) request.getSession()
            .getAttribute(Constants.SESSION_CONNECTION_ELEMENT);
    // The user is going to the portal so get them guest credentials if they need them
    if ("true".equals(prefs.get("PORTAL")) || s.startsWith("Register") || s.startsWith("ResetPassword")
            || s.startsWith("LoginAccept") || s.startsWith("LoginReject") || s.startsWith("Search")
            || s.startsWith("ContactUs")) {
        if (userSession == null || ceSession == null) {
            // Allow portal mode to create a default session with guest capabilities
            userSession = UserUtils.createGuestUser();
            request.getSession().setAttribute(Constants.SESSION_USER, userSession);
            // Give them a connection element
            ceSession = new ConnectionElement();
            ceSession.setDriver(prefs.get("SITE.DRIVER"));
            ceSession.setUrl(prefs.get("SITE.URL"));
            ceSession.setUsername(prefs.get("SITE.USER"));
            ceSession.setPassword(prefs.get("SITE.PASSWORD"));
            request.getSession().setAttribute(Constants.SESSION_CONNECTION_ELEMENT, ceSession);
        }
        // Make sure SSL is being used for this connection
        if (userSession.getId() > 0 && "true".equals(prefs.get("SSL"))
                && !"https".equals(request.getScheme())) {
            LOG.info("Redirecting to..." + requestedPath);
            request.setAttribute("redirectTo",
                    "https://" + request.getServerName() + request.getContextPath() + requestedPath);
            request.removeAttribute(Constants.REQUEST_PAGE_LAYOUT);
            return "Redirect301";
        }
        // Generate global items
        Connection db = null;
        try {
            db = getConnection(context, request);
            // TODO: Implement cache since every hit needs this list
            if (db != null) {
                // Load the project languages
                WebSiteLanguageList webSiteLanguageList = new WebSiteLanguageList();
                webSiteLanguageList.setEnabled(Constants.TRUE);
                webSiteLanguageList.buildList(db);
                // If an admin of a language, enable it...
                webSiteLanguageList.add(userSession.getWebSiteLanguageList());
                request.setAttribute("webSiteLanguageList", webSiteLanguageList);

                // Load the menu list
                ProjectList menu = new ProjectList();
                PagedListInfo menuListInfo = new PagedListInfo();
                menuListInfo.setColumnToSortBy("p.portal_default desc, p.level asc, p.title asc");
                menuListInfo.setItemsPerPage(-1);
                menu.setPagedListInfo(menuListInfo);
                menu.setIncludeGuestProjects(false);
                menu.setPortalState(Constants.TRUE);
                menu.setOpenProjectsOnly(true);
                menu.setApprovedOnly(true);
                menu.setBuildLink(true);
                menu.setLanguageId(webSiteLanguageList.getLanguageId(request));
                menu.buildList(db);
                request.setAttribute("menuList", menu);

                // Load the main profile to use throughout
                Project mainProfile = ProjectUtils.loadProject(prefs.get("MAIN_PROFILE"));
                request.setAttribute(Constants.REQUEST_MAIN_PROFILE, mainProfile);

                // Load the project categories for search and tab menus
                ProjectCategoryList categoryList = new ProjectCategoryList();
                categoryList.setEnabled(Constants.TRUE);
                categoryList.setTopLevelOnly(true);
                categoryList.buildList(db);
                request.setAttribute(Constants.REQUEST_TAB_CATEGORY_LIST, categoryList);

                // Determine the tab that needs to be highlighted
                int chosenTabId = -1;
                if (requestedPath != null) {
                    String chosenCategory = null;
                    String chosenTab = null;
                    if (requestedPath.length() > 0) {
                        chosenTab = requestedPath.substring(1);
                    }
                    LOG.debug("chosenTab? " + chosenTab);
                    if (chosenTab == null || "".equals(chosenTab)) {
                        LOG.debug("Setting tab to Home");
                        chosenTab = "home.shtml";
                    } else {
                        boolean foundChosenTab = false;
                        for (ProjectCategory projectCategory : categoryList) {
                            String categoryName = projectCategory.getDescription();
                            String normalizedCategoryTab = projectCategory.getNormalizedCategoryName()
                                    .concat(".shtml");
                            if (chosenTab.startsWith(normalizedCategoryTab)) {
                                foundChosenTab = true;
                                chosenCategory = categoryName;
                                chosenTabId = projectCategory.getId();
                                LOG.debug("found: " + chosenCategory);
                            }
                        }
                        if (!foundChosenTab) {
                            LOG.debug("No tab to highlight");
                            chosenTab = "none";
                        }
                    }
                    request.setAttribute("chosenTab", chosenTab);
                    request.setAttribute("chosenCategory", chosenCategory);
                }

                // Go through the tabs and remove the ones the user shouldn't see, also check permission
                if (!userSession.isLoggedIn()) {
                    boolean allowed = true;
                    Iterator i = categoryList.iterator();
                    while (i.hasNext()) {
                        ProjectCategory projectCategory = (ProjectCategory) i.next();
                        if (projectCategory.getSensitive()) {
                            if (chosenTabId == projectCategory.getId()) {
                                allowed = false;
                            }
                            i.remove();
                        }
                    }
                    if (!allowed) {
                        // Redirect to the login, perhaps the page would exist then
                        String newUrl = URLFactory.createURL(prefs.getPrefs()) + "/login?redirectTo="
                                + requestedPath;
                        request.setAttribute("redirectTo", newUrl);
                        LOG.debug("redirectTo: " + newUrl);
                        request.removeAttribute(Constants.REQUEST_PAGE_LAYOUT);
                        return "Redirect301";
                    }
                }

                // Category drop-down for search
                HtmlSelect thisSelect = categoryList.getHtmlSelect();
                thisSelect.addItem(-1, prefs.get("TITLE"), 0);
                request.setAttribute(Constants.REQUEST_MENU_CATEGORY_LIST, thisSelect);
            }
        } catch (Exception e) {
            LOG.error("Global items error", e);
        } finally {
            this.freeConnection(context, db);
        }
    }

    // The user is not going to login, so verify login
    if (!s.startsWith("Login")) {
        if (userSession == null || ceSession == null) {
            // boot them off now
            LOG.debug("Security failed.");
            LoginBean failedSession = new LoginBean();
            failedSession.addError("actionError", "* Please login, your session has expired");
            failedSession.checkURL(request);
            request.setAttribute("LoginBean", failedSession);
            request.removeAttribute(Constants.REQUEST_PAGE_LAYOUT);
            return "SecurityCheck";
        } else {
            // The user should have a valid login now, so let them proceed
            LOG.debug("Security passed: " + userSession.getId() + " (" + userSession.getUsername() + ")");
        }
    }
    // Generate user's global items
    if (userSession != null && userSession.getId() > 0) {
        Connection db = null;
        try {
            db = getConnection(context, request);
            // TODO: Implement cache since every hit needs this list
            if (db != null) {
                // A map for global alerts
                ArrayList<String> alerts = new ArrayList<String>();
                request.setAttribute(Constants.REQUEST_GLOBAL_ALERTS, alerts);

                // Check to see if the application is configured...
                if (userSession.getAccessAdmin()) {
                    String url = URLFactory.createURL(prefs.getPrefs());
                    if (url == null) {
                        alerts.add(
                                "The application configuration requires that the URL properties are updated.  Store <strong>"
                                        + RequestUtils.getAbsoluteServerUrl(request) + "</strong>? <a href=\""
                                        + RequestUtils.getAbsoluteServerUrl(request)
                                        + "/AdminUsage.do?command=StoreURL\">Save Changes</a>");
                    } else if (!url.equals(RequestUtils.getAbsoluteServerUrl(request))) {
                        alerts.add("There is a configuration mismatch -- expected: <strong>"
                                + RequestUtils.getAbsoluteServerUrl(request)
                                + "</strong> but the configuration has <strong><a href=\"" + url + "\">" + url
                                + "</a></strong> <a href=\"" + RequestUtils.getAbsoluteServerUrl(request)
                                + "/AdminUsage.do?command=StoreURL\">Save Changes</a>");
                    }
                }

                // Check the # of invitations
                int invitationCount = InvitationList.queryCount(db, userSession.getId(),
                        userSession.getProfileProjectId());
                request.setAttribute(Constants.REQUEST_INVITATION_COUNT, String.valueOf(invitationCount));

                // Check the # of private messages
                int newMailCount = PrivateMessageList.queryUnreadCountForUser(db, userSession.getId());
                request.setAttribute(Constants.REQUEST_PRIVATE_MESSAGE_COUNT, String.valueOf(newMailCount));

                // NOTE: removed because not currently used
                // Check the number of what's new
                //          int whatsNewCount = ProjectUtils.queryWhatsNewCount(db, userSession.getId());
                //          request.setAttribute(Constants.REQUEST_WHATS_NEW_COUNT, String.valueOf(whatsNewCount));
                // Check the number of assignments
                //          int whatsAssignedCount = ProjectUtils.queryWhatsAssignedCount(db, userSession.getId());
                //          request.setAttribute(Constants.REQUEST_WHATS_ASSIGNED_COUNT, String.valueOf(whatsAssignedCount));
                //          int projectCount = ProjectUtils.queryMyProjectCount(db, userSession.getId());
                //          request.setAttribute(Constants.REQUEST_MY_PROJECT_COUNT, String.valueOf(projectCount));
            }
        } catch (Exception e) {
            LOG.error("User's global items error", e);
        } finally {
            this.freeConnection(context, db);
        }
    }
    return null;
}

From source file:edu.harvard.iq.dvn.core.web.subsetting.AnalysisPage.java

public String edaAction() {

    //        dbgLog.fine("selected tab(eda)="+getTabSet1().getSelected());
    // clear the error message around the EDA button if they exisit
    resetMsgEdaButton();/*from   w  w w .j  ava  2s  .c o  m*/

    if (checkEdaParameters()) {

        FacesContext cntxt = FacesContext.getCurrentInstance();

        HttpServletResponse res = (HttpServletResponse) cntxt.getExternalContext().getResponse();
        HttpServletRequest req = (HttpServletRequest) cntxt.getExternalContext().getRequest();

        dbgLog.info("***** within edaAction() *****");

        StudyFile sf = dataTable.getStudyFile();
        Long noRecords = dataTable.getRecordsPerCase();

        String dsbUrl = getDsbUrl();
        dbgLog.info("dsbUrl=" + dsbUrl);

        String serverPrefix = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort()
                + req.getContextPath();
        /*
        String serverPrefix = "http://dvn-alpha.hmdc.harvard.edu"
            + req.getContextPath();
        */
        dbgLog.info("serverPrefix" + serverPrefix);

        /*
         * "optnlst_a" => "A01|A02|A03",
         * "analysis" => "A01 A02",
         * "varbl" => "v1.3 v1.10 v1.13 v1.22 v1.40", 
         * "charVarNoSet" =>
         * "v1.10|v1.719",
         */

        Map<String, List<String>> mpl = new HashMap<String, List<String>>();

        Object[] vs = edaOptionSet.getSelectedValues();
        // analysis=[A01, A02]
        List<String> alst = new ArrayList<String>();

        for (int i = 0; i < vs.length; i++) {
            dbgLog.info("eda option[" + i + "]=" + vs[i]);
            alst.add((String) vs[i]);
        }

        mpl.put("analysis", alst);

        mpl.put("optnlst_a", Arrays.asList("A01|A02|A03"));

        // if there is a user-defined (recoded) variables
        /*
        if (recodedVarSet.size() > 0) {
            mpl.putAll(getRecodedVarParameters());
        }
        */
        //                dbgLog.fine("citation info to be sent:\n" + citation);
        //                mpl.put("OfflineCitation", Arrays.asList(citation));
        //                mpl.put("appSERVER", Arrays.asList(req.getServerName() + ":"
        //                    + req.getServerPort() + req.getContextPath()));

        mpl.put("studytitle", Arrays.asList(getStudyTitle()));
        dbgLog.info("studyId from get method=" + getStudyId().toString());
        mpl.put("studyno", Arrays.asList(getStudyId().toString()));
        mpl.put("studyURL", Arrays.asList(studyURL));
        mpl.put("browserType", Arrays.asList(browserType));

        mpl.put("recodedVarIdSet", getRecodedVarIdSet());
        mpl.put("recodedVarNameSet", getRecodedVarNameSet());
        mpl.put("recodedVarLabelSet", getRecodedVarLabelSet());
        mpl.put("recodedVarTypeSet", getRecodedVariableType());
        mpl.put("recodedVarBaseTypeSet", getBaseVariableTypeForRecodedVariable());

        mpl.put("baseVarIdSet", getBaseVarIdSetFromRecodedVarIdSet());
        mpl.put("baseVarNameSet", getBaseVarNameSetFromRecodedVarIdSet());

        mpl.put("requestType", Arrays.asList("EDA"));

        // -----------------------------------------------------
        // New processing route
        // 
        // Step 0. Locate the data file and its attributes

        String fileId = sf.getId().toString();
        //VDC vdc = vdcService.getVDCFromRequest(req);

        String fileloc = sf.getFileSystemLocation();
        String tabflnm = sf.getFileName();
        boolean sbstOK = sf.isSubsettable();
        String flct = sf.getFileType();

        dbgLog.info("location=" + fileloc);
        dbgLog.info("filename=" + tabflnm);
        dbgLog.info("subsettable=" + sbstOK);
        dbgLog.info("filetype=" + flct);

        DvnRJobRequest sro = null;

        List<File> zipFileList = new ArrayList();

        // the data file for downloading/statistical analyses must be subset-ready
        // local (relative to the application) file case 
        // note: a typical remote case is: US Census Bureau
        File tmpsbfl = null;
        if (sbstOK) {

            try {
                // this temp file will store the requested columns:
                tmpsbfl = File.createTempFile("tempsubsetfile.", ".tab");
                deleteTempFileList.add(tmpsbfl);

                // to produce this file, we'll either open the stream
                // and run our local subsetting code on it, or request
                // the subsetting to be performed natively by the access
                // driver, if it supports the functionality:

                // check whether a source file is tab-delimited or not

                boolean fieldcut = true;
                if ((noRecords != null) && (noRecords >= 1)) {
                    fieldcut = false;
                }

                DataAccessRequest daReq = new DataAccessRequest();
                daReq.setParameter("noVarHeader", "1");

                DataAccessObject accessObject = DataAccess.createDataAccessObject(sf, daReq);

                if (accessObject.isSubsetSupported()) {
                    dbgLog.info("Using NATIVE subset functionality of the repository.");
                    daReq.setParameter("vars", getVariableNamesForSubset());

                    accessObject.open();

                    InputStream inSubset = accessObject.getInputStream();
                    OutputStream outSubset = new BufferedOutputStream(
                            new FileOutputStream(tmpsbfl.getAbsolutePath()));

                    int bufsize = 8192;
                    byte[] subsetDataBuffer = new byte[bufsize];
                    while ((bufsize = inSubset.read(subsetDataBuffer)) != -1) {
                        outSubset.write(subsetDataBuffer, 0, bufsize);
                    }

                    inSubset.close();
                    outSubset.close();

                    // TODO: catch exceptions; reset the state of the page
                    // if anything went wrong. See the fixed-field section
                    // below for an example.

                } else {
                    accessObject.open();

                    if (fieldcut) {
                        // Cutting requested fields of data from a TAB-delimited stream:

                        Set<Integer> fields = getFieldNumbersForSubsetting();
                        dbgLog.info("subsetting fields=" + fields);

                        // Create an instance of DvnJavaFieldCutter
                        FieldCutter fc = new DvnJavaFieldCutter();

                        // Executes the subsetting request
                        fc.subsetFile(accessObject.getInputStream(), tmpsbfl.getAbsolutePath(), fields,
                                dataTable.getCaseQuantity(), "\t");

                        // TODO: catch exceptions; reset the state of the page
                        // if anything went wrong. See the fixed-field section
                        // below for an example.

                    } else {
                        // Cutting requested columns of data from a fixed-field stream:

                        Map<Long, List<List<Integer>>> varMetaSet = getSubsettingMetaData(noRecords);
                        DvnNewJavaFieldCutter fc = new DvnNewJavaFieldCutter(varMetaSet);

                        try {
                            //fc.cutColumns(new File(cutOp1), noRecords.intValue(), 0, "\t", tmpsbfl.getAbsolutePath());
                            fc.cutColumns(accessObject.getInputStream(), noRecords.intValue(), 0, "\t",
                                    tmpsbfl.getAbsolutePath());
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();

                            msgDwnldButton.setValue("* could not generate subset due to an IO problem");
                            msgDwnldButton.setVisible(true);
                            dbgLog.warning("exiting dwnldAction() due to an IO problem ");
                            getVDCRequestBean().setSelectedTab("tabDwnld");
                            dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                            return "";

                        } catch (RuntimeException re) {
                            re.printStackTrace();

                            msgDwnldButton.setValue("* could not generate subset due to an runtime error");
                            msgDwnldButton.setVisible(true);
                            dbgLog.warning("exiting dwnldAction() due to an runtime error");
                            getVDCRequestBean().setSelectedTab("tabDwnld");
                            dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                            return "";

                        }

                    }
                }

                // Checks the resulting subset file 
                if (tmpsbfl.exists()) {
                    Long subsetFileSize = tmpsbfl.length();
                    dbgLog.info("subsettFile:Length=" + subsetFileSize);
                    dbgLog.info("tmpsb file name=" + tmpsbfl.getAbsolutePath());

                    if (subsetFileSize > 0) {
                        mpl.put("subsetFileName", Arrays.asList(tmpsbfl.getAbsolutePath()));
                        mpl.put("subsetDataFileName", Arrays.asList(tmpsbfl.getName()));
                    } else {
                        // subset file exists but it is empty

                        msgEdaButton.setValue("* an subset file is empty");
                        msgEdaButton.setVisible(true);
                        dbgLog.warning(
                                "exiting edaAction() due to a subsetting error:" + "a subset file is empty");
                        getVDCRequestBean().setSelectedTab("tabEda");

                        return "";

                    }
                } else {
                    // subset file was not created
                    msgEdaButton.setValue("* a subset file was not created");
                    msgEdaButton.setVisible(true);
                    dbgLog.warning(
                            "exiting edaAction() due to a subsetting error:" + "a subset file was not created");
                    getVDCRequestBean().setSelectedTab("tabEda");

                    return "";

                }

                // Step 3. Organizes parameters/metadata to be sent to the implemented
                // data-analysis-service class

                //Map<String, Map<String, String>> vls = getValueTableForRequestedVariables(getDataVariableForRequest());
                Map<String, Map<String, String>> vls = getValueTablesForAllRequestedVariables();

                sro = new DvnRJobRequest(getDataVariableForRequest(), mpl, vls, recodeSchema);

                //                    dbgLog.fine("sro dump:\n"+ToStringBuilder.reflectionToString(sro, ToStringStyle.MULTI_LINE_STYLE));

                // Step 4. Creates an instance of the the implemented 
                // data-analysis-service class 

                DvnRDataAnalysisServiceImpl das = new DvnRDataAnalysisServiceImpl();

                // Executes a request of downloading or data analysis and 
                // capture result info as a Map <String, String>

                resultInfo = das.execute(sro);

                // Step 5. Checks the DSB-exit-status code
                if (resultInfo.get("RexecError").equals("true")) {
                    msgEdaButton.setValue("* The Request failed due to an R-runtime error");
                    msgEdaButton.setVisible(true);
                    dbgLog.info("exiting edaAction() due to an R-runtime error");
                    getVDCRequestBean().setSelectedTab("tabEda");

                    return "";
                } else {
                    if (recodeSchema.size() > 0) {
                        resultInfo.put("subsettingCriteria", sro.getSubsetConditionsForCitation());
                    } else {
                        resultInfo.put("subsettingCriteria", "");
                    }
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();

                msgEdaButton.setValue("* file URL is malformed");
                msgEdaButton.setVisible(true);
                dbgLog.warning("exiting edaAction() due to a URL problem ");
                getVDCRequestBean().setSelectedTab("tabEda");

                return "";

            } catch (IOException e) {
                // this may occur if the dataverse is not released
                // the file exists, but it is not accessible 
                e.printStackTrace();

                msgEdaButton.setValue("* an IO problem occurred");
                msgEdaButton.setVisible(true);
                dbgLog.warning("exiting edaAction() due to an IO problem ");
                getVDCRequestBean().setSelectedTab("tabEda");

                return "";
            }

            // end of the subset-OK case
        } else {
            // not subsettable data file
            msgEdaButton.setValue("* this data file is not subsettable file");
            msgEdaButton.setVisible(true);
            dbgLog.warning("exiting edaAction(): the data file is not subsettable ");
            getVDCRequestBean().setSelectedTab("tabEda");

            return "";

        } // end:subsetNotOKcaseF

        // final processing steps for all successful cases

        resultInfo.put("offlineCitation", getCitation());
        resultInfo.put("studyTitle", getStudyTitle());
        resultInfo.put("studyNo", getStudyId().toString());
        resultInfo.put("fileName", fileName);
        resultInfo.put("dtId", dtId.toString());
        if (versionNumber != null) {
            resultInfo.put("versionNumber", versionNumber.toString());
        }
        resultInfo.put("studyURL", studyURL);
        resultInfo.put("R_min_verion_no", resultInfo.get("Rversion").substring(2));
        resultInfo.put("dataverse_version_no", dvnVersionNumber);

        dbgLog.info("RwrkspFileName=" + resultInfo.get("wrkspFileName"));

        // writing necessary files
        try {

            // rename the subsetting file
            File tmpsbflnew = File.createTempFile(SUBSET_FILENAME_PREFIX + resultInfo.get("PID") + ".", ".tab");
            deleteTempFileList.add(tmpsbflnew);
            InputStream inb = new BufferedInputStream(new FileInputStream(tmpsbfl));
            OutputStream outb = new BufferedOutputStream(new FileOutputStream(tmpsbflnew));

            int bufsize;
            byte[] bffr = new byte[8192];
            while ((bufsize = inb.read(bffr)) != -1) {
                outb.write(bffr, 0, bufsize);
            }
            inb.close();
            outb.close();

            String rhistNew = StringUtils.replace(resultInfo.get("RCommandHistory"), tmpsbfl.getName(),
                    tmpsbflnew.getName());

            //zipFileList.add(tmpsbflnew);

            // (1) write a citation file 
            //                String citationFilePrefix = "citationFile_"+ resultInfo.get("PID") + "_";
            //                File tmpcfl = File.createTempFile(citationFilePrefix, ".txt");
            //
            //                zipFileList.add(tmpcfl);
            //                deleteTempFileList.add(tmpcfl);
            //
            //                DvnCitationFileWriter dcfw = new DvnCitationFileWriter(resultInfo);
            //
            //                String fmpcflFullname = tmpcfl.getAbsolutePath();
            //                String fmpcflname = tmpcfl.getName();
            //                dcfw.write(tmpcfl);

            // (2) R command file
            String rhistoryFilePrefix = R_COMMAND_FILE_PREFIX + resultInfo.get("PID") + ".R";
            File tmpRhfl = new File(TEMP_DIR, rhistoryFilePrefix);

            zipFileList.add(tmpRhfl);
            deleteTempFileList.add(tmpRhfl);
            resultInfo.put("dvn_R_helper_file", "dvn_helper.R");
            DvnReplicationCodeFileWriter rcfw = new DvnReplicationCodeFileWriter(resultInfo);
            rcfw.writeEdaCode(tmpRhfl);

            // (3)RData Replication file
            String wrkspFileName = resultInfo.get("wrkspFileName");
            dbgLog.info("wrkspFileName=" + wrkspFileName);

            File RwrkspFileName = new File(wrkspFileName);
            if (RwrkspFileName.exists()) {
                dbgLog.info("RwrkspFileName:length=" + RwrkspFileName.length());

                zipFileList.add(RwrkspFileName);

            } else {
                dbgLog.info("RwrkspFileName does not exist");
                //msgEdaButton.setValue("* The workspace file is not available");
                //msgEdaButton.setVisible(true);
                dbgLog.warning("edaAction(): R workspace file was not transferred");
                //getVDCRequestBean().setSelectedTab("tabEda");

                //return "failure";
            }
            deleteTempFileList.add(RwrkspFileName);

            //                // vdc_startup.R file
            //                String vdc_startupFileName = resultInfo.get("vdc_startupFileName");
            //                dbgLog.fine("vdc_startupFileName="+vdc_startupFileName);
            //                File vdcstrtFileName = new File(vdc_startupFileName);
            //                if (vdcstrtFileName.exists()){
            //                    dbgLog.fine("vdcstrtFileName:length="+vdcstrtFileName.length());
            //                    zipFileList.add(vdcstrtFileName);
            //                } else {
            //                    dbgLog.fine("vdcstrtFileName does not exist");
            //                    //msgEdaButton.setValue("* vdc_startup.R is not available");
            //                    //msgEdaButton.setVisible(true);
            //                    dbgLog.warning("edaAction(): vdc_startup.R was not transferred");
            //                    //getVDCRequestBean().setSelectedTab("tabEda");
            //
            //                    //return "failure";
            //                }
            //                deleteTempFileList.add(vdcstrtFileName);
            // add replication readme file
            // (4) readme file
            // zipFileList.add(REP_README_FILE);
            String readMeFileName = REP_README_FILE_PREFIX + resultInfo.get("PID") + ".txt";
            File readMeFile = new File(TEMP_DIR, readMeFileName);

            DvnReplicationREADMEFileWriter rw = new DvnReplicationREADMEFileWriter(resultInfo);
            rw.writeREADMEfile(readMeFile);
            //zipFileList.add(REP_README_FILE);
            zipFileList.add(readMeFile);
            deleteTempFileList.add(readMeFile);

            // (5) css file
            zipFileList.add(DVN_R2HTML_CSS_FILE);
            // (6) dvn_R_helper
            zipFileList.add(DVN_R_HELPER_FILE);
            // zip the following files as a replication-pack
            //
            // local     local        local      remote
            // citation, tab-file,   R history,  wrksp

            for (File f : zipFileList) {
                dbgLog.fine("path=" + f.getAbsolutePath() + "\tname=" + f.getName());
            }

            // zipping all required files
            try {
                String zipFilePrefix = "zipFile_" + resultInfo.get("PID") + ".zip";
                File zipFile = new File(TEMP_DIR, zipFilePrefix);

                //res.setContentType("application/zip");
                String zfname = zipFile.getAbsolutePath();
                //res.setHeader("content-disposition", "attachment; filename=" + zfname);

                OutputStream zfout = new FileOutputStream(zipFile);
                //zipFiles(res.getOutputStream(), zipFileList);
                zipFiles(zfout, zipFileList);
                deleteTempFileList.add(zipFile);
                if (zipFile.exists()) {
                    Long zipFileSize = zipFile.length();
                    dbgLog.info("zip file:length=" + zipFileSize);
                    dbgLog.info("zip file:name=" + zipFile.getAbsolutePath());
                    if (zipFileSize > 0) {
                        resultInfo.put("replicationZipFile", zfname);
                        resultInfo.put("replicationZipFileName", zipFile.getName());
                    } else {
                        dbgLog.info("zip file is empty");
                    }
                } else {
                    dbgLog.info("zip file was not saved");
                }

                resultInfo.remove("RCommandHistory");

                // put resultInfo into the session object

                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("resultInfo",
                        resultInfo);

                dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);

                dbgLog.info("***** within edaAction(): succcessfully ends here *****");

                return "/subsetting/AnalysisResultsPage?faces-redirect=true" + getContextSuffix();
                //return "success";

            } catch (IOException e) {
                // file-access problem, etc.
                e.printStackTrace();
                dbgLog.info("zipping IO exception");
                msgEdaButton.setValue("* an IO problem occurred during zipping replication files");
                msgEdaButton.setVisible(true);
                dbgLog.warning("exiting edaAction() due to an zipping IO problem ");
                //getVDCRequestBean().setSelectedTab("tabEda");
                dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                return "/subsetting/AnalysisResultsPage?faces-redirect=true" + getContextSuffix();
                //return "success";
            }
            // end of zipping step

        } catch (IOException e) {
            // io errors caught during writing files
            e.printStackTrace();

            msgEdaButton.setValue("* an IO problem occurred");
            msgEdaButton.setVisible(true);
            dbgLog.warning("exiting edaAction() due to an IO problem ");
            getVDCRequestBean().setSelectedTab("tabEda");

            return "";
        }

        // end of CheckParameters: OK case
    } else {
        // parameters are not complete: show error message;
        msgEdaButton.setValue("* Select at least one option");
        msgEdaButton.setVisible(true);
        dbgLog.warning("exiting edaAction(): selection is incomplete");
        getVDCRequestBean().setSelectedTab("tabEda");

        return "";
    }

}

From source file:edu.harvard.iq.dvn.core.web.subsetting.AnalysisPage.java

public String dwnldAction() {
    dbgLog.fine("***** within dwnldAction() *****");

    resetMsgDwnldButton();//from ww  w  .  j  a  v a  2s  . c  o m

    if (checkDwnldParameters()) {

        FacesContext cntxt = FacesContext.getCurrentInstance();

        HttpServletResponse res = (HttpServletResponse) cntxt.getExternalContext().getResponse();

        HttpServletRequest req = (HttpServletRequest) cntxt.getExternalContext().getRequest();

        StudyFile sf = dataTable.getStudyFile();

        Long noRecords = dataTable.getRecordsPerCase();

        String dsbUrl = getDsbUrl();
        dbgLog.fine("dsbUrl=" + dsbUrl);

        String serverPrefix = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort()
                + req.getContextPath();

        dbgLog.fine("serverPrefix" + serverPrefix);

        Map<String, List<String>> mpl = new HashMap<String, List<String>>();

        // File inFile = new File(sf.getFileSystemLocation());
        // File origFile = new File(inFile.getParent(), "_" + sf.getFileSystemName());

        String formatType = (String) dwnldFileTypeSet.getValue();
        dbgLog.fine("file type from the binding=" + formatType);
        if (formatType == null) {
            formatType = dwnldFileTypeSelected;
            dbgLog.fine("file type from the value=" + dwnldFileTypeSelected);
        }
        mpl.put("dtdwnld", Arrays.asList(formatType));

        dbgLog.fine("citation info to be sent:\n" + getCitation());

        mpl.put("studytitle", Arrays.asList(getStudyTitle()));
        dbgLog.fine("studyId=" + getStudyId().toString());
        mpl.put("studyno", Arrays.asList(getStudyId().toString()));
        mpl.put("studyURL", Arrays.asList(studyURL));

        mpl.put("", Arrays.asList(""));
        mpl.put("browserType", Arrays.asList(browserType));

        mpl.put("recodedVarIdSet", getRecodedVarIdSet());
        mpl.put("recodedVarNameSet", getRecodedVarNameSet());
        mpl.put("recodedVarLabelSet", getRecodedVarLabelSet());
        mpl.put("recodedVarTypeSet", getRecodedVariableType());
        mpl.put("recodedVarBaseTypeSet", getBaseVariableTypeForRecodedVariable());

        mpl.put("baseVarIdSet", getBaseVarIdSetFromRecodedVarIdSet());
        mpl.put("baseVarNameSet", getBaseVarNameSetFromRecodedVarIdSet());

        mpl.put("requestType", Arrays.asList("Download"));

        // Added by Matt Owen to find a way to sneak in Metadata that is lost between Rdata -> Tab 
        mpl.put("originalFile", Arrays.asList(getOriginalFileSystemLocation().getAbsolutePath()));

        // -----------------------------------------------------
        // Processing route, step by step:
        // 
        // Step 0. Locate the data file and its attributes

        String fileId = sf.getId().toString();

        String fileloc = sf.getFileSystemLocation();
        String tabflnm = sf.getFileName();
        boolean sbstOK = sf.isSubsettable();
        String flct = sf.getFileType();

        // Output debug statements
        dbgLog.info("location=" + fileloc);
        dbgLog.info("filename=" + tabflnm);
        dbgLog.info("subsettable=" + sbstOK);
        dbgLog.info("filetype=" + flct);
        dbgLog.info("studyUrl = " + studyURL);
        dbgLog.info("original file asbolute path = " + getOriginalFileSystemLocation().getAbsolutePath());

        // D
        DvnRJobRequest sro = null;

        List<File> zipFileList = new ArrayList();

        File tmpsbfl = null;

        if (sbstOK) {

            try {

                // this temp file will store the requested column(s):
                tmpsbfl = File.createTempFile("tempsubsetfile.", ".tab");
                deleteTempFileList.add(tmpsbfl);

                // to produce this file, we'll either open the stream
                // and run our local subsetting code on it, or request
                // the subsetting to be performed natively by the access
                // driver, if it supports the functionality:

                // check whether a source file is tab-delimited or not

                boolean fieldcut = true;
                if ((noRecords != null) && (noRecords >= 1)) {
                    fieldcut = false;
                }

                DataAccessRequest daReq = new DataAccessRequest();
                daReq.setParameter("noVarHeader", "1");

                DataAccessObject accessObject = DataAccess.createDataAccessObject(sf, daReq);

                if (accessObject.isSubsetSupported()) {
                    dbgLog.fine("Using NATIVE subset functionality of the repository.");
                    daReq.setParameter("vars", getVariableNamesForSubset());

                    accessObject.open();

                    InputStream inSubset = accessObject.getInputStream();
                    OutputStream outSubset = new BufferedOutputStream(
                            new FileOutputStream(tmpsbfl.getAbsolutePath()));

                    int bufsize = 8192;
                    byte[] subsetDataBuffer = new byte[bufsize];
                    while ((bufsize = inSubset.read(subsetDataBuffer)) != -1) {
                        outSubset.write(subsetDataBuffer, 0, bufsize);
                    }

                    inSubset.close();
                    outSubset.close();

                    // TODO: catch exceptions; reset the state of the page
                    // if anything went wrong. See the fixed-field section
                    // below for an example.

                } else {
                    accessObject.open();

                    if (fieldcut) {
                        // Cutting requested fields of data from a TAB-delimited stream:

                        Set<Integer> fields = getFieldNumbersForSubsetting();
                        dbgLog.fine("subsetting fields=" + fields);

                        // Create an instance of DvnJavaFieldCutter
                        FieldCutter fc = new DvnJavaFieldCutter();

                        // Executes the subsetting request
                        fc.subsetFile(accessObject.getInputStream(), tmpsbfl.getAbsolutePath(), fields,
                                dataTable.getCaseQuantity(), "\t");

                        // TODO: catch exceptions; reset the state of the page
                        // if anything went wrong. See the fixed-field section
                        // below for an example.

                    } else {
                        // Cutting requested columns of data from a fixed-field stream:

                        Map<Long, List<List<Integer>>> varMetaSet = getSubsettingMetaData(noRecords);
                        DvnNewJavaFieldCutter fc = new DvnNewJavaFieldCutter(varMetaSet);

                        try {
                            //fc.cutColumns(new File(cutOp1), noRecords.intValue(), 0, "\t", tmpsbfl.getAbsolutePath());
                            fc.cutColumns(accessObject.getInputStream(), noRecords.intValue(), 0, "\t",
                                    tmpsbfl.getAbsolutePath());
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();

                            msgDwnldButton.setValue("* could not generate subset due to an IO problem");
                            msgDwnldButton.setVisible(true);
                            dbgLog.warning("exiting dwnldAction() due to an IO problem ");
                            getVDCRequestBean().setSelectedTab("tabDwnld");
                            dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                            return "";

                        } catch (RuntimeException re) {
                            re.printStackTrace();

                            msgDwnldButton.setValue("* could not generate subset due to an runtime error");
                            msgDwnldButton.setVisible(true);
                            dbgLog.warning("exiting dwnldAction() due to an runtime error");
                            getVDCRequestBean().setSelectedTab("tabDwnld");
                            dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                            return "";

                        }

                    }
                }

                // Checks the resulting subset file:

                if (tmpsbfl.exists()) {
                    Long subsetFileSize = tmpsbfl.length();
                    dbgLog.fine("subset file:Length=" + subsetFileSize);
                    dbgLog.fine("subset file:name=" + tmpsbfl.getAbsolutePath());

                    if (subsetFileSize > 0) {
                        mpl.put("subsetFileName", Arrays.asList(tmpsbfl.getAbsolutePath()));
                        mpl.put("subsetDataFileName", Arrays.asList(tmpsbfl.getName()));
                    } else {
                        // subset file exists but it is empty

                        msgDwnldButton.setValue("* an subset file is empty");
                        msgDwnldButton.setVisible(true);
                        dbgLog.warning(
                                "exiting dwnldAction() due to a subsetting error:" + "a subset file is empty");
                        getVDCRequestBean().setSelectedTab("tabDwnld");
                        dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                        return "";

                    }
                } else {
                    // subset file was not created
                    msgDwnldButton.setValue("* a subset file was not created");
                    msgDwnldButton.setVisible(true);
                    dbgLog.warning("exiting dwnldAction() due to a subsetting error:"
                            + "a subset file was not created");
                    getVDCRequestBean().setSelectedTab("tabDwnld");
                    dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                    return "";

                }

                // If we've made it this far, we can increment the number of
                // downloads for the study file:

                VDC vdc = vdcService.getVDCFromRequest(req);
                GuestBookResponse guestbookResponse = (GuestBookResponse) getVDCSessionBean()
                        .getGuestbookResponseMap().get("guestBookResponse_" + sf.getStudy().getId());

                if (guestbookResponse == null) {
                    //need to set up dummy network response
                    guestbookResponse = guestBookResponseServiceBean.initNetworkGuestBookResponse(sf.getStudy(),
                            sf, getVDCSessionBean().getLoginBean());
                }
                guestbookResponse.setStudyVersion(sf.getStudy().getStudyVersionByNumber(versionNumber));
                String jsessionId = null;
                Cookie cookies[] = req.getCookies();

                for (int i = 0; i < cookies.length; i++) {
                    if ("JSESSIONID".equals(cookies[i].getName())) {
                        jsessionId = cookies[i].getValue();
                    }
                }

                if (jsessionId == null || "".equals(jsessionId)) {
                    String[] stringArray = getVDCSessionBean().toString().split("@");
                    jsessionId = stringArray[1];
                }

                guestbookResponse.setSessionId(jsessionId);

                String friendlyFormatName = "";
                String formatRequestedMimeType = "";

                if (formatType != null && !"".equals(formatType)) {
                    if (formatType.equals("D00")) {
                        formatRequestedMimeType = "text/tab-separated-values"; // tabular
                    } else if (formatType.equals("D01")) {
                        formatRequestedMimeType = "text/tab-separated-values"; // fixed-field
                    } else {
                        for (DataFileFormatType type : studyService.getDataFileFormatTypes()) {
                            if (formatType.equals(type.getValue())) {
                                formatRequestedMimeType = type.getMimeType();
                            }
                        }
                    }
                }

                if (formatRequestedMimeType == null || "".equals(formatRequestedMimeType)) {
                    formatRequestedMimeType = "application/x-unknown";
                }

                friendlyFormatName = FileUtil.getUserFriendlyTypeForMime(formatRequestedMimeType);
                guestbookResponse.setDownloadtype("Subsetting - " + friendlyFormatName);

                if (vdc != null) {
                    studyService.incrementNumberOfDownloads(sf.getId(), vdc.getId(),
                            (GuestBookResponse) guestbookResponse);
                } else {
                    studyService.incrementNumberOfDownloads(sf.getId(), (Long) null,
                            (GuestBookResponse) guestbookResponse);
                }

                // Step 3. Organizes parameters/metadata to be sent to the implemented
                // data-analysis-service class

                // skip the Rserve call completely (for plain tab file format, with no recoding)! -- L.A.
                if (!formatType.equals("D01") || (recodeSchema.size() > 0)) {

                    Map<String, Map<String, String>> vls = getValueTablesForAllRequestedVariables();

                    // New (as of 3.6): support for ordered categorical variables 
                    // (ingested from R ordered factors). 
                    // Note that this is only being added here, i.e., to the 
                    // download-and-save part; if/when we make the analysis 
                    // and statistics utilize/handle these ordered categories 
                    // in some special way, we'll need to add the actual 
                    // ordered values to the SRO objects there as well. -- L.A. 

                    Map<String, List<String>> categoryOrders = getCategoryValueOrdersForAllRequestedVariables();

                    if (categoryOrders != null) {
                        sro = new DvnRJobRequest(getDataVariableForRequest(), mpl, vls, recodeSchema,
                                categoryOrders, null);
                    } else {
                        sro = new DvnRJobRequest(getDataVariableForRequest(), mpl, vls, recodeSchema);
                    }

                    /*
                     * Add the recoded -> base variable name map; (new as of v3.6;)
                     * TODO: (?) do the same for the other action requests. 
                     *          -- L.A.
                     */
                    sro.setRecodedToBaseVar(getRecodedVarToBaseVarName());

                    // dbgLog.fine("sro dump:\n"+ToStringBuilder.reflectionToString(sro, ToStringStyle.MULTI_LINE_STYLE));

                    // Step 4. Creates an instance of the the implemented
                    // data-analysis-service class

                    DvnRDataAnalysisServiceImpl das = new DvnRDataAnalysisServiceImpl();

                    // Executes a download or data analysis request and
                    // stores the results in a Map <String, String>

                    resultInfo = das.execute(sro);

                    // Step 5. Check the exit status of the R process:

                    if (resultInfo.get("RexecError").equals("true")) {

                        msgDwnldButton.setValue("* The Request failed due to an R-runtime error");
                        msgDwnldButton.setVisible(true);
                        dbgLog.fine("exiting dwnldAction() due to an R-runtime error");
                        getVDCRequestBean().setSelectedTab("tabDwnld");
                        dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                        return "";
                    }
                }

                if (recodeSchema.size() > 0) {
                    resultInfo.put("subsettingCriteria", sro.getSubsetConditionsForCitation());
                } else {
                    resultInfo.put("subsettingCriteria", "variables: " + getVariableNamesForSubset());
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();

                msgDwnldButton.setValue("* file URL is malformed");
                msgDwnldButton.setVisible(true);
                dbgLog.warning("exiting dwnldAction() due to a URL problem ");
                getVDCRequestBean().setSelectedTab("tabDwnld");

                return "";

            } catch (IOException e) {
                // this may occur if the dataverse is not released,
                // or if the file exists, but it is not accessible, etc.
                e.printStackTrace();

                msgDwnldButton.setValue("* an IO problem occurred");
                msgDwnldButton.setVisible(true);
                dbgLog.warning("exiting dwnldAction() due to an IO problem ");
                getVDCRequestBean().setSelectedTab("tabDwnld");

                return "";
            }

            // end of subset-OK case
        } else {
            // not subsettable data file
            msgDwnldButton.setValue("* this data file is not subsettable file");
            msgDwnldButton.setVisible(true);
            dbgLog.warning("exiting dwnldAction(): the data file is not subsettable ");
            getVDCRequestBean().setSelectedTab("tabDwnld");
            dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
            return "";

        } // end:subsetNotOKcase

        // final processing steps for all successful cases
        // add study-metadata to the resultInfo map

        if (formatType.equals("D01") && !(recodeSchema.size() > 0)) {
            resultInfo.put("wbDataFileName", tmpsbfl.getAbsolutePath());
            // Fields that would normally be populated by R:

            resultInfo.put("PID", "N/A");
            resultInfo.put("R_min_verion_no", "N/A");
            resultInfo.put("dsbHost", "N/A");
            Date now = new Date();
            resultInfo.put("RexecDate", now.toString());
        } else {
            resultInfo.put("R_min_verion_no", resultInfo.get("Rversion").substring(2));
        }

        resultInfo.put("offlineCitation", getCitation());
        resultInfo.put("studyTitle", getStudyTitle());
        resultInfo.put("studyNo", getStudyId().toString());
        resultInfo.put("dtId", dtId.toString());
        if (versionNumber != null) {
            resultInfo.put("versionNumber", versionNumber.toString());
        }
        resultInfo.put("studyURL", studyURL);
        resultInfo.put("dataverse_version_no", dvnVersionNumber);

        resultInfo.put("option", "subset");
        resultInfo.put("variableList", getVariableNamesForSubset());

        // calculate UNF (locally, on the application side):

        List<DataVariable> subsetVariableList = getDataVariableForRequest();

        String subsetUNFvalue = "[NOT CALCULATED]";

        String[] unfValues = new String[subsetVariableList.size()];

        for (int i = 0; i < subsetVariableList.size(); i++) {
            unfValues[i] = subsetVariableList.get(i).getUnf();
        }

        dbgLog.fine("unf set:\n" + Arrays.deepToString(unfValues));

        try {
            subsetUNFvalue = UNF5Util.calculateUNF(unfValues);

        } catch (NumberFormatException ex) {
            // if anything went wrong during the UNF calculation, it's not
            // a fatal condition; we'll just be displaying "not calculated"
            // instead of the UNF in the final README file.

            dbgLog.fine("error while trying to calculate subset UNF: Number Format Exception.");
            ex.printStackTrace();
        } catch (IOException ex) {
            dbgLog.fine("error while trying to calculate subset UNF: IO Exception.");
            ex.printStackTrace();
        }

        resultInfo.put("fileUNF", subsetUNFvalue);

        // writing necessary files:

        try {

            if (formatType.equals("D01") && !(recodeSchema.size() > 0)) {
                // (2) tab-delimited-format-only step:
                //
                // In the final zip file we package the subset file
                // and a replication README file (also contains citation). 
                // We also *used to* include the SAS, SPSS and R control
                // files created by R. We are not doing this anymore, but 
                // I left the code commented-out below. 
                //                  -- L.A. Jan. 2012
                //
                // We are also adding the variable header to the file here.

                /* SKIP CODE FILES -- L.A.
                String codeFileSas = "codeFile_sas_" + resultInfo.get("PID") + ".sas";
                File tmpCCsasfl = new File(TEMP_DIR, codeFileSas);
                        
                deleteTempFileList.add(tmpCCsasfl);
                zipFileList.add(tmpCCsasfl);
                        
                String codeFileSpss = "codeFile_spss_" + resultInfo.get("PID") + ".sps";
                File tmpCCspsfl = new File(TEMP_DIR, codeFileSpss);
                        
                deleteTempFileList.add(tmpCCspsfl);
                zipFileList.add(tmpCCspsfl);
                        
                String codeFileStata = "codeFile_stata_" + resultInfo.get("PID") + ".do";
                File tmpCCdofl  = new File(TEMP_DIR, codeFileStata);
                        
                deleteTempFileList.add(tmpCCdofl);
                zipFileList.add(tmpCCdofl);
                        
                StatisticalCodeFileWriter scfw = new StatisticalCodeFileWriter(sro);
                scfw.write(tmpCCsasfl, tmpCCspsfl, tmpCCdofl);
                 */

                // add the subset file:

                File tmpsbflnew = File.createTempFile("tempsubsetfile_new.", ".tab");
                deleteTempFileList.add(tmpsbflnew);

                InputStream inb = new BufferedInputStream(new FileInputStream(tmpsbfl));
                OutputStream outb = new BufferedOutputStream(new FileOutputStream(tmpsbflnew));

                String varHeaderLine = getVariableHeaderForSubset();
                // Add the variable header to the subset file:
                byte[] varHeaderBuffer = null;
                varHeaderBuffer = varHeaderLine.getBytes();
                outb.write(varHeaderBuffer);
                outb.flush();

                int bufsize;
                byte[] bffr = new byte[8192];
                while ((bufsize = inb.read(bffr)) != -1) {
                    outb.write(bffr, 0, bufsize);
                }
                inb.close();
                outb.close();

                dbgLog.fine("adding tab file: " + tmpsbflnew.getName());
                zipFileList.add(tmpsbflnew);

            } else {

                // (2)The format-converted subset data file
                String wbDataFileName = resultInfo.get("wbDataFileName");
                dbgLog.fine("wbDataFileName=" + wbDataFileName);

                File wbSubsetDataFile = new File(wbDataFileName);
                if (wbSubsetDataFile.exists()) {
                    dbgLog.fine("wbSubsetDataFile:length=" + wbSubsetDataFile.length());
                    deleteTempFileList.add(wbSubsetDataFile);
                    zipFileList.add(wbSubsetDataFile);
                } else {
                    // the data file was not created
                    dbgLog.fine("wbSubsetDataFile does not exist");

                    msgDwnldButton.setValue("* The requested data file is not available");
                    msgDwnldButton.setVisible(true);
                    dbgLog.warning("exiting dwnldAction(): data file was not transferred");
                    getVDCRequestBean().setSelectedTab("tabDwnld");
                    dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                    return "";
                }
            }

            // Create README file:

            String readMeFileName = null;

            if (resultInfo.get("PID") != null && !resultInfo.get("PID").equals("N/A")) {
                readMeFileName = REP_README_FILE_PREFIX + resultInfo.get("PID") + ".txt";
            } else {
                readMeFileName = REP_README_FILE_PREFIX + fileId + ".txt";
            }

            File readMeFile = new File(TEMP_DIR, readMeFileName);

            DvnReplicationREADMEFileWriter rw = new DvnReplicationREADMEFileWriter(resultInfo);
            rw.writeREADMEfile(readMeFile, true);

            zipFileList.add(readMeFile);
            deleteTempFileList.add(readMeFile);

            for (File f : zipFileList) {
                dbgLog.fine("file to zip: path=" + f.getAbsolutePath() + "\tname=" + f.getName());
            }

            // We can now zip all the required files"
            try {
                String zipFilePrefix = null;

                if (resultInfo.get("PID") != null && !resultInfo.get("PID").equals("N/A")) {
                    zipFilePrefix = "zipFile_" + resultInfo.get("PID") + ".zip";
                } else {
                    zipFilePrefix = "zipFile_" + fileId + ".zip";

                }
                File zipFile = new File(TEMP_DIR, zipFilePrefix);

                //deleteTempFileList.add(zipFile);
                String zfname = zipFile.getName();
                zipFileName = zfname;
                zipFiles(new FileOutputStream(zipFile), zipFileList);

                /*
                try {
                Thread.sleep(1000);
                } catch (Exception e) {
                        
                }
                 */

                zipResourceDynFileName = new ByteArrayResource(
                        toByteArray(new FileInputStream(zipFile.getAbsolutePath())));
                dbgLog.info("Subsetting: zipFileName=" + zipFileName);
                dbgLog.info("Subsetting: zipFile, absolute path: " + zipFile.getAbsolutePath());
                dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);

                // Hide 'Create' button, show 'Download' button:
                dwnldButton.setRendered(false);
                dwnloadSubsetButton.setRendered(true);
                dbgLog.info("***** within dwnldAction(): ends here *****");

                /*
                 * Navigation: 
                 *  - is it necessary to use "faces-redirect" navigation here? 
                 *    Or should we simply return "" as long as we want to stay 
                 *    on the subsetting page?
                if (versionNumber != null) {
                    return "/study/SubsettingPage?faces-redirect=true&dtId=" + dtId + "&versionNumber=" + versionNumber;
                }
                return "/study/SubsettingPage?faces-redirect=true&dtId=" + dtId;
                 */
                return "";

            } catch (IOException e) {
                // file-access problem, etc.
                e.printStackTrace();
                dbgLog.fine("download zipping IO exception");
                msgDwnldButton.setValue("* an IO problem occurred");
                msgDwnldButton.setVisible(true);
                dbgLog.warning("exiting dwnldAction() due to an IO problem ");
                getVDCRequestBean().setSelectedTab("tabDwnld");
                dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                return "";
            }
            // end of zipping step

        } catch (IOException e) {
            e.printStackTrace();

            msgDwnldButton.setValue("* an IO problem occurred");
            msgDwnldButton.setVisible(true);
            dbgLog.warning("exiting dwnldAction() due to an IO problem ");
            getVDCRequestBean().setSelectedTab("tabDwnld");
            dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
            return "";
        }

        // end: params are OK-case
    } else {
        // the selection is incomplete
        // show error message;
        pgDwnldErrMsg.setRendered(true);
        msgDwnldButton.setValue("* Error: Select a file format");
        msgDwnldButton.setVisible(true);
        dbgLog.warning("exiting dwnldAction() due to incomplete data ");
        getVDCRequestBean().setSelectedTab("tabDwnld");

        return "";
    } // end: checking params

}

From source file:edu.harvard.iq.dvn.core.web.subsetting.AnalysisPage.java

public String advStatAction() {
    //        dbgLog.fine("selected tab(advStat)="+getTabSet1().getSelected());
    // check the current model

    String mdlName = (String) dropDown1.getValue();
    dbgLog.fine("model name=" + mdlName);

    AdvancedStatGUIdata.Model modelSpec = getAnalysisApplicationBean().getSpecMap().get(mdlName);

    if (checkAdvStatParameters(mdlName)) {

        FacesContext cntxt = FacesContext.getCurrentInstance();

        HttpServletResponse res = (HttpServletResponse) cntxt.getExternalContext().getResponse();
        HttpServletRequest req = (HttpServletRequest) cntxt.getExternalContext().getRequest();

        dbgLog.fine("***** within advStatAction() *****");
        // common parts
        // data file
        StudyFile sf = dataTable.getStudyFile();
        Long noRecords = dataTable.getRecordsPerCase();

        String dsbUrl = getDsbUrl();
        dbgLog.fine("dsbUrl=" + dsbUrl);

        String serverPrefix = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort()
                + req.getContextPath();//  ww w . jav  a  2s  . c  o  m

        dbgLog.fine("serverPrefix=" + serverPrefix);
        //                /
        //                  "optnlst_a" => "A01|A02|A03", "analysis" => "A01 A02",
        //                  "varbl" => "v1.3 v1.10 v1.13 v1.22 v1.40", "charVarNoSet" =>
        //                  "v1.10|v1.719",
        //                 /

        // common parameters
        Map<String, List<String>> mpl = new HashMap<String, List<String>>();

        List<String> alst = new ArrayList<String>();
        List<String> aoplst = new ArrayList<String>();

        aoplst.add("A01|A02|A03");
        mpl.put("optnlst_a", aoplst);

        Map<String, List<String>> xtbro = new HashMap<String, List<String>>();

        // xtbro: modelName
        xtbro.put("modelName", Arrays.asList(mdlName));

        // outoput options

        List<String> outOptionList = new ArrayList<String>();

        mpl.put("modelName", Arrays.asList(mdlName));
        if (mdlName.equals("xtb")) {
            mpl.put("requestType", Arrays.asList("Xtab"));
        } else {
            mpl.put("requestType", Arrays.asList("Zelig"));
        }

        if (mdlName.equals("xtb")) {
            alst.add("A03");

            // output options
            // Object[] outOptn = (Object[]) checkboxGroupXtbOptions.getSelectedValue();
            Object[] outOptn = chkbxAdvStatOutputXtbOpt.getSelectedValues();
            List<String> tv = new ArrayList<String>();
            //tv.add("T");

            for (int j = 0; j < outOptn.length; j++) {
                dbgLog.fine("output option[" + j + "]=" + outOptn[j]);
                mpl.put((String) outOptn[j], Arrays.asList("T"));
                tv.add((String) outOptn[j]);
            }
            mpl.put("xtb_outputOptions", tv);

            // variables: 1st RBox
            if (advStatVarRBox1.size() >= 1) {
                dbgLog.fine("RB1:" + getDataVariableForRBox1());
                mpl.put("xtb_nmBxR1", getDataVariableForRBox1());
            }

            // variables: 2nd RBox
            if (advStatVarRBox2.size() >= 1) {
                dbgLog.fine("RB2:" + getDataVariableForRBox2());
                mpl.put("xtb_nmBxR2", getDataVariableForRBox2());
            }

            mpl.put("analysis", alst);

        } else {
            // Zelig cases

            dbgLog.fine("***** zelig param block *****");
            // non-xtb, i.e., zelig cases
            // check zlg value

            dbgLog.fine("model spec dump=" + modelSpec);
            dbgLog.fine("model spec mdlId=" + modelSpec.getMdlId());

            String zligPrefix = modelSpec.getMdlId();
            dbgLog.fine("model no=" + zligPrefix);

            // get the varId-list of each box
            // 1-RBox case
            if (advStatVarRBox1.size() >= 1) {
                dbgLog.fine("RB1:" + getDataVariableForRBox1());
                //mpl.put(zligPrefix + "_nmBxR1", getDataVariableForRBox1());
                mpl.put("nmBxR1", getDataVariableForRBox1());
            }

            // 2-RBox case
            if (advStatVarRBox2.size() >= 1) {
                dbgLog.fine("RB2:" + getDataVariableForRBox2());
                //mpl.put(zligPrefix + "_nmBxR2", getDataVariableForRBox2());
                mpl.put("nmBxR2", getDataVariableForRBox2());
            }

            // 3-RBox case
            if (advStatVarRBox3.size() >= 1) {
                dbgLog.fine("RB3:" + getDataVariableForRBox3());
                //mpl.put(zligPrefix + "_nmBxR3", getDataVariableForRBox3());
                mpl.put("nmBxR3", getDataVariableForRBox3());
            }

            // model name
            //mpl.put("zlg", getZlg(zligPrefix, mdlName));

            // model type
            //String sfn = modelSpec.getSpecialFn();
            //mpl.put("mdlType_" + mdlName, getMdlType(mdlName, sfn));

            // model title
            //String ttl = modelSpec.getTitle();
            //dbgLog.fine("model title=" + ttl);
            //mpl.put("mdlTitle_" + mdlName, Arrays.asList(ttl));
            //mpl.put("mdlTitle", Arrays.asList(ttl));

            // nrBoxes
            int noRboxes = modelSpec.getNoRboxes();
            dbgLog.fine("noRboxes=" + noRboxes);

            //mpl.put("noBoxes_" + mdlName, Arrays.asList(Integer.toString(noRboxes)));
            mpl.put("noBoxes", Arrays.asList(Integer.toString(noRboxes)));

            // binary
            String mdlCategory = modelSpec.getCategory();
            String outcomeType = modelSpec.getVarBox().get(0).getVarType();

            dbgLog.fine("model category=" + mdlCategory);

            if (mdlCategory.equals("Models for Dichotomous Dependent Variables")) {
                mpl.put("mdlDepVarType", Arrays.asList("binary"));
            }
            if (outcomeType.equals("binary")) {
                mpl.put("isOutcomeBinary", Arrays.asList("T"));
            } else {
                mpl.put("isOutcomeBinary", Arrays.asList("F"));
            }
            // output options
            //                    //
            //                     // zlg_017_Summary zlg_017_Plots zlg_017_BinOutput
            //                     //
            //Object[] outOptn = (Object[]) checkboxGroup2DefaultOptions.getSelectedValue();
            Object[] outOptn = chkbxAdvStatOutputOpt.getSelectedValues();
            List<String> tv = new ArrayList<String>();

            for (int j = 0; j < outOptn.length; j++) {
                //String outputOptnkey = zligPrefix + "_"+ (String) outOptn[j];
                String outputOptnkey = (String) outOptn[j];
                dbgLog.fine("zelig: output option[" + j + "]=" + outputOptnkey);
                mpl.put(outputOptnkey, Arrays.asList("T"));
                tv.add((String) outOptn[j]);
            }
            mpl.put("zelig_outputOptions", tv);

            // analysis options
            //                    
            //                      zlg_017_Sim zlg_017_setx zlg_017_setx_var
            //                      zlg_017_setx_val_1 zlg_017_setx_val_2
            //                      
            //                      zlg_017_naMethod
            //                     
            //
            dbgLog.fine("simulationPanel is selected=" + simulationPanel.isSelected());
            if (simulationPanel.isSelected()) {
                dbgLog.fine("radioSimTypeChoiceSelected=" + radioSimTypeChoiceSelected);
                //mpl.put(zligPrefix + "_Sim", Arrays.asList("T"));
                mpl.put("Sim", Arrays.asList("T"));

                //Object simOptn = radioButtonGroup1DefaultOptions.getSelectedValue();
                dbgLog.fine("radioSimTypeChoice(value)=" + radioSimTypeChoice.getValue());
                dbgLog.fine("radioSimTypeChoiceSelected=" + radioSimTypeChoiceSelected);
                // simOptn = 0 or 1
                //mpl.put(zligPrefix + "_setx", Arrays.asList((String) simOptn));
                //                        mpl.put("setx", Arrays.asList((String) simOptn));
                //                        mpl.put("setxType", Arrays.asList((String) simOptn));
                mpl.put("setx", Arrays.asList(radioSimTypeChoiceSelected));
                mpl.put("setxType", Arrays.asList(radioSimTypeChoiceSelected));
                if (radioSimTypeChoiceSelected.equals("1")) {
                    Object v1 = dropDown2.getValue();
                    Object v2 = dropDown3.getValue();
                    Object vl1 = textField10.getValue();
                    Object vl2 = textField8.getValue();
                    dbgLog.fine("v1=" + v1);
                    dbgLog.fine("v2=" + v2);
                    dbgLog.fine("vl1=" + vl1);
                    dbgLog.fine("vl2=" + vl2);

                    List<String> setxVars = new ArrayList<String>();

                    if (v1 != null) {
                        setxVars.add((String) v1);

                    }
                    if (v2 != null) {
                        setxVars.add((String) v2);
                    }
                    //mpl.put(zligPrefix + "_setx_var", setxVars);
                    mpl.put("setx_var", setxVars);

                    if (vl1 != null) {
                        //mpl.put(zligPrefix + "_setx_val_1", Arrays.asList((String) vl1));
                        mpl.put("setx_val_1", Arrays.asList((String) vl1));
                    }
                    if (vl2 != null) {
                        //mpl.put(zligPrefix + "_setx_val_2", Arrays.asList((String) vl2));
                        mpl.put("setx_val_2", Arrays.asList((String) vl2));
                    }

                    List<String> setxVar1 = new ArrayList<String>();

                    if (v1 != null) {
                        setxVar1.add("v" + v1);
                        if (vl1 != null) {
                            setxVar1.add((String) vl1);
                        } else {
                            setxVar1.add("");
                        }
                        mpl.put("setx_var1", setxVar1);
                    }

                    if (v2 != null) {
                        List<String> setxVar2 = new ArrayList<String>();

                        setxVar2.add("v" + v2);
                        if (vl2 != null) {
                            setxVar2.add((String) vl2);
                        } else {
                            setxVar2.add("");
                        }
                        mpl.put("setx_var2", setxVar2);
                    }

                }
            } else {
                mpl.put("Sim", Arrays.asList("F"));
            }

        }

        dbgLog.fine("contents(mpl so far):" + mpl);

        // if there is a user-defined (recoded) variables
        /*
        if (recodedVarSet.size() > 0) {
            mpl.putAll(getRecodedVarParameters());
        }
        */
        //                dbgLog.fine("citation info to be sent:\n" + citation);
        //                mpl.put("OfflineCitation", Arrays.asList(citation));
        //                mpl.put("appSERVER", Arrays.asList(req.getServerName() +
        //                    ":"+ req.getServerPort() + req.getContextPath()));

        mpl.put("studytitle", Arrays.asList(getStudyTitle()));
        dbgLog.fine("alt studyId=" + getStudyId().toString());
        mpl.put("studyno", Arrays.asList(getStudyId().toString()));
        mpl.put("studyURL", Arrays.asList(studyURL));
        mpl.put("browserType", Arrays.asList(browserType));

        mpl.put("recodedVarIdSet", getRecodedVarIdSet());
        mpl.put("recodedVarNameSet", getRecodedVarNameSet());
        mpl.put("recodedVarLabelSet", getRecodedVarLabelSet());
        mpl.put("recodedVarTypeSet", getRecodedVariableType());
        mpl.put("recodedVarBaseTypeSet", getBaseVariableTypeForRecodedVariable());

        mpl.put("baseVarIdSet", getBaseVarIdSetFromRecodedVarIdSet());
        mpl.put("baseVarNameSet", getBaseVarNameSetFromRecodedVarIdSet());

        // -----------------------------------------------------
        // New processing route
        // 
        // Step 0. Locate the data file and its attributes

        String fileId = sf.getId().toString();
        //VDC vdc = vdcService.getVDCFromRequest(req);

        String fileloc = sf.getFileSystemLocation();
        String tabflnm = sf.getFileName();
        boolean sbstOK = sf.isSubsettable();
        String flct = sf.getFileType();

        dbgLog.fine("location=" + fileloc);
        dbgLog.fine("filename=" + tabflnm);
        dbgLog.fine("subsettable=" + sbstOK);
        dbgLog.fine("filetype=" + flct);

        DvnRJobRequest sro = null;

        List<File> zipFileList = new ArrayList();

        // the data file for downloading/statistical analyses must be subset-ready
        // local (relative to the application) file case 
        // note: a typical remote case is: US Census Bureau
        File tmpsbfl = null;
        if (sbstOK) {

            try {
                // this temp file will store the requested columns:
                tmpsbfl = File.createTempFile("tempsubsetfile.", ".tab");
                deleteTempFileList.add(tmpsbfl);

                // to produce this file, we'll either open the stream
                // and run our local subsetting code on it, or request
                // the subsetting to be performed natively by the access
                // driver, if it supports the functionality:

                // check whether a source file is tab-delimited or not

                boolean fieldcut = true;
                if ((noRecords != null) && (noRecords >= 1)) {
                    fieldcut = false;
                }

                DataAccessRequest daReq = new DataAccessRequest();
                daReq.setParameter("noVarHeader", "1");

                DataAccessObject accessObject = DataAccess.createDataAccessObject(sf, daReq);

                if (accessObject.isSubsetSupported()) {
                    dbgLog.fine("Using NATIVE subset functionality of the repository.");
                    daReq.setParameter("vars", getVariableNamesForSubset());

                    accessObject.open();

                    InputStream inSubset = accessObject.getInputStream();
                    OutputStream outSubset = new BufferedOutputStream(
                            new FileOutputStream(tmpsbfl.getAbsolutePath()));

                    int bufsize = 8192;
                    byte[] subsetDataBuffer = new byte[bufsize];
                    while ((bufsize = inSubset.read(subsetDataBuffer)) != -1) {
                        outSubset.write(subsetDataBuffer, 0, bufsize);
                    }

                    inSubset.close();
                    outSubset.close();

                    // TODO: catch exceptions; reset the state of the page
                    // if anything went wrong. See the fixed-field section
                    // below for an example.

                } else {
                    accessObject.open();

                    if (fieldcut) {
                        // Cutting requested fields of data from a TAB-delimited stream:

                        Set<Integer> fields = getFieldNumbersForSubsetting();
                        dbgLog.fine("subsetting fields=" + fields);

                        // Create an instance of DvnJavaFieldCutter
                        FieldCutter fc = new DvnJavaFieldCutter();

                        // Executes the subsetting request
                        fc.subsetFile(accessObject.getInputStream(), tmpsbfl.getAbsolutePath(), fields,
                                dataTable.getCaseQuantity(), "\t");

                        // TODO: catch exceptions; reset the state of the page
                        // if anything went wrong. See the fixed-field section
                        // below for an example.

                    } else {
                        // Cutting requested columns of data from a fixed-field stream:

                        Map<Long, List<List<Integer>>> varMetaSet = getSubsettingMetaData(noRecords);
                        DvnNewJavaFieldCutter fc = new DvnNewJavaFieldCutter(varMetaSet);

                        try {
                            //fc.cutColumns(new File(cutOp1), noRecords.intValue(), 0, "\t", tmpsbfl.getAbsolutePath());
                            fc.cutColumns(accessObject.getInputStream(), noRecords.intValue(), 0, "\t",
                                    tmpsbfl.getAbsolutePath());
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();

                            msgDwnldButton.setValue("* could not generate subset due to an IO problem");
                            msgDwnldButton.setVisible(true);
                            dbgLog.warning("exiting dwnldAction() due to an IO problem ");
                            getVDCRequestBean().setSelectedTab("tabDwnld");
                            dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                            return "";

                        } catch (RuntimeException re) {
                            re.printStackTrace();

                            msgDwnldButton.setValue("* could not generate subset due to an runtime error");
                            msgDwnldButton.setVisible(true);
                            dbgLog.warning("exiting dwnldAction() due to an runtime error");
                            getVDCRequestBean().setSelectedTab("tabDwnld");
                            dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                            return ""; //"failure";

                        }

                    }
                }

                // Checks the resulting subset file 
                if (tmpsbfl.exists()) {
                    Long subsetFileSize = tmpsbfl.length();
                    dbgLog.fine("subsettFile:Length=" + subsetFileSize);
                    dbgLog.fine("tmpsb file name=" + tmpsbfl.getAbsolutePath());

                    if (subsetFileSize > 0) {
                        mpl.put("subsetFileName", Arrays.asList(tmpsbfl.getAbsolutePath()));
                        mpl.put("subsetDataFileName", Arrays.asList(tmpsbfl.getName()));
                    } else {
                        // subset file exists but it is empty

                        msgAdvStatButton.setValue("* an subset file is empty");
                        msgAdvStatButton.setVisible(true);
                        dbgLog.warning("exiting advStatAction() due to a subsetting error:"
                                + "a subset file is empty");
                        getVDCRequestBean().setSelectedTab("tabAdvStat");

                        return ""; //"failure";

                    }
                } else {
                    // subset file was not created
                    msgAdvStatButton.setValue("* a subset file was not created");
                    msgAdvStatButton.setVisible(true);
                    dbgLog.warning("exiting advStatAction() due to a subsetting error:"
                            + "a subset file was not created");
                    getVDCRequestBean().setSelectedTab("tabAdvStat");

                    return ""; //"failure";

                }

                // Step 3. Organizes parameters/metadata to be sent to the implemented
                // data-analysis-service class

                //Map<String, Map<String, String>> vls = getValueTableForRequestedVariables(getDataVariableForRequest());
                Map<String, Map<String, String>> vls = getValueTablesForAllRequestedVariables();

                sro = new DvnRJobRequest(getDataVariableForRequest(), mpl, vls, recodeSchema, modelSpec);

                //dbgLog.fine("Prepared sro dump:\n"+ToStringBuilder.reflectionToString(sro, ToStringStyle.MULTI_LINE_STYLE));

                // Step 4. Creates an instance of the the implemented
                // data-analysis-service class 

                DvnRDataAnalysisServiceImpl das = new DvnRDataAnalysisServiceImpl();

                // Executes a request of downloading or data analysis and 
                // capture result info as a Map <String, String>

                resultInfo = das.execute(sro);

                // Step 5. Checks the DSB-exit-status code
                if (resultInfo.get("RexecError").equals("true")) {
                    //msgAdvStatButton.setValue("* The Request failed due to an R-runtime error");
                    msgAdvStatButton.setValue("* The Request failed due to an R-runtime error");
                    msgAdvStatButton.setVisible(true);
                    dbgLog.fine("exiting advStatAction() due to an R-runtime error");
                    getVDCRequestBean().setSelectedTab("tabAdvStat");

                    return ""; //"failure";
                } else {
                    if (recodeSchema.size() > 0) {
                        resultInfo.put("subsettingCriteria", sro.getSubsetConditionsForCitation());
                    } else {
                        resultInfo.put("subsettingCriteria", "");
                    }
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
                // pass the error message to the resultPage
                // resultInfo.put();
                msgAdvStatButton.setValue("* file URL is malformed");
                msgAdvStatButton.setVisible(true);
                dbgLog.warning("exiting advStatAction() due to a URL problem ");
                getVDCRequestBean().setSelectedTab("tabAdvStat");

                return ""; //"failure";

            } catch (IOException e) {
                // this may occur if the dataverse is not released
                // the file exists, but it is not accessible 
                e.printStackTrace();

                msgAdvStatButton.setValue("* an IO problem occurred");
                msgAdvStatButton.setVisible(true);
                dbgLog.warning("exiting advStatAction() due to an IO problem ");
                getVDCRequestBean().setSelectedTab("tabAdvStat");

                return ""; //"failure";

            }
            // end of the subset-OK case
        } else {
            // not subsettable data file
            msgAdvStatButton.setValue("* this data file is not subsettable file");
            msgAdvStatButton.setVisible(true);
            dbgLog.warning("exiting advStatAction(): the data file is not subsettable ");
            getVDCRequestBean().setSelectedTab("tabAdvStat");

            return ""; //"failure";

        } // end:subsetNotOKcase

        // final processing steps for all successful cases

        resultInfo.put("offlineCitation", getCitation());
        resultInfo.put("studyTitle", getStudyTitle());
        resultInfo.put("studyNo", getStudyId().toString());
        resultInfo.put("dtId", dtId.toString());
        if (versionNumber != null) {
            resultInfo.put("versionNumber", versionNumber.toString());
        }
        resultInfo.put("studyURL", studyURL);
        resultInfo.put("R_min_verion_no", resultInfo.get("Rversion").substring(2));
        resultInfo.put("dataverse_version_no", dvnVersionNumber);

        dbgLog.fine("RwrkspFileName=" + resultInfo.get("wrkspFileName"));

        // writing necessary files
        try {

            // rename the subsetting file
            File tmpsbflnew = File.createTempFile(SUBSET_FILENAME_PREFIX + resultInfo.get("PID") + ".", ".tab");
            deleteTempFileList.add(tmpsbflnew);
            InputStream inb = new BufferedInputStream(new FileInputStream(tmpsbfl));
            OutputStream outb = new BufferedOutputStream(new FileOutputStream(tmpsbflnew));

            int bufsize;
            byte[] bffr = new byte[8192];
            while ((bufsize = inb.read(bffr)) != -1) {
                outb.write(bffr, 0, bufsize);
            }
            inb.close();
            outb.close();

            String rhistNew = StringUtils.replace(resultInfo.get("RCommandHistory"), tmpsbfl.getName(),
                    tmpsbflnew.getName());

            //zipFileList.add(tmpsbflnew);

            // (1) write a citation file 
            //                String citationFilePrefix = "citationFile_"+ resultInfo.get("PID") + "_";
            //                File tmpcfl = File.createTempFile(citationFilePrefix, ".txt");
            //
            //                zipFileList.add(tmpcfl);
            //                deleteTempFileList.add(tmpcfl);
            //
            //                DvnCitationFileWriter dcfw = new DvnCitationFileWriter(resultInfo);
            //
            //                String fmpcflFullname = tmpcfl.getAbsolutePath();
            //                String fmpcflname = tmpcfl.getName();
            //                dcfw.write(tmpcfl);

            // (2) R command file
            String rhistoryFilePrefix = R_COMMAND_FILE_PREFIX + resultInfo.get("PID") + ".R";
            File tmpRhfl = new File(TEMP_DIR, rhistoryFilePrefix);

            zipFileList.add(tmpRhfl);
            deleteTempFileList.add(tmpRhfl);
            resultInfo.put("dvn_R_helper_file", "dvn_helper.R");
            DvnReplicationCodeFileWriter rcfw = new DvnReplicationCodeFileWriter(resultInfo);
            if (mdlName.equals("xtb")) {
                rcfw.writeXtabCode(tmpRhfl);
            } else {
                rcfw.writeZeligCode(tmpRhfl);
            }

            // (3) RData Replication file
            String wrkspFileName = resultInfo.get("wrkspFileName");
            dbgLog.fine("wrkspFileName=" + wrkspFileName);

            File RwrkspFileName = new File(wrkspFileName);
            if (RwrkspFileName.exists()) {
                dbgLog.fine("RwrkspFileName:length=" + RwrkspFileName.length());

                zipFileList.add(RwrkspFileName);

            } else {
                dbgLog.fine("RwrkspFileName does not exist");
                //msgAdvStatButton.setValue("* The workspace file is not available");
                //msgAdvStatButton.setVisible(true);
                dbgLog.warning("advStatAction(): R workspace file was not transferred");
                //getVDCRequestBean().setSelectedTab("tabAdvStat");

                //return "failure";
            }
            deleteTempFileList.add(RwrkspFileName);

            // vdc_startup.R file
            //                String vdc_startupFileName = resultInfo.get("vdc_startupFileName");
            //                dbgLog.fine("vdc_startupFileName="+vdc_startupFileName);
            //                File vdcstrtFileName = new File(vdc_startupFileName);
            //                if (vdcstrtFileName.exists()){
            //                    dbgLog.fine("vdcstrtFileName:length="+vdcstrtFileName.length());
            //                    zipFileList.add(vdcstrtFileName);
            //                } else {
            //                    dbgLog.fine("vdcstrtFileName does not exist");
            //                    //msgAdvStatButton.setValue("* vdc_startup.R is not available");
            //                    //msgAdvStatButton.setVisible(true);
            //                    dbgLog.warning("advStatAction(): vdc_startup.R was not transferred");
            //                    //getVDCRequestBean().setSelectedTab("tabAdvStat");
            //
            //                    //return "failure";
            //                }
            //                deleteTempFileList.add(vdcstrtFileName);
            // (4) add replication readme file
            String readMeFileName = REP_README_FILE_PREFIX + resultInfo.get("PID") + ".txt";
            File readMeFile = new File(TEMP_DIR, readMeFileName);

            DvnReplicationREADMEFileWriter rw = new DvnReplicationREADMEFileWriter(resultInfo);
            rw.writeREADMEfile(readMeFile);
            //zipFileList.add(REP_README_FILE);
            zipFileList.add(readMeFile);
            deleteTempFileList.add(readMeFile);
            // (5) helper
            zipFileList.add(DVN_R_HELPER_FILE);
            if (mdlName.equals("xtb")) {
                // (6) css file
                zipFileList.add(DVN_R2HTML_CSS_FILE);

            }
            // zip the following files as a replication-pack
            //
            // local     local        local      remote
            // citation, tab-file,   R history,  wrksp

            for (File f : zipFileList) {
                dbgLog.fine("path=" + f.getAbsolutePath() + "\tname=" + f.getName());
            }

            // zipping all required files
            try {
                String zipFilePrefix = "zipFile_" + resultInfo.get("PID") + ".zip";
                File zipFile = new File(TEMP_DIR, zipFilePrefix);

                deleteTempFileList.add(zipFile);
                //res.setContentType("application/zip");
                String zfname = zipFile.getAbsolutePath();
                //res.setHeader("content-disposition", "attachment; filename=" + zfname);

                OutputStream zfout = new FileOutputStream(zipFile);
                //zipFiles(res.getOutputStream(), zipFileList);
                zipFiles(zfout, zipFileList);

                if (zipFile.exists()) {
                    Long zipFileSize = zipFile.length();
                    dbgLog.fine("zip file:length=" + zipFileSize);
                    dbgLog.fine("zip file:name=" + zipFile.getAbsolutePath());
                    if (zipFileSize > 0) {
                        resultInfo.put("replicationZipFile", zfname);
                        resultInfo.put("replicationZipFileName", zipFile.getName());

                    } else {
                        dbgLog.fine("zip file is empty");
                    }
                } else {
                    dbgLog.fine("zip file was not saved");
                }

                // put resultInfo into the session object
                resultInfo.remove("RCommandHistory");
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("resultInfo",
                        resultInfo);

                dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);

                dbgLog.fine("***** within advStatAction(): succcessfully ends here *****");

                return "/subsetting/AnalysisResultsPage?faces-redirect=true" + getContextSuffix(); //"success";

            } catch (IOException e) {
                // file-access problem, etc.
                e.printStackTrace();
                dbgLog.fine("zipping IO exception");
                msgAdvStatButton.setValue("* an IO problem occurred during zipping replication files");
                msgAdvStatButton.setVisible(true);
                dbgLog.warning("exiting edaAction() due to an zipping IO problem ");
                //getVDCRequestBean().setSelectedTab("tabAdvStat");
                dvnDSBTimerService.createTimer(deleteTempFileList, TEMP_FILE_LIFETIME);
                return "/subsetting/AnalysisResultsPage?faces-redirect=true" + getContextSuffix(); //"success";
            }
            // end of zipping step

        } catch (IOException e) {
            // io errors caught during writing files
            e.printStackTrace();

            msgAdvStatButton.setValue("* an IO problem occurred");
            msgAdvStatButton.setVisible(true);
            dbgLog.warning("exiting edaAction() due to an IO problem ");
            getVDCRequestBean().setSelectedTab("tabAdvStat");

            return ""; //"failure";
        }

        // end of CheckParameters: OK case
    } else {
        // parameters are not complete: show error message;

        msgAdvStatButton.setValue("* Selection is incomplete");
        msgAdvStatButton.setVisible(true);
        dbgLog.fine("exiting advStatAction(): selection is incomplete");
        getVDCRequestBean().setSelectedTab("tabAdvStat");

        return ""; //"failure";
    }

}

From source file:org.ecocean.servlet.UserCreate.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    String context = "context0";
    //context=ServletUtilities.getContext(request);

    //set up the user directory
    //setup data dir
    String rootWebappPath = getServletContext().getRealPath("/");
    File webappsDir = new File(rootWebappPath).getParentFile();
    File shepherdDataDir = new File(webappsDir, CommonConfiguration.getDataDirectoryName(context));
    if (!shepherdDataDir.exists()) {
        shepherdDataDir.mkdirs();/*from  ww  w.  j a  va 2 s  . c om*/
    }
    File usersDir = new File(shepherdDataDir.getAbsolutePath() + "/users");
    if (!usersDir.exists()) {
        usersDir.mkdirs();
    }

    //set up for response
    response.setContentType("text/html; charset=UTF-8");
    PrintWriter out = response.getWriter();
    boolean createThisUser = false;

    String addedRoles = "";
    boolean isEdit = false;
    if (request.getParameter("isEdit") != null) {
        isEdit = true;
        //System.out.println("isEdit is TRUE in UserCreate!");
    }

    //create a new Role from an encounter

    if ((request.getParameter("username") != null) && (!request.getParameter("username").trim().equals(""))
            && (((request.getParameter("password") != null)
                    && (!request.getParameter("password").trim().equals(""))
                    && (request.getParameter("password2") != null)
                    && (!request.getParameter("password2").trim().equals("")))
                    || (request.getParameter("isEdit") != null))) {

        String username = request.getParameter("username").trim();

        String password = "";
        if (!isEdit)
            password = request.getParameter("password").trim();
        String password2 = "";
        if (!isEdit)
            password2 = request.getParameter("password2").trim();

        if ((password.equals(password2)) || (isEdit)) {

            Shepherd myShepherd = new Shepherd(context);
            myShepherd.setAction("UserCreate.class");

            User newUser = new User();

            myShepherd.beginDBTransaction();

            if (myShepherd.getUser(username) == null) {

                String salt = ServletUtilities.getSalt().toHex();
                String hashedPassword = ServletUtilities.hashAndSaltPassword(password, salt);
                //System.out.println("hashed password: "+hashedPassword+" with salt "+salt + " from source password "+password);
                newUser = new User(username, hashedPassword, salt);
                myShepherd.getPM().makePersistent(newUser);
                createThisUser = true;
            } else {
                newUser = myShepherd.getUser(username);
            }

            //here handle all of the other User fields (e.g., email address, etc.)
            if ((request.getParameter("fullName") != null)
                    && (!request.getParameter("fullName").trim().equals(""))) {
                newUser.setFullName(request.getParameter("fullName").trim());
            } else if (isEdit && (request.getParameter("fullName") != null)
                    && (request.getParameter("fullName").trim().equals(""))) {
                newUser.setFullName(null);
            }

            if (request.getParameter("receiveEmails") != null) {
                newUser.setReceiveEmails(true);
            } else {
                newUser.setReceiveEmails(false);
            }

            if ((request.getParameter("emailAddress") != null)
                    && (!request.getParameter("emailAddress").trim().equals(""))) {
                newUser.setEmailAddress(request.getParameter("emailAddress").trim());
            } else if (isEdit && (request.getParameter("emailAddress") != null)
                    && (request.getParameter("emailAddress").trim().equals(""))) {
                newUser.setEmailAddress(null);
            }

            if ((request.getParameter("affiliation") != null)
                    && (!request.getParameter("affiliation").trim().equals(""))) {
                newUser.setAffiliation(request.getParameter("affiliation").trim());
            } else if (isEdit && (request.getParameter("affiliation") != null)
                    && (request.getParameter("affiliation").trim().equals(""))) {
                newUser.setAffiliation(null);
            }

            if ((request.getParameter("userProject") != null)
                    && (!request.getParameter("userProject").trim().equals(""))) {
                newUser.setUserProject(request.getParameter("userProject").trim());
            } else if (isEdit && (request.getParameter("userProject") != null)
                    && (request.getParameter("userProject").trim().equals(""))) {
                newUser.setUserProject(null);
            }

            if ((request.getParameter("userStatement") != null)
                    && (!request.getParameter("userStatement").trim().equals(""))) {
                newUser.setUserStatement(request.getParameter("userStatement").trim());
            } else if (isEdit && (request.getParameter("userStatement") != null)
                    && (request.getParameter("userStatement").trim().equals(""))) {
                newUser.setUserStatement(null);
            }

            if ((request.getParameter("userURL") != null)
                    && (!request.getParameter("userURL").trim().equals(""))) {
                newUser.setUserURL(request.getParameter("userURL").trim());
            } else if (isEdit && (request.getParameter("userURL") != null)
                    && (request.getParameter("userURL").trim().equals(""))) {
                newUser.setUserURL(null);
            }

            newUser.RefreshDate();

            //now handle roles

            //if this is not a new user, we need to blow away all old roles
            List<Role> preexistingRoles = new ArrayList<Role>();
            if (!createThisUser) {
                //get existing roles for this existing user
                preexistingRoles = myShepherd.getAllRolesForUser(username);
                myShepherd.getPM().deletePersistentAll(preexistingRoles);
            }

            //start role processing

            List<String> contexts = ContextConfiguration.getContextNames();
            int numContexts = contexts.size();
            //System.out.println("numContexts is: "+numContexts);
            for (int d = 0; d < numContexts; d++) {

                String[] roles = request.getParameterValues("context" + d + "rolename");
                if (roles != null) {
                    int numRoles = roles.length;
                    //System.out.println("numRoles in context"+d+" is: "+numRoles);
                    for (int i = 0; i < numRoles; i++) {

                        String thisRole = roles[i].trim();
                        if (!thisRole.trim().equals("")) {
                            Role role = new Role();
                            if (myShepherd.getRole(thisRole, username, ("context" + d)) == null) {

                                role.setRolename(thisRole);
                                role.setUsername(username);
                                role.setContext("context" + d);
                                myShepherd.getPM().makePersistent(role);
                                addedRoles += ("SEPARATORSTART"
                                        + ContextConfiguration.getNameForContext("context" + d) + ":" + roles[i]
                                        + "SEPARATOREND");
                                //System.out.println(addedRoles);
                                myShepherd.commitDBTransaction();
                                myShepherd.beginDBTransaction();
                                //System.out.println("Creating role: context"+d+thisRole);
                            }
                        }
                    }
                }
            }
            //end role processing

            myShepherd.commitDBTransaction();
            myShepherd.closeDBTransaction();
            myShepherd = null;

            //output success statement
            out.println(ServletUtilities.getHeader(request));
            if (createThisUser) {
                out.println("<strong>Success:</strong> User '" + StringEscapeUtils.escapeHtml4(username)
                        + "' was successfully created with added roles: <ul>"
                        + addedRoles.replaceAll("SEPARATORSTART", "<li>").replaceAll("SEPARATOREND", "</li>")
                        + "</ul>");
            } else {
                out.println("<strong>Success:</strong> User '" + StringEscapeUtils.escapeHtml4(username)
                        + "' was successfully updated and has assigned roles: <ul>"
                        + addedRoles.replaceAll("SEPARATORSTART", "<li>").replaceAll("SEPARATOREND", "</li>")
                        + "</ul>");

            }
            out.println("<p><a href=\"" + request.getScheme() + "://"
                    + CommonConfiguration.getURLLocation(request) + "/appadmin/users.jsp?context=context0"
                    + "\">Return to User Administration" + "</a></p>\n");
            out.println(ServletUtilities.getFooter(context));

        } else {
            //output failure statement
            out.println(ServletUtilities.getHeader(request));
            out.println(
                    "<strong>Failure:</strong> User was NOT successfully created. Your passwords did not match.");
            out.println("<p><a href=\"" + request.getScheme() + "://"
                    + CommonConfiguration.getURLLocation(request) + "/appadmin/users.jsp?context=context0"
                    + "\">Return to User Administration" + "</a></p>\n");
            out.println(ServletUtilities.getFooter(context));

        }

    } else {
        //output failure statement
        out.println(ServletUtilities.getHeader(request));
        out.println(
                "<strong>Failure:</strong> User was NOT successfully created. I did not have all of the username and password information I needed.");
        out.println("<p><a href=\"" + request.getScheme() + "://" + CommonConfiguration.getURLLocation(request)
                + "/appadmin/users.jsp?context=context0" + "\">Return to User Administration" + "</a></p>\n");
        out.println(ServletUtilities.getFooter(context));

    }

    out.close();

}

From source file:org.ecocean.servlet.AdoptionAction.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String adopterName = "";
    String adopterAddress = "";
    String adopterEmail = "";
    String adopterImage;/*  w ww  .  j  a v  a2s . co m*/
    String adoptionStartDate = "";
    String adoptionEndDate = "";
    String adopterQuote = "";
    String adoptionManager = "";
    String shark = "";
    String encounter = "";
    String notes = "";
    String adoptionType = "";
    String number = "";
    String text = "";

    // Saved to the selected shark, not the adoption.
    String newNickName = "";

    // Storing the customer ID here makes the subscription cancellation process easier to do in less moves.
    String stripeCustomerID = "";

    // Stores some wack response string from google recaptcha.
    String gresp = "";

    boolean adoptionSuccess = true;
    String failureMessage = "";

    //set UTF-8
    request.setCharacterEncoding("UTF-8");

    HttpSession session = request.getSession(true);
    String context = "context0";
    context = ServletUtilities.getContext(request);
    Shepherd myShepherd = new Shepherd(context);
    myShepherd.setAction("AdoptionAction.class");
    System.out.println("in context " + context);
    //request.getSession()getServlet().getServletContext().getRealPath("/"));
    String rootDir = getServletContext().getRealPath("/");
    System.out.println("rootDir=" + rootDir);

    // This value is only stored in the email specific edit form.
    Boolean emailEdit = false;
    if ((Boolean) session.getAttribute("emailEdit") != false) {
        emailEdit = (Boolean) session.getAttribute("emailEdit");
        number = (String) session.getAttribute("sessionAdoptionID");
    }
    //setup data dir
    String rootWebappPath = getServletContext().getRealPath("/");
    File webappsDir = new File(rootWebappPath).getParentFile();
    File shepherdDataDir = new File(webappsDir, CommonConfiguration.getDataDirectoryName(context));
    //if(!shepherdDataDir.exists()){shepherdDataDir.mkdirs();}
    File adoptionsDir = new File(shepherdDataDir.getAbsolutePath() + "/adoptions");
    if (!adoptionsDir.exists()) {
        adoptionsDir.mkdirs();
    }

    //get the form to read data from
    // AdoptionForm theForm = (AdoptionForm) form;

    //set up for response
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    boolean locked = false;

    String fileName = "None";
    String username = "None";
    String fullPathFilename = "";

    String id = "";

    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>();

    Calendar date = Calendar.getInstance();

    long maxSizeMB = CommonConfiguration.getMaxMediaSizeInMegabytes(context);
    long maxSizeBytes = maxSizeMB * 1048576;

    //set form value hashmap
    HashMap fv = new HashMap();

    //else {
    id = "adpt" + (new Integer(date.get(Calendar.DAY_OF_MONTH))).toString()
            + (new Integer(date.get(Calendar.MONTH) + 1)).toString()
            + (new Integer(date.get(Calendar.YEAR))).toString()
            + (new Integer(date.get(Calendar.HOUR_OF_DAY))).toString()
            + (new Integer(date.get(Calendar.MINUTE))).toString()
            + (new Integer(date.get(Calendar.SECOND))).toString();
    //}

    System.out.println("Starting an adoption submission...");
    Calendar todayDate = Calendar.getInstance();

    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 { //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";
    }

    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);

    boolean isEdit = false;

    if (fileSuccess) {

        if ((fv.get("number") != null) && !fv.get("number").toString().equals("")) {

            //handle adoption number processing
            number = fv.get("number").toString();
            if ((number != null) && (!number.equals(""))) {
                isEdit = true;
                System.out.println("Ping! Hit adoption number recieved by action servlet.");
                //myShepherd.beginDBTransaction();
            }

            //end adoption number/id processing
        }

        if ((fv.get("adopterName") != null) && !fv.get("adopterName").toString().equals("")) {
            adopterName = fv.get("adopterName").toString().trim();
        }
        if ((fv.get("adopterAddress") != null) && !fv.get("adopterAddress").toString().equals("")) {
            adopterAddress = fv.get("adopterAddress").toString().trim();
        }
        if ((fv.get("adopterEmail") != null) && !fv.get("adopterEmail").toString().equals("")) {
            adopterEmail = fv.get("adopterEmail").toString().trim();
        }

        if ((fv.get("adoptionStartDate") != null) && !fv.get("adoptionStartDate").toString().equals("")) {
            adoptionStartDate = fv.get("adoptionStartDate").toString().trim();
        }

        if ((fv.get("adoptionEndDate") != null) && !fv.get("adoptionEndDate").toString().equals("")) {
            adoptionEndDate = fv.get("adoptionEndDate").toString().trim();
        }

        if ((fv.get("adopterQuote") != null) && !fv.get("adopterQuote").toString().equals("")) {
            adopterQuote = fv.get("adopterQuote").toString().trim();
        }

        if ((fv.get("adoptionManager") != null) && !fv.get("adoptionManager").toString().equals("")) {
            adoptionManager = fv.get("adoptionManager").toString().trim();
        }

        if ((fv.get("shark") != null) && !fv.get("shark").toString().equals("")) {
            shark = fv.get("shark").toString().trim();
        }

        if ((fv.get("encounter") != null) && !fv.get("encounter").toString().equals("")) {
            encounter = fv.get("encounter").toString().trim();
        }

        if ((fv.get("notes") != null) && !fv.get("notes").toString().equals("")) {
            notes = fv.get("notes").toString().trim();
        }

        if ((fv.get("adoptionType") != null) && !fv.get("adoptionType").toString().equals("")) {
            adoptionType = fv.get("adoptionType").toString().trim();
        }

        if ((fv.get("text") != null) && !fv.get("text").toString().equals("")) {
            text = fv.get("text").toString().trim();
        }

        // New nickname to save to marked individual object.

        if ((fv.get("newNickName") != null) && !fv.get("newNickName").toString().equals("")) {
            newNickName = fv.get("newNickName").toString().trim();
        }

        if ((fv.get("g-recaptcha-response") != null) && !fv.get("g-recaptcha-response").toString().equals("")) {
            gresp = fv.get("g-recaptcha-response").toString().trim();
        }

        if (isEdit) {
            id = number;
        }

        // Grab the stripe customer out of session.

        stripeCustomerID = (String) session.getAttribute("stripeID");

        File thisAdoptionDir = new File(adoptionsDir.getAbsolutePath() + "/" + id);
        if (!thisAdoptionDir.exists()) {
            thisAdoptionDir.mkdirs();
        }

        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);

            try {

                //retrieve the file data
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                InputStream stream = item.getInputStream();
                //System.out.println(writeFile);
                //if ((!(file[iter].getFileName().equals(""))) && (file[iter].getFileSize() > 0)) {
                //write the file to the file specified
                //String writeName=file[iter].getFileName().replace('#', '_').replace('-', '_').replace('+', '_').replaceAll(" ", "_");
                //String writeName=forHTMLTag(file[iter].getFileName());
                String writeName = "adopter.jpg";

                //String writeName=URLEncoder.encode(file[iter].getFileName(), "UTF-8");
                //while (writeName.indexOf(".") != writeName.lastIndexOf(".")) {
                //  writeName = writeName.replaceFirst("\\.", "_");
                // }
                //System.out.println(writeName);

                OutputStream bos = new FileOutputStream(new File(thisAdoptionDir, writeName));
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                    bos.write(buffer, 0, bytesRead);
                }
                bos.close();
                //data = "The file has been written to \"" + id + "\\" + writeName + "\"";
                adopterImage = writeName;
                // }
                //close the stream
                stream.close();
                baos.close();
            } catch (FileNotFoundException fnfe) {
                System.out.println("File not found exception.\n");
                fnfe.printStackTrace();
                //return null;
            } catch (IOException ioe) {
                System.out.println("IO Exception.\n");
                ioe.printStackTrace();
                //return null;
            }

        }

        // This verifies the user being logged in or passing the recapture.
        boolean loggedIn = false;
        try {
            if (request.getUserPrincipal() != null) {
                loggedIn = true;
            }
        } catch (NullPointerException ne) {
            System.out.println("Got a null pointer checking for logged in user.");
        }
        boolean validCaptcha = false;
        if (loggedIn != true) {
            String remoteIP = request.getRemoteAddr();
            validCaptcha = ServletUtilities.captchaIsValid(context, gresp, remoteIP);
            System.out.println("Results from captchaIsValid(): " + validCaptcha);
        }
        if ((validCaptcha == true) || (loggedIn == true)) {
            System.out.println("Ping! Hit the Adoption creation section.");
            try {
                Adoption ad = new Adoption(id, adopterName, adopterEmail, adoptionStartDate, adoptionEndDate);
                if (isEdit || emailEdit) {
                    ad = myShepherd.getAdoption(number);
                    ad.setAdopterName(adopterName);
                    ad.setAdopterEmail(adopterEmail);
                    ad.setAdoptionEndDate(adoptionEndDate);
                    ad.setAdoptionStartDate(adoptionStartDate);
                }

                ad.setAdopterQuote(adopterQuote);
                ad.setAdoptionManager(adoptionManager);
                ad.setIndividual(shark);
                ad.setEncounter(encounter);
                ad.setNotes(notes);
                ad.setAdoptionType(adoptionType);
                ad.setAdopterAddress(adopterAddress);
                ad.setStripeCustomerId(stripeCustomerID);

                if ((filesOK != null) && (filesOK.size() > 0)) {
                    ad.setAdopterImage(filesOK.get(0));
                }

                myShepherd.beginDBTransaction();

                if (adoptionSuccess && !isEdit) {
                    try {
                        myShepherd.storeNewAdoption(ad, id);
                    } catch (Exception e) {
                        adoptionSuccess = false;
                        failureMessage += "Failed to presist the new adoption.<br>";
                    }
                }

                // New logic to change marked individual nickname if necessary in adoption.
                MarkedIndividual mi = myShepherd.getMarkedIndividual(shark);
                if (!newNickName.equals("")) {
                    if (adoptionSuccess && !isEdit) {
                        try {
                            mi.setNickName(newNickName);
                            mi.setNickNamer(adopterName);
                        } catch (Exception e) {
                            failureMessage += "Retrieving shark to set nickname failed.<br>";
                        }
                    }
                }

                // Sends a confirmation email to a a new adopter with cancellation and update information.
                if (emailEdit == false) {
                    try {
                        String emailContext = "context0";
                        String langCode = "en";
                        String to = ad.getAdopterEmail();
                        String type = "adoptionConfirmation";
                        System.out.println("About to email new adopter.");
                        // Retrieve background service for processing emails
                        ThreadPoolExecutor es = MailThreadExecutorService.getExecutorService();
                        Map<String, String> tagMap = NotificationMailer.createBasicTagMap(request, mi, ad);
                        NotificationMailer mailer = new NotificationMailer(emailContext, langCode, to, type,
                                tagMap);
                        NotificationMailer adminMailer = new NotificationMailer(emailContext, langCode,
                                CommonConfiguration.getNewSubmissionEmail(emailContext), type, tagMap);
                        es.execute(mailer);
                        es.execute(adminMailer);
                    } catch (Exception e) {
                        System.out.println("Error in sending email confirmation of adoption.");
                        e.printStackTrace();
                    }
                }

                if ((adoptionSuccess && isEdit) || (emailEdit == true)) {
                    myShepherd.commitDBTransaction();
                }

            } catch (Exception e) {
                System.out.println("The recaptcha passed but something went wrong saving the adoption.");
                e.printStackTrace();
            }

        }

    }
    // Sets adoption paid to false to allow multiple adoptions
    session.setAttribute("paid", false);

    //return a forward to display.jsp
    System.out.println("Ending adoption data submission.");
    //if((submitterID!=null)&&(submitterID.equals("deepblue"))) {
    if ((adoptionSuccess) && (emailEdit == false)) {
        response.sendRedirect(request.getScheme() + "://" + CommonConfiguration.getURLLocation(request)
                + "/adoptions/adoptionSuccess.jsp?id=" + id);
    } else if ((adoptionSuccess) && (emailEdit == true)) {
        response.sendRedirect(request.getScheme() + "://" + CommonConfiguration.getURLLocation(request)
                + "/adoptions/editSuccess.jsp");
    } else {
        response.sendRedirect(request.getScheme() + "://" + CommonConfiguration.getURLLocation(request)
                + "/adoptions/adoptionFailure.jsp?message=" + failureMessage);
    }

    //}

    myShepherd.closeDBTransaction();

}

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  ava  2s .c om
        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 + "&deg; " + 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;
}