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:net.lightbody.bmp.proxy.jetty.servlet.SessionDump.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    Page page = new Page();

    HttpSession session = request.getSession(getURI(request).indexOf("new") > 0);

    page.title("Session Dump Servlet: ");

    TableForm tf = new TableForm(response.encodeURL(getURI(request)));
    tf.method("POST");

    if (session == null) {
        page.add("<H1>No Session</H1>");
        tf.addButton("Action", "New Session");
    } else {//  w w w . ja va  2s.  com
        try {
            tf.addText("ID", session.getId());
            tf.addText("State", session.isNew() ? "NEW" : "Valid");
            tf.addText("Creation", new Date(session.getCreationTime()).toString());
            tf.addText("Last Access", new Date(session.getLastAccessedTime()).toString());
            tf.addText("Max Inactive", "" + session.getMaxInactiveInterval());

            tf.addText("Context", "" + session.getServletContext());

            Enumeration keys = session.getAttributeNames();
            while (keys.hasMoreElements()) {
                String name = (String) keys.nextElement();
                String value = session.getAttribute(name).toString();
                tf.addText(name, value);
            }

            tf.addTextField("Name", "Property Name", 20, "name");
            tf.addTextField("Value", "Property Value", 20, "value");
            tf.addTextField("MaxAge", "MaxAge(s)", 5, "");
            tf.addButtonArea();
            tf.addButton("Action", "Set");
            tf.addButton("Action", "Remove");
            tf.addButton("Action", "Invalidate");

            page.add(tf);
            tf = null;
            if (request.isRequestedSessionIdFromCookie())
                page.add("<P>Turn off cookies in your browser to try url encoding<BR>");

            if (request.isRequestedSessionIdFromURL())
                page.add("<P>Turn on cookies in your browser to try cookie encoding<BR>");

        } catch (IllegalStateException e) {
            log.debug(LogSupport.EXCEPTION, e);
            page.add("<H1>INVALID Session</H1>");
            tf = new TableForm(getURI(request));
            tf.addButton("Action", "New Session");
        }
    }

    if (tf != null)
        page.add(tf);

    Writer writer = response.getWriter();
    page.write(writer);
    writer.flush();
}

From source file:com.orchestra.portale.controller.NewEventController.java

