Example usage for javax.servlet.http HttpSession getServletContext

List of usage examples for javax.servlet.http HttpSession getServletContext

Introduction

In this page you can find the example usage for javax.servlet.http HttpSession getServletContext.

Prototype

public ServletContext getServletContext();

Source Link

Document

Returns the ServletContext to which this session belongs.

Usage

From source file:org.egov.infra.web.filter.ApplicationCoreFilter.java

private void prepareUserSession(HttpSession session) {
    if (session.getAttribute(CITY_CODE_KEY) == null)
        cityService.cityDataAsMap().forEach(session::setAttribute);
    if (session.getAttribute(APP_RELEASE_ATTRIB_NAME) == null)
        session.setAttribute(APP_RELEASE_ATTRIB_NAME, applicationRelease);
    if (session.getAttribute(TENANTID_KEY) == null)
        session.setAttribute(TENANTID_KEY, ApplicationThreadLocals.getTenantID());
    if (session.getServletContext().getAttribute(CDN_ATTRIB_NAME) == null)
        session.getServletContext().setAttribute(CDN_ATTRIB_NAME, cdnURL);
    if (session.getAttribute(USERID_KEY) == null) {
        Optional<Authentication> authentication = getCurrentAuthentication();
        if (authentication.isPresent() && authentication.get().getPrincipal() instanceof CurrentUser) {
            session.setAttribute(USERID_KEY, ((CurrentUser) authentication.get().getPrincipal()).getUserId());
        } else if (!authentication.isPresent() || !(authentication.get().getPrincipal() instanceof User)) {
            session.setAttribute(USERID_KEY, securityUtils.getCurrentUser().getId());
        }//from w  w w.java2  s  . c  o  m
    }
}

From source file:org.jbpm.formbuilder.server.RESTFileServiceTest.java

private List<Object> createRequestMocks() {
    List<Object> requestMocks = new ArrayList<Object>();
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    ServletContext context = EasyMock.createMock(ServletContext.class);
    EasyMock.expect(request.getSession()).andReturn(session).once();
    EasyMock.expect(session.getServletContext()).andReturn(context).once();
    requestMocks.add(request);/*from   w  ww  . ja  va  2 s.c  o m*/
    requestMocks.add(session);
    requestMocks.add(context);
    return requestMocks;
}

From source file:org.jbpm.formbuilder.server.RESTFileServiceTest.java

public void testSaveFileNotMultipart() throws Exception {
    RESTFileService restService = createSaveFileMockService(null, null, null, false);
    FileService fileService = EasyMock.createMock(FileService.class);
    restService.setFileService(fileService);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    EasyMock.expect(request.getSession()).andReturn(session).once();
    ServletContext context = EasyMock.createMock(ServletContext.class);
    EasyMock.expect(session.getServletContext()).andReturn(context).once();

    EasyMock.replay(fileService, request, session, context);
    Response resp = restService.saveFile("somePackage", request);
    EasyMock.verify(fileService, request, session, context);

    assertNotNull("resp shouldn't be null", resp);
    assertStatus(resp.getStatus(), Status.INTERNAL_SERVER_ERROR);
}

From source file:org.jbpm.formbuilder.server.RESTFileServiceTest.java

public void testSaveFileUploadProblem() throws Exception {
    byte[] bstream = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    String fileName = "fileName";
    RESTFileService restService = createSaveFileMockService(bstream, fileName, FileUploadException.class, true);
    FileService fileService = EasyMock.createMock(FileService.class);
    restService.setFileService(fileService);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    EasyMock.expect(request.getSession()).andReturn(session).once();
    ServletContext context = EasyMock.createMock(ServletContext.class);
    EasyMock.expect(session.getServletContext()).andReturn(context).once();

    EasyMock.replay(fileService, request, session, context);
    Response resp = restService.saveFile("somePackage", request);
    EasyMock.verify(fileService, request, session, context);

    assertNotNull("resp shouldn't be null", resp);
    assertStatus(resp.getStatus(), Status.INTERNAL_SERVER_ERROR);
}

From source file:org.jbpm.formbuilder.server.RESTFileServiceTest.java

public void testSaveFileIOProblem() throws Exception {
    byte[] bstream = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    String fileName = "fileName";
    RESTFileService restService = createSaveFileMockService(bstream, fileName, IOException.class, true);
    FileService fileService = EasyMock.createMock(FileService.class);
    restService.setFileService(fileService);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    EasyMock.expect(request.getSession()).andReturn(session).once();
    ServletContext context = EasyMock.createMock(ServletContext.class);
    EasyMock.expect(session.getServletContext()).andReturn(context).once();

    EasyMock.replay(fileService, request, session, context);
    Response resp = restService.saveFile("somePackage", request);
    EasyMock.verify(fileService, request, session, context);

    assertNotNull("resp shouldn't be null", resp);
    assertStatus(resp.getStatus(), Status.INTERNAL_SERVER_ERROR);
}