@RequestMapping(value = "/insertevent", method = RequestMethod.POST)
public ModelAndView insertPoi(HttpServletRequest request, @RequestParam Map<String, String> params,
        @RequestParam("file") MultipartFile[] files, @RequestParam("cover") MultipartFile cover)
        throws InterruptedException {

    CompletePOI poi = new CompletePOI();
    CompletePOI poitest = new CompletePOI();

    ModelAndView model = new ModelAndView("insertpoi");
    ModelAndView model2 = new ModelAndView("errorViewPoi");
    poitest = pm.findOneCompletePoiByName(params.get("name"));
    if (poitest != null && poitest.getName().toLowerCase().equals(params.get("name").toLowerCase())) {

        model2.addObject("err", "Esiste gi un poi chiamato " + params.get("name"));
        return model2;
    } else {// w  w w  .  j a  v a2 s .  co m
        poi.setName(params.get("name"));
        poi.setAddress(params.get("address"));
        double lat = Double.parseDouble(params.get("latitude"));
        double longi = Double.parseDouble(params.get("longitude"));
        poi.setLocation(new double[] { lat, longi });
        poi.setShortDescription(params.get("shortd"));
        poi.setStart_date(params.get("datai"));
        poi.setEnd_date(params.get("dataf"));
        int i = 1;
        ArrayList<String> categories = new ArrayList<String>();
        while (params.containsKey("category" + i)) {

            categories.add(params.get("category" + i));

            i = i + 1;
        }
        poi.setCategories(categories);

        ArrayList<AbstractPoiComponent> listComponent = new ArrayList<AbstractPoiComponent>();
        CoverImgComponent coverimg = new CoverImgComponent();
        //componente cover
        if (!cover.isEmpty()) {

            coverimg.setLink("cover.jpg");
            listComponent.add(coverimg);
        }
        //componente galleria immagini
        ArrayList<ImgGallery> links = new ArrayList<ImgGallery>();
        ImgGalleryComponent img_gallery = new ImgGalleryComponent();
        if (files.length > 0) {

            i = 0;
            while (i < files.length) {
                ImgGallery img = new ImgGallery();

                Thread.sleep(100);
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyyhmmssSSa");
                String currentTimestamp = sdf.format(date);
                img.setLink("img_" + currentTimestamp + ".jpg");
                if (params.containsKey("credit" + (i + 1)))
                    img.setCredit(params.get("credit" + (i + 1)));

                i = i + 1;
                links.add(img);
            }
            img_gallery.setLinks(links);
            listComponent.add(img_gallery);
        }
        //componente contatti
        ContactsComponent contacts_component = new ContactsComponent();
        //Recapiti telefonici
        i = 1;
        boolean contacts = false;
        if (params.containsKey("tel" + i)) {
            ArrayList<PhoneContact> phoneList = new ArrayList<PhoneContact>();

            while (params.containsKey("tel" + i)) {
                PhoneContact phone = new PhoneContact();
                if (params.containsKey("tel" + i)) {
                    phone.setLabel(params.get("desctel" + i));
                }
                phone.setNumber(params.get("tel" + i));
                phoneList.add(phone);
                i = i + 1;
            }
            contacts = true;
            contacts_component.setPhoneList(phoneList);
        }
        //Recapiti mail
        i = 1;
        if (params.containsKey("email" + i)) {
            ArrayList<EmailContact> emailList = new ArrayList<EmailContact>();

            while (params.containsKey("email" + i)) {
                EmailContact email = new EmailContact();
                if (params.containsKey("email" + i)) {
                    email.setLabel(params.get("descemail" + i));
                }
                email.setEmail(params.get("email" + i));
                emailList.add(email);
                i = i + 1;
            }
            contacts = true;
            contacts_component.setEmailsList(emailList);
        }
        //Recapiti fax
        i = 1;
        if (params.containsKey("fax" + i)) {
            ArrayList<FaxContact> faxList = new ArrayList<FaxContact>();

            while (params.containsKey("fax" + i)) {
                FaxContact fax = new FaxContact();
                if (params.containsKey("fax" + i)) {
                    fax.setLabel(params.get("descfax" + i));
                }
                fax.setFax(params.get("fax" + i));
                faxList.add(fax);
                i = i + 1;
            }
            contacts = true;
            contacts_component.setFaxList(faxList);
        }
        //Social predefiniti
        i = 1;
        if (params.containsKey("SN" + i)) {

            while (params.containsKey("SN" + i)) {
                if (params.get("SN" + i).equals("facebook")) {
                    contacts = true;
                    contacts_component.setFacebook(params.get("LSN" + i));
                }
                if (params.get("SN" + i).equals("twitter")) {
                    contacts = true;
                    contacts_component.setTwitter(params.get("LSN" + i));
                }
                if (params.get("SN" + i).equals("google")) {
                    contacts = true;
                    contacts_component.setGoogle(params.get("LSN" + i));
                }
                if (params.get("SN" + i).equals("skype")) {
                    contacts = true;
                    contacts_component.setSkype(params.get("LSN" + i));
                }
                i = i + 1;
            }
        }
        //Social personalizzati
        i = 1;
        if (params.containsKey("CSN" + i)) {
            ArrayList<GenericSocial> customsocial = new ArrayList<GenericSocial>();

            while (params.containsKey("CSN" + i)) {
                GenericSocial social = new GenericSocial();
                contacts = true;
                social.setLabel(params.get("CSN" + i));
                social.setSocial(params.get("LCSN" + i));
                customsocial.add(social);
                i = i + 1;
            }
            contacts_component.setSocialList(customsocial);
        }
        if (contacts == true) {
            listComponent.add(contacts_component);
        }
        //DESCRIPTION COMPONENT
        i = 1;
        if (params.containsKey("par" + i)) {
            ArrayList<Section> list = new ArrayList<Section>();

            while (params.containsKey("par" + i)) {
                Section section = new Section();
                if (params.containsKey("titolo" + i)) {
                    section.setTitle(params.get("titolo" + i));
                }
                section.setDescription(params.get("par" + i));
                list.add(section);
                i = i + 1;

            }
            DescriptionComponent description_component = new DescriptionComponent();
            description_component.setSectionsList(list);
            listComponent.add(description_component);
        }
        //Orari
        i = 1;
        int k = 1;

        EventsDateComponent workingtime = new EventsDateComponent();
        if (params.containsKey("WD" + i + "start" + k + "H")) {
            ArrayList<EventsDates> workingdays = new ArrayList<EventsDates>();

            while (params.containsKey("WD" + i)) {
                ArrayList<EventsHours> Listwh = new ArrayList<EventsHours>();
                k = 1;
                while (params.containsKey("WD" + i + "start" + k + "H")) {
                    EventsHours wh = new EventsHours();
                    wh.setStart(params.get("WD" + i + "start" + k + "H") + ":"
                            + params.get("WD" + i + "start" + k + "M"));
                    wh.setEnd(params.get("WD" + i + "end" + k + "H") + ":"
                            + params.get("WD" + i + "end" + k + "M"));
                    Listwh.add(wh);
                    k = k + 1;
                }
                EventsDates cwd = new EventsDates();
                cwd.setDate(params.get("WD" + i));
                cwd.setHours(Listwh);
                workingdays.add(cwd);
                i = i + 1;
            }

            workingtime.setDates(workingdays);
            listComponent.add(workingtime);
        }
        /*
        LinkedPoiComponent lpc = new LinkedPoiComponent();
        ArrayList<LinkedPoi> alp = new ArrayList<LinkedPoi>();
        i=1;
        while (params.containsKey("mot"+i)) {
         k=1;
         ArrayList<String> apoi = new ArrayList<String>();
         while(params.containsKey("COL"+i+"-"+k)){
             apoi.add(params.get("COL"+i+"-"+k));
             k++;
         }
         LinkedPoi lp= new LinkedPoi();
         lp.setDescription(params.get("mot"+i));
         lp.setIdlist(apoi);
         alp.add(lp);
         i++;
        }
        lpc.setLinked(alp);
        if(params.containsKey("mot1")) {
         listComponent.add(lpc);
        }
        */
        i = 1;
        if (params.containsKey("type" + i)) {

            PricesComponent pc = new PricesComponent();
            ArrayList<TicketPrice> tpList = new ArrayList<TicketPrice>();
            while (params.containsKey("type" + i)) {
                TicketPrice tp = new TicketPrice();
                tp.setType(params.get("type" + i));
                double dp = Double.parseDouble(params.get("price" + i));
                tp.setPrice(dp);
                tp.setType_description(params.get("typedesc" + i));
                tpList.add(tp);
                i = i + 1;
            }
            pc.setPrices(tpList);
            listComponent.add(pc);
        }

        i = 1;
        if (params.containsKey("SERV" + i)) {
            ArrayList<String> servList = new ArrayList<String>();
            while (params.containsKey("SERV" + i)) {
                servList.add(params.get("SERV" + i));
                i = i + 1;
            }
            ServicesComponent servicescomponent = new ServicesComponent();
            servicescomponent.setServicesList(servList);
            listComponent.add(servicescomponent);
        }

        poi.setComponents(listComponent);

        pm.savePoi(poi);

        CompletePOI poi2 = (CompletePOI) pm.findOneCompletePoiByName(poi.getName());
        // POI INGLESE 
        if (params.get("inglese").equals("true")) {
            addeng(params, poi2.getId(), coverimg, img_gallery);
        } else {
            EnCompletePOI enpoi = new EnCompletePOI();
            enpoi.setAddress(poi.getAddress());
            enpoi.setCategories(poi.getCategories());
            enpoi.setId(poi.getId());
            enpoi.setName(poi.getName());
            enpoi.setShortDescription(poi.getShortDescription());
            enpoi.setStart_date(poi.getStart_date());
            enpoi.setEnd_date(poi.getEnd_date());
            double enlat = Double.parseDouble(params.get("latitude"));
            double enlongi = Double.parseDouble(params.get("longitude"));
            poi.setLocation(new double[] { enlat, enlongi });
            enpoi.setComponents(listComponent);

            pm.saveEnPoi(enpoi);
        }

        for (int z = 0; z < files.length; z++) {
            MultipartFile file = files[z];

            try {
                byte[] bytes = file.getBytes();

                // Creating the directory to store file
                HttpSession session = request.getSession();
                ServletContext sc = session.getServletContext();

                File dir = new File(sc.getRealPath("/") + "dist" + File.separator + "poi" + File.separator
                        + "img" + File.separator + poi2.getId());
                if (!dir.exists())
                    dir.mkdirs();

                // Create the file on server
                File serverFile = new File(dir.getAbsolutePath() + File.separator + links.get(z).getLink());
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
                stream.write(bytes);
                stream.close();

            } catch (Exception e) {
                return model;
            }
        }
        MultipartFile file = cover;

        try {
            byte[] bytes = file.getBytes();

            // Creating the directory to store file
            HttpSession session = request.getSession();
            ServletContext sc = session.getServletContext();

            File dir = new File(sc.getRealPath("/") + "dist" + File.separator + "poi" + File.separator + "img"
                    + File.separator + poi2.getId());
            if (!dir.exists())
                dir.mkdirs();

            // Create the file on server
            File serverFile = new File(dir.getAbsolutePath() + File.separator + "cover.jpg");
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();

        } catch (Exception e) {
            return model;
        }
        return model;

    }
}

From source file:org.wso2.carbon.ui.CarbonSecuredHttpContext.java

/**
 * //ww w .  j a  v a  2 s.com
 * @param request
 * @param session
 * @param resourceURI
 * @return
 */
private boolean isCurrentUrlToBePassed(HttpServletRequest request, HttpSession session, String resourceURI) {

    if (!urlsToBeByPassed.isEmpty() && urlsToBeByPassed.containsKey(resourceURI)) {
        if (log.isDebugEnabled()) {
            log.debug("By passing authentication check for URI : " + resourceURI);
        }
        // Before bypassing, set the backendURL properly so that it doesn't fail
        String contextPath = request.getContextPath();
        String backendServerURL = request.getParameter("backendURL");
        if (backendServerURL == null) {
            backendServerURL = CarbonUIUtil.getServerURL(session.getServletContext(), request.getSession());
        }
        if ("/".equals(contextPath)) {
            contextPath = "";
        }
        backendServerURL = backendServerURL.replace("${carbon.context}", contextPath);
        session.setAttribute(CarbonConstants.SERVER_URL, backendServerURL);
        return true;
    }

    return false;
}

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

public void testDeleteFormOK() throws Exception {
    RESTFormService restService = new RESTFormService();
    FormDefinitionService formService = EasyMock.createMock(FormDefinitionService.class);
    formService.deleteForm(EasyMock.eq("somePackage"), EasyMock.eq("myFormId"));
    EasyMock.expectLastCall().once();/*w w  w  .j  a  v  a 2s .c  o m*/
    restService.setFormService(formService);
    ServletContext context = EasyMock.createMock(ServletContext.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(request.getSession()).andReturn(session);
    EasyMock.expect(session.getServletContext()).andReturn(context);

    EasyMock.replay(formService, context, session, request);
    Response resp = restService.deleteForm("somePackage", "myFormId", request);
    EasyMock.verify(formService, context, session, request);

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

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

public void testDeleteFormItemOK() throws Exception {
    RESTFormService restService = new RESTFormService();
    FormDefinitionService formService = EasyMock.createMock(FormDefinitionService.class);
    formService.deleteForm(EasyMock.eq("somePackage"), EasyMock.eq("MY_FORM_ID"));
    EasyMock.expectLastCall().once();/*from   w  ww  .  ja va 2  s . com*/
    ServletContext context = EasyMock.createMock(ServletContext.class);
    restService.setFormService(formService);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(request.getSession()).andReturn(session);
    EasyMock.expect(session.getServletContext()).andReturn(context);

    EasyMock.replay(formService, context, session, request);
    Response resp = restService.deleteForm("somePackage", "MY_FORM_ID", request);
    EasyMock.verify(formService, context, session, request);

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

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

public void testDeleteFormServiceProblem() throws Exception {
    RESTFormService restService = new RESTFormService();
    FormDefinitionService formService = EasyMock.createMock(FormDefinitionService.class);
    formService.deleteForm(EasyMock.eq("somePackage"), EasyMock.eq("myFormId"));
    FormServiceException exception = new FormServiceException("Something going wrong");
    EasyMock.expectLastCall().andThrow(exception).once();
    restService.setFormService(formService);
    ServletContext context = EasyMock.createMock(ServletContext.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(request.getSession()).andReturn(session);
    EasyMock.expect(session.getServletContext()).andReturn(context);

    EasyMock.replay(formService, context, session, request);
    Response resp = restService.deleteForm("somePackage", "myFormId", request);
    EasyMock.verify(formService, context, session, request);

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

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

public void testDeleteFormItemServiceProblem() throws Exception {
    RESTFormService restService = new RESTFormService();
    FormDefinitionService formService = EasyMock.createMock(FormDefinitionService.class);
    formService.deleteForm(EasyMock.eq("somePackage"), EasyMock.eq("MY_FORM_ID"));
    FormServiceException exception = new FormServiceException("Something going wrong");
    EasyMock.expectLastCall().andThrow(exception).once();
    ServletContext context = EasyMock.createMock(ServletContext.class);
    restService.setFormService(formService);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(request.getSession()).andReturn(session);
    EasyMock.expect(session.getServletContext()).andReturn(context);

    EasyMock.replay(formService, context, session, request);
    Response resp = restService.deleteForm("somePackage", "MY_FORM_ID", request);
    EasyMock.verify(formService, context, session, request);

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

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

public void testSaveFormEncodingProblem() throws Exception {
    RESTFormService restService = new RESTFormService();
    FormRepresentationDecoder decoder = EasyMock.createMock(FormRepresentationDecoder.class);
    FormEncodingFactory.register(FormEncodingServerFactory.getEncoder(), decoder);
    FormDefinitionService formService = EasyMock.createMock(FormDefinitionService.class);
    FormRepresentation form = RESTAbstractTest.createMockForm("formToBeSaved", "param1", "param2", "param3");
    FormEncodingException exception = new FormEncodingException("Something going wrong");
    EasyMock.expect(decoder.decode(EasyMock.anyObject(String.class))).andThrow(exception).once();
    restService.setFormService(formService);
    ServletContext context = EasyMock.createMock(ServletContext.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(request.getSession()).andReturn(session);
    EasyMock.expect(session.getServletContext()).andReturn(context);

    EasyMock.replay(formService, context, decoder, session, request);
    Response resp = restService.saveForm(FormEncodingFactory.getEncoder().encode(form), "somePackage", request);
    EasyMock.verify(formService, context, decoder, session, request);

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

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

public void testSaveFormServiceProblem() throws Exception {
    FormEncodingFactory.register(FormEncodingServerFactory.getEncoder(),
            FormEncodingServerFactory.getDecoder());
    RESTFormService restService = new RESTFormService();
    FormDefinitionService formService = EasyMock.createMock(FormDefinitionService.class);
    FormRepresentation form = RESTAbstractTest.createMockForm("formToBeSaved", "param1", "param2", "param3");
    FormServiceException exception = new FormServiceException("Something going wrong");
    EasyMock.expect(formService.saveForm(EasyMock.eq("somePackage"), EasyMock.eq(form))).andThrow(exception)
            .once();/*from   w w  w.  ja  va2  s.com*/
    restService.setFormService(formService);
    ServletContext context = EasyMock.createMock(ServletContext.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(request.getSession()).andReturn(session);
    EasyMock.expect(session.getServletContext()).andReturn(context);

    EasyMock.replay(formService, context, session, request);
    Response resp = restService.saveForm(FormEncodingFactory.getEncoder().encode(form), "somePackage", request);
    EasyMock.verify(formService, context, session, request);

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

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

public void testSaveFormItemServiceProblem() throws Exception {
    RESTFormService restService = new RESTFormService();
    FormDefinitionService formService = EasyMock.createMock(FormDefinitionService.class);
    FormItemRepresentation item = RESTAbstractTest.createMockForm("formToBeSaved", "param1").getFormItems()
            .iterator().next();// ww w  . j  a  va  2 s.  c o  m
    FormServiceException exception = new FormServiceException("Something going wrong");
    EasyMock.expect(formService.saveFormItem(EasyMock.eq("somePackage"), EasyMock.eq("MY_FORM_ITEM_ID"),
            EasyMock.eq(item))).andThrow(exception).once();
    restService.setFormService(formService);
    ServletContext context = EasyMock.createMock(ServletContext.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(request.getSession()).andReturn(session);
    EasyMock.expect(session.getServletContext()).andReturn(context);

    EasyMock.replay(formService, context, session, request);
    Response resp = restService.saveFormItem(FormEncodingFactory.getEncoder().encode(item), "somePackage",
            "MY_FORM_ITEM_ID", request);
    EasyMock.verify(formService, context, session, request);

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