From source file:de.betterform.agent.web.WebUtil.java

public static WebProcessor getWebProcessor(String key, HttpServletRequest request, HttpServletResponse response,
        HttpSession session) {
    if (key == null || key.equals("")) {
        LOGGER.warn("SessionKey is null");
        return null;
    }/*w w  w. j av a  2  s  .  c o  m*/

    org.infinispan.Cache<String, FluxProcessor> sessionCache;
    try {
        sessionCache = XFSessionCache.getCache();
    } catch (XFormsException xfe) {
        sessionCache = null;
    }

    if (sessionCache == null || !(sessionCache.containsKey(key))) {
        LOGGER.warn("No xformsSession for key " + key + " in Cache");
        return null;
    }
    WebProcessor processor = sessionCache.get(key);

    if (processor.getContext() != null) {
        return processor;
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Element is read from disk " + processor.toString());
        }
        //re-initialize transient state
        processor.setRequest(request);
        processor.setResponse(response);
        processor.setHttpSession(session);
        processor.setKey(key);
        processor.getHttpRequestHandler();
        processor.setContext(session.getServletContext());

        try {
            processor.configure();
            processor.createUIGenerator();
            processor.init();
            return processor;
        } catch (Exception e) {
            LOGGER.error("Could not reload xformSession from disk.", e);
        }
    }

    return null;

}

From source file:org.jbpm.formbuilder.server.RESTFileServiceTest.java

public void testSaveFileServiceProblem() throws Exception {
    byte[] bstream = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    String fileName = "fileName";
    RESTFileService restService = createSaveFileMockService(bstream, fileName, null, true);
    FileService fileService = EasyMock.createMock(FileService.class);
    FileException exception = new FileException();
    EasyMock.expect(//from w w  w .  ja v a 2  s  .  c o  m
            fileService.storeFile(EasyMock.eq("somePackage"), EasyMock.eq("fileName"), EasyMock.same(bstream)))
            .andThrow(exception).once();
    restService.setFileService(fileService);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    EasyMock.expect(request.getSession()).andReturn(session).once();
    ServletContext context = EasyMock.createMock(ServletContext.class);
    EasyMock.expect(session.getServletContext()).andReturn(context).once();

    EasyMock.replay(fileService, request, session, context);
    Response resp = restService.saveFile("somePackage", request);
    EasyMock.verify(fileService, request, session, context);

    assertNotNull("resp shouldn't be null", resp);
    assertStatus(resp.getStatus(), Status.INTERNAL_SERVER_ERROR);
}

From source file:com.crimelab.service.ChemistryServiceImpl.java

@Override
public Workbook create(Chemistry chemistry, HttpSession session) {
    Workbook wb = null;//from   www .  java2 s .  c  o m
    try {

        //            date = sdf.parse(chemistry.getTimeDateReceived());
        //            sdf.applyPattern("M-dd-yyyy hh:mm:ss a");
        //            dateformat = sdf.format(date);
        InputStream inp = session.getServletContext()
                .getResourceAsStream("/WEB-INF/templates/DefaultDrugs.xls");
        wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);

        Cell examType = findCell(sheet, "$examType");
        Cell reportNo = findCell(sheet, "$reportNo");
        Cell caseType = findCell(sheet, "$caseType");
        Cell suspects = findCell(sheet, "$suspects");
        Cell victims = findCell(sheet, "$victims");
        Cell timeDateReceived = findCell(sheet, "$timeDateReceived");
        Cell requestingParty = findCell(sheet, "$requestingParty");
        Cell specimenSubmitted = findCell(sheet, "$specimenSubmitted");
        Cell purposeOfLabExam = findCell(sheet, "$purposeOfLabExam");
        Cell findings = findCell(sheet, "$findings");
        Cell conclusions = findCell(sheet, "$conclusions");
        Cell remarks = findCell(sheet, "$remarks");
        Cell timeDateCompleted = findCell(sheet, "$timeDateCompleted");
        Cell examinerName = findCell(sheet, "$examinerName");
        Cell examinerRank = findCell(sheet, "$examinerRank");
        Cell examinerPosition = findCell(sheet, "$examinerPosition");
        Cell appName = findCell(sheet, "$appName");
        Cell appRank = findCell(sheet, "$appRank");
        Cell appPosition = findCell(sheet, "$appPosition");
        Cell notedName = findCell(sheet, "$notedName");
        Cell notedRank = findCell(sheet, "$notedRank");
        Cell notedPosition = findCell(sheet, "$notedPosition");
        Cell subscribed = findCell(sheet, "$subscribed");
        Cell subscribedName = findCell(sheet, "$subscribedName");
        Cell subscribedRank = findCell(sheet, "$subscribedRank");
        Cell subscribedPosition = findCell(sheet, "$subscribedPosition");

        examType.setCellValue(chemistry.getExamType());
        reportNo.setCellValue(chemistry.getReportNo());
        caseType.setCellValue(chemistry.getCaseType());
        suspects.setCellValue(chemistry.getSuspects());
        victims.setCellValue(chemistry.getVictims());
        timeDateReceived.setCellValue(chemistry.getTimeDateReceived());
        requestingParty.setCellValue(chemistry.getRequestingParty());
        specimenSubmitted.setCellValue(chemistry.getSpecimenSubmitted());
        purposeOfLabExam.setCellValue(chemistry.getPurposeOfLabExam());
        findings.setCellValue(chemistry.getFindings());
        conclusions.setCellValue(chemistry.getConclusions());
        remarks.setCellValue(chemistry.getRemarks());
        timeDateCompleted.setCellValue(chemistry.getTimeDateCompleted());
        examinerName.setCellValue(chemistry.getExaminerName());
        examinerRank.setCellValue(chemistry.getExaminerRank());
        examinerPosition.setCellValue(chemistry.getExaminerPosition());
        appName.setCellValue(chemistry.getAppName());
        appRank.setCellValue(chemistry.getAppRank());
        appPosition.setCellValue(chemistry.getAppPosition());
        notedName.setCellValue(chemistry.getNotedName());
        notedRank.setCellValue(chemistry.getNotedRank());
        notedPosition.setCellValue(chemistry.getNotedPosition());
        subscribed.setCellValue(chemistry.getSubscribed());
        subscribedName.setCellValue(chemistry.getSubscribedName());
        subscribedRank.setCellValue(chemistry.getSubscribedRank());
        subscribedPosition.setCellValue(chemistry.getSubscribedPosition());

        chemistryDAO.chemistryInfo(chemistry);

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

    }
    return wb;
}

From source file:cs545.proj.controller.MemberController.java

@RequestMapping(value = "/new", method = RequestMethod.POST)
public String memberUpdateWithLicense(@Valid @ModelAttribute("editMember") Member editMember,
        BindingResult result, HttpSession session) throws IllegalStateException, IOException {
    if (result.hasErrors()) {
        return "editMemberTile";
    }//from  w  w w  . j  a v a  2s.  c  o  m

    MultipartFile licenseFile = editMember.getLicenseMultipart();
    if ((licenseFile != null) && (!licenseFile.isEmpty())) {
        String newFilename = sdf.format(new Date()) + licenseFile.getOriginalFilename();
        String rootDirectory = session.getServletContext().getRealPath("/");
        licenseFile.transferTo(new File(rootDirectory + licensePath + newFilename));
        editMember.setLicenseFileName(newFilename);
    }

    Member savedMember = memberService.saveOrMerge(editMember);
    Set<Category> categorySet = new HashSet<Category>();
    for (Category category : savedMember.getSelectedCategories()) {
        categorySet.add(category);
    }
    for (Category category : categorySet) {
        savedMember.removeCategory(category);
    }
    List<Integer> checkedIDs = editMember.getCheckedCategoryIDs();
    if (checkedIDs != null) {
        for (Integer categoryId : checkedIDs) {
            savedMember.addCategory(categoryService.getCategoryById(categoryId));
        }
    }
    savedMember = memberService.saveOrMerge(savedMember);
    return "redirect:/member/detail";
}

From source file:org.jbpm.formbuilder.server.RESTFileServiceTest.java

public void testSaveFileOK() throws Exception {
    byte[] bstream = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    String fileName = "fileName";
    RESTFileService restService = createSaveFileMockService(bstream, fileName, null, true);
    FileService fileService = EasyMock.createMock(FileService.class);
    String url = "http://www.redhat.com";
    EasyMock.expect(//ww w. j a  v a2s . c om
            fileService.storeFile(EasyMock.eq("somePackage"), EasyMock.eq("fileName"), EasyMock.same(bstream)))
            .andReturn(url).once();
    restService.setFileService(fileService);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    EasyMock.expect(request.getSession()).andReturn(session).once();
    ServletContext context = EasyMock.createMock(ServletContext.class);
    EasyMock.expect(session.getServletContext()).andReturn(context).once();

    EasyMock.replay(fileService, request, session, context);
    Response resp = restService.saveFile("somePackage", request);
    EasyMock.verify(fileService, request, session, context);

    assertNotNull("resp shouldn't be null", resp);
    assertStatus(resp.getStatus(), Status.OK);
    assertNotNull("resp.entity shouldn't be null", resp.getEntity());
    Object entity = resp.getEntity();
    assertNotNull("resp.metadata shouldn't be null", resp.getMetadata());
    Object contentType = resp.getMetadata().getFirst(HttpHeaderNames.CONTENT_TYPE);
    assertNotNull("resp.entity shouldn't be null", contentType);
    assertEquals("contentType should be application/xml but is" + contentType, contentType,
            MediaType.TEXT_PLAIN);
    String retval = entity.toString();
    assertTrue("retval should contain url", retval.contains(url));
}