Example usage for javax.servlet ServletResponse setContentType

List of usage examples for javax.servlet ServletResponse setContentType

Introduction

In this page you can find the example usage for javax.servlet ServletResponse setContentType.

Prototype


public void setContentType(String type);

Source Link

Document

Sets the content type of the response being sent to the client, if the response has not been committed yet.

Usage

From source file:de.betterform.agent.web.filter.XFormsFilter.java

/**
 * The actual filtering method/*from   w w w .  j av  a2  s .  c om*/
 * todo: add request attribute to set baseURI 
 *
 * @see http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/Filter.html#doFilter(javax.servlet.ServletRequest,%20javax.servlet.ServletResponse,%20javax.servlet.FilterChain)
 */
public void doFilter(ServletRequest srvRequest, ServletResponse srvResponse, FilterChain filterChain)
        throws IOException, ServletException {

    //ensure correct Request encoding
    if (srvRequest.getCharacterEncoding() == null) {
        srvRequest.setCharacterEncoding(defaultRequestEncoding);
    }

    HttpServletRequest request = (HttpServletRequest) srvRequest;
    HttpServletResponse response = (HttpServletResponse) srvResponse;
    HttpSession session = request.getSession(true);

    /*
    if header "betterform-internal is present this means that the internal http client of the processor is the
    originator of the incoming request. Since this may not be processed we skip these requests by
    simply calling the servlet chain.
            
    As the contenttype may not be present for such requests we have to determine the mimetype by the file ending
    of the requested resource.
    */

    //Check if  MEDIATYPE is set if so we will handle response as XForms
    if (request.getHeader(BetterFORMConstants.BETTERFORM_INTERNAL) != null) {
        LOG.warn("Request from internal betterForm HTTP Client arrived in XFormsFilter");
        String requestURI = request.getRequestURI();

        String mimeType = webFactory.getServletContext().getMimeType(requestURI);

        if (LOG.isDebugEnabled()) {
            LOG.debug("request URI: " + requestURI);
            LOG.debug("mimeType: " + mimeType);
        }

        if (mimeType != null) {
            srvResponse.setContentType(mimeType);
        } else {
            LOG.warn("no contenttype set for internal request");
            //                throw new ServletException("Contenttype of " + requestURI + " unknown. Please configure your webcontainer appropriately.");
        }

        filterChain.doFilter(srvRequest, srvResponse);
        return;
    }

    if (request.getParameter("source") != null) {
        srvResponse.setContentType("text/plain");
        // override setContentType to keep "text/plain" as content type.
        HttpServletResponseWrapper resp = new HttpServletResponseWrapper((HttpServletResponse) srvResponse) {
            public void setContentType(String s) {
                return;
            }
        };
        filterChain.doFilter(srvRequest, resp);
        return;
    }

    if (request.getParameter("isUpload") != null) {
        //Got an upload...
        handleUpload(request, response, session);
        //TODO: XFORMS  PROCESSING: do we need to exit?
    } else if ("GET".equalsIgnoreCase(request.getMethod())
            && request.getParameter(BetterFORMConstants.SUBMISSION_RESPONSE) != null) {
        doSubmissionReplaceAll(request, response);
    } else if ("GET".equalsIgnoreCase(request.getMethod())
            && request.getParameter(BetterFORMConstants.SUBMISSION_RESPONSE_XFORMS) != null) {
        doSubmissionReplaceAllXForms(request, response, session);
    } else {
        /* do servlet request */
        LOG.info("Passing to Chain");
        BufferedHttpServletResponseWrapper bufResponse = new BufferedHttpServletResponseWrapper(
                (HttpServletResponse) srvResponse);
        filterChain.doFilter(srvRequest, bufResponse);
        LOG.info("Returned from Chain");

        //handleResponse(srvResponse, request, response, session, bufResponse, webFactory);

        // response is contains no betterFORM relevant content so it is not buffered and cannot be processed
        if (!bufResponse.isBuffered()) {
            return;
        }

        // response is already committed to the client, so nothing is to
        // be done
        if (bufResponse.isCommitted()) {
            return;
        }

        //pass to request object
        request.setAttribute(WebFactory.USER_AGENT, XFormsFilter.USERAGENT);

        /* dealing with response from chain */
        if (handleResponseBody(request, bufResponse)) {
            byte[] data = prepareData(bufResponse);
            if (data.length > 0) {
                request.setAttribute(WebFactory.XFORMS_INPUTSTREAM, new ByteArrayInputStream(data));
            }
        }

        if (handleRequestAttributes(request)) {
            bufResponse.getOutputStream().close();
            LOG.info("Start Filter XForm");
            processXForms(request, response, session);
            LOG.info("End Render XForm");
        } else {
            srvResponse.getOutputStream().write(bufResponse.getData());
            srvResponse.getOutputStream().close();
        }
    }
}

From source file:org.toobsframework.servlet.filters.compression.CompressionFilter.java

/**
 * The filter intercepts the querystring for this request
 * and will reject the request if any of the filtered characters are found
 *
 * @param request The servlet request object
 * @param response The servlet response object
 * @param chain The servlet chain/*from  w  w  w. ja v  a  2  s.c o  m*/
 *
 * @throws ServletException
 */
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (log.isDebugEnabled()) {
        log.debug("Begin [doFilter]");
    }

    request.setCharacterEncoding("UTF-8");

    String query = ((HttpServletRequest) request).getQueryString();
    boolean noFilter = (query != null && query.indexOf("DisableWhiteSpace") != -1);
    boolean compressible = false;
    //long startTime = 0L;
    if (!filterEnabled || noFilter) { // || ((HttpServletRequest)request).getRequestURI().endsWith("application.do")
        chain.doFilter(request, response);
    } else {
        if (filterCompress) {
            Enumeration<String> headers = ((HttpServletRequest) request).getHeaders("Accept-Encoding");
            while (headers.hasMoreElements()) {
                String value = (String) headers.nextElement();
                if (value.indexOf(compressionType) != -1) {
                    compressible = true;
                    break;
                }
            }
        }
        if (smartCompress) {
            for (int i = 0; i < excludedHosts.size(); i++) {
                if (("" + ((HttpServletRequest) request).getRemoteAddr())
                        .startsWith((String) excludedHosts.get(i))) {
                    compressible = false;
                }
            }
        }
        FilterResponseWrapper wrapper = new FilterResponseWrapper((HttpServletResponse) response);
        chain.doFilter(request, wrapper);
        byte[] input = wrapper.getData();
        byte[] output = null;

        OutputStream servletoutputstream = null;
        String content = wrapper.getContentType();
        if (content != null && content.toUpperCase().indexOf("TEXT/") >= 0) {
            response.setContentType(content);
            if (filterCompress && compressible) {
                HttpServletResponse httpResponse = (HttpServletResponse) response;
                httpResponse.addHeader("Content-Encoding", compressionType);
                servletoutputstream = new GZIPOutputStream(response.getOutputStream(), bufferSize);
            } else {
                servletoutputstream = response.getOutputStream();
            }
            if (filterWhitespace) {
                output = new byte[input.length];
                int i = 0;
                int o = 0;
                boolean htComment = false;
                boolean inScript = false;
                while (i < input.length) {
                    if (input[i] == 13 || input[i] == 9) { // 13=CR, 9=TAB
                        // nothing
                    } else if (input[i] == 10) { // 10=LF
                        if (inScript) {
                            output[o] = input[i];
                            o++;
                        }
                    } else if (input[i] == 32) { // 32=Space
                        if (!htComment && !inScript && i + 1 < input.length && input[i + 1] == 32) {
                            i++;
                            while (i + 1 < input.length && input[i + 1] == 32) {
                                i++;
                            }
                        } else if (!htComment) {
                            output[o] = input[i];
                            o++;
                        }
                    } else if (input[i] == 60) { // 60=<
                        if (!inScript && (i + 2 < input.length) && (input[i + 1] == 33)
                                && (input[i + 2] == 45)) {
                            i += 3;
                            htComment = true;
                        } else if (!htComment && i + 2 < input.length
                                && (input[i + 1] == 83 || input[i + 1] == 115)
                                && (input[i + 2] == 67 || input[i + 2] == 99)) {
                            inScript = true;
                            output[o] = input[i];
                            output[o + 1] = input[i + 1];
                            output[o + 2] = input[i + 2];
                            o += 3;
                            i += 2;
                        } else if (!htComment && i + 3 < input.length && (input[i + 1] == 47)
                                && (input[i + 2] == 83 || input[i + 2] == 115)
                                && (input[i + 3] == 67 || input[i + 3] == 99)) {
                            inScript = false;
                            output[o] = input[i];
                            output[o + 1] = input[i + 1];
                            output[o + 2] = input[i + 2];
                            output[o + 3] = input[i + 3];
                            o += 4;
                            i += 3;
                        } else if (!htComment) {
                            output[o] = input[i];
                            o++;
                        }
                    } else if (input[i] == 62) { // 62 = >
                        if (htComment && input[i - 1] == 45 && input[i - 2] == 45) {
                            htComment = false;
                        } else if (!htComment) {
                            output[o] = input[i];
                            o++;
                        }
                    } else if (!htComment) {
                        output[o] = input[i];
                        o++;
                    }
                    i++;
                }
                servletoutputstream.write(output, 0, o);
            } else {
                servletoutputstream.write(input);
            }
        } else {
            servletoutputstream = response.getOutputStream();
            servletoutputstream.write(input);
        }

        if (servletoutputstream != null) {
            servletoutputstream.flush();
            servletoutputstream.close();
        }
        input = null;
        output = null;
    }
    if (log.isDebugEnabled()) {
        log.debug("End [doFilter]");
    }
}

From source file:com.flexive.war.servlet.CeFileUpload.java

@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse)
        throws ServletException, IOException {
    String renderContent = null;/*w ww  .  j a  v a2  s  . c  om*/
    try {

        final HttpServletRequest request = (HttpServletRequest) servletRequest;
        final BeContentEditorBean ceb = null; // = ContentEditorBean.getSingleton().getInstance(request);

        if (ceb == null) {
            renderContent = "No Content Editor Bean is active";
        } else {

            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();

            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);

            // Parse the request
            List /* FileItem */ items = upload.parseRequest(request);

            BinaryDescriptor binary = null;

            String xpath = null;
            for (Object item1 : items) {
                FileItem item = (FileItem) item1;
                if (item.isFormField()) {
                    if (item.getFieldName().equalsIgnoreCase("result")) {
                        renderContent = item.getString().replaceAll("\\\\n", "\\\n");
                    } else if (item.getFieldName().equalsIgnoreCase("xpath")) {
                        xpath = item.getString();
                    }
                } else {
                    InputStream uploadedStream = null;
                    try {
                        uploadedStream = item.getInputStream();
                        String name = item.getName();
                        if (name.indexOf('\\') > 0)
                            name = name.substring(name.lastIndexOf('\\') + 1);
                        binary = new BinaryDescriptor(name, item.getSize(), uploadedStream);
                    } finally {
                        if (uploadedStream != null)
                            uploadedStream.close();
                    }
                }
                //                    System.out.println("Item: " + item.getName());
            }

            //FxContent co = ceb.getContent();
            FxBinary binProperty = new FxBinary(binary);
            //co.setValue(xpath, binProperty);
            //ceb.getContentEngine().prepareSave(co);
        }
    } catch (Throwable t) {
        System.err.println(t.getMessage());
        t.printStackTrace();
        renderContent = t.getMessage();
    }

    // Render the result
    PrintWriter w = servletResponse.getWriter();
    if (renderContent == null) {
        renderContent = "No content";
    }
    w.print(renderContent);
    w.close();
    servletResponse.setContentType("text/html");
    servletResponse.setContentLength(renderContent.length());
    ((HttpServletResponse) servletResponse).setStatus(HttpServletResponse.SC_OK);
}

From source file:org.bonitasoft.forms.server.filter.BPMURLSupportFilter.java

@Override
@SuppressWarnings("unchecked")
public void doFilter(final ServletRequest request, final ServletResponse response,
        final FilterChain filterChain) throws ServletException, IOException {
    try {/*from   ww  w.j  a  v a  2s .  c om*/
        final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        final HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        final Map<String, String[]> parameters = new HashMap<String, String[]>(
                httpServletRequest.getParameterMap());
        final List<String> supportedParameterKeysList = Arrays.asList(FORM_LOCALE_URL_PARAM, TENANT_PARAM,
                UI_MODE_PARAM, THEME_PARAM, GWT_DEBUG_PARAM, TOKEN_URL_PARAM, AUTO_LOGIN_PARAM);
        final Set<String> parameterKeys = new HashSet<String>(parameters.keySet());
        parameterKeys.removeAll(supportedParameterKeysList);
        if (!parameterKeys.isEmpty()) {
            if (parameterKeys.contains(PROCESS_NAME_PARAM)) {
                final long processDefinitionID = getProcessIDOfLatestVersion(parameters.get(PROCESS_NAME_PARAM),
                        httpServletRequest);
                if (processDefinitionID != -1) {
                    final String[] parameterValues = new String[1];
                    parameterValues[0] = String.valueOf(processDefinitionID);
                    parameters.put(PROCESS_PARAM, parameterValues);
                }
                parameters.remove(PROCESS_NAME_PARAM);
            }
            if (!parameterKeys.contains(FORM_ID_PARAM) && isForm(httpServletRequest)) {
                final String formID[] = getFormIDFromRegularURLParameters(parameters, httpServletRequest);
                if (formID != null && formID.length > 0) {
                    parameters.put(FORM_ID_PARAM, formID);
                }
            }
            final StringBuilder hashString = new StringBuilder();
            final StringBuilder queryString = new StringBuilder();
            for (final Entry<String, String[]> parameter : parameters.entrySet()) {
                final String key = parameter.getKey();
                final String[] values = parameter.getValue();
                if (supportedParameterKeysList.contains(key)) {
                    buildQueryString(queryString, key, values);
                } else {
                    buildQueryString(hashString, key, values);
                }
            }
            final StringBuilder redirectionURL = new StringBuilder();
            redirectionURL.append(httpServletRequest.getRequestURI());
            if (queryString.length() > 0) {
                redirectionURL.append("?");
                redirectionURL.append(queryString);
            }
            if (hashString.length() > 0) {
                redirectionURL.append("#");
                redirectionURL.append(hashString);
            }
            final String encodeRedirectURL = httpServletResponse.encodeRedirectURL(redirectionURL.toString());
            httpServletResponse.sendRedirect(encodeRedirectURL);
        } else {
            response.setContentType(CharEncoding.UTF_8);
            filterChain.doFilter(request, response);
        }
    } catch (final Exception e) {
        LOGGER.log(Level.SEVERE, "Error while parsing the regular parameters into hash parameters.");
        throw new ServletException(e);
    }
}

From source file:org.b3log.solo.filter.PageCacheFilter.java

/**
 * Try to write response from cache.//  w  ww .  j ava  2s.  c o m
 *
 * @param request the specified request
 * @param response the specified response
 * @param chain filter chain
 * @throws IOException io exception
 * @throws ServletException servlet exception
 */
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {
    final long startTimeMillis = System.currentTimeMillis();
    request.setAttribute(Keys.HttpRequest.START_TIME_MILLIS, startTimeMillis);

    final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    final String requestURI = httpServletRequest.getRequestURI();
    LOGGER.log(Level.FINER, "Request URI[{0}]", requestURI);

    if (StaticResources.isStatic(httpServletRequest)) {
        final String path = httpServletRequest.getServletPath() + httpServletRequest.getPathInfo();
        LOGGER.log(Level.FINEST, "Requests a static resource, forwards to servlet[path={0}]", path);
        request.getRequestDispatcher(path).forward(request, response);

        return;
    }

    if (!Latkes.isPageCacheEnabled()) {
        LOGGER.log(Level.FINEST, "Page cache is disabled");
        chain.doFilter(request, response);

        return;
    }

    final String skinDirName = (String) httpServletRequest.getAttribute(Keys.TEMAPLTE_DIR_NAME);
    if ("mobile".equals(skinDirName)) {
        // Mobile request, bypasses page caching
        chain.doFilter(request, response);

        return;
    }

    String pageCacheKey;
    final String queryString = httpServletRequest.getQueryString();
    pageCacheKey = (String) request.getAttribute(Keys.PAGE_CACHE_KEY);
    if (Strings.isEmptyOrNull(pageCacheKey)) {
        pageCacheKey = PageCaches.getPageCacheKey(requestURI, queryString);
        request.setAttribute(Keys.PAGE_CACHE_KEY, pageCacheKey);
    }

    final JSONObject cachedPageContentObject = PageCaches.get(pageCacheKey, httpServletRequest,
            (HttpServletResponse) response);

    if (null == cachedPageContentObject) {
        LOGGER.log(Level.FINER, "Page cache miss for request URI[{0}]", requestURI);
        chain.doFilter(request, response);

        return;
    }

    final String cachedType = cachedPageContentObject.optString(PageCaches.CACHED_TYPE);

    try {
        // If cached an article that has view password, dispatches the password form
        if (langPropsService.get(PageTypes.ARTICLE.getLangeLabel()).equals(cachedType)
                && cachedPageContentObject.has(PageCaches.CACHED_PWD)) {
            JSONObject article = new JSONObject();

            final String articleId = cachedPageContentObject.optString(PageCaches.CACHED_OID);

            article.put(Keys.OBJECT_ID, articleId);
            article.put(Article.ARTICLE_VIEW_PWD, cachedPageContentObject.optString(PageCaches.CACHED_PWD));

            if (articles.needViewPwd(httpServletRequest, article)) {
                article = articleRepository.get(articleId); // Loads the article entity

                final HttpServletResponse httpServletResponse = (HttpServletResponse) response;
                try {
                    httpServletResponse.sendRedirect(Latkes.getServePath() + "/console/article-pwd"
                            + articles.buildArticleViewPwdFormParameters(article));
                    return;
                } catch (final Exception e) {
                    httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
                    return;
                }
            }
        }
    } catch (final Exception e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
        chain.doFilter(request, response);
    }

    try {
        LOGGER.log(Level.FINEST, "Writes resposne for page[pageCacheKey={0}] from cache", pageCacheKey);
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        final PrintWriter writer = response.getWriter();
        String cachedPageContent = cachedPageContentObject.getString(PageCaches.CACHED_CONTENT);
        final String topBarHTML = TopBars.getTopBarHTML((HttpServletRequest) request,
                (HttpServletResponse) response);
        cachedPageContent = cachedPageContent.replace(Common.TOP_BAR_REPLACEMENT_FLAG, topBarHTML);

        final String cachedTitle = cachedPageContentObject.getString(PageCaches.CACHED_TITLE);
        LOGGER.log(Level.FINEST, "Cached value[key={0}, type={1}, title={2}]",
                new Object[] { pageCacheKey, cachedType, cachedTitle });

        statistics.incBlogViewCount((HttpServletRequest) request, (HttpServletResponse) response);

        final long endimeMillis = System.currentTimeMillis();
        final String dateString = DateFormatUtils.format(endimeMillis, "yyyy/MM/dd HH:mm:ss");
        final String msg = String.format("<!-- Cached by B3log Solo(%1$d ms), %2$s -->",
                endimeMillis - startTimeMillis, dateString);
        LOGGER.finer(msg);
        cachedPageContent += Strings.LINE_SEPARATOR + msg;
        writer.write(cachedPageContent);
        writer.flush();
        writer.close();
    } catch (final JSONException e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
        chain.doFilter(request, response);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
        chain.doFilter(request, response);
    } catch (final ServiceException e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
        chain.doFilter(request, response);
    }
}

From source file:org.apache.unomi.web.ContextServlet.java

@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
    final Date timestamp = new Date();
    if (request.getParameter("timestamp") != null) {
        timestamp.setTime(Long.parseLong(request.getParameter("timestamp")));
    }/*ww  w . j  av a2  s .com*/
    // first we must retrieve the context for the current visitor, and build a Javascript object to attach to the
    // script output.
    String profileId;

    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    String httpMethod = httpServletRequest.getMethod();
    //        logger.debug(HttpUtils.dumpRequestInfo(httpServletRequest));

    // set up CORS headers as soon as possible so that errors are not misconstrued on the client for CORS errors
    HttpUtils.setupCORSHeaders(httpServletRequest, response);

    if ("options".equals(httpMethod.toLowerCase())) {
        response.flushBuffer();
        return;
    }

    Profile profile = null;

    String cookieProfileId = null;
    Cookie[] cookies = httpServletRequest.getCookies();
    for (Cookie cookie : cookies) {
        if (profileIdCookieName.equals(cookie.getName())) {
            cookieProfileId = cookie.getValue();
        }
    }

    Session session = null;

    String personaId = request.getParameter("personaId");
    if (personaId != null) {
        PersonaWithSessions personaWithSessions = profileService.loadPersonaWithSessions(personaId);
        if (personaWithSessions == null) {
            logger.error("Couldn't find persona with id=" + personaId);
            profile = null;
        } else {
            profile = personaWithSessions.getPersona();
            session = personaWithSessions.getLastSession();
        }
    }

    String sessionId = request.getParameter("sessionId");

    if (cookieProfileId == null && sessionId == null && personaId == null) {
        ((HttpServletResponse) response).sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    boolean profileCreated = false;

    ContextRequest contextRequest = null;
    String scope = null;
    String stringPayload = HttpUtils.getPayload(httpServletRequest);
    if (stringPayload != null) {
        ObjectMapper mapper = CustomObjectMapper.getObjectMapper();
        JsonFactory factory = mapper.getFactory();
        try {
            contextRequest = mapper.readValue(factory.createParser(stringPayload), ContextRequest.class);
        } catch (Exception e) {
            logger.error("Cannot read payload " + stringPayload, e);
            return;
        }
        scope = contextRequest.getSource().getScope();
    }

    int changes = EventService.NO_CHANGE;

    if (profile == null) {
        if (sessionId != null) {
            session = profileService.loadSession(sessionId, timestamp);
            if (session != null) {
                profileId = session.getProfileId();
                profile = profileService.load(profileId);
                profile = checkMergedProfile(response, profile, session);
            }
        }
        if (profile == null) {
            // profile not stored in session
            if (cookieProfileId == null) {
                // no profileId cookie was found, we generate a new one and create the profile in the profile service
                profile = createNewProfile(null, response, timestamp);
                profileCreated = true;
            } else {
                profile = profileService.load(cookieProfileId);
                if (profile == null) {
                    // this can happen if we have an old cookie but have reset the server,
                    // or if we merged the profiles and somehow this cookie didn't get updated.
                    profile = createNewProfile(null, response, timestamp);
                    profileCreated = true;
                    HttpUtils.sendProfileCookie(profile, response, profileIdCookieName, profileIdCookieDomain);
                } else {
                    profile = checkMergedProfile(response, profile, session);
                }
            }
        } else if ((cookieProfileId == null || !cookieProfileId.equals(profile.getItemId()))
                && !profile.isAnonymousProfile()) {
            // profile if stored in session but not in cookie
            HttpUtils.sendProfileCookie(profile, response, profileIdCookieName, profileIdCookieDomain);
        }
        // associate profile with session
        if (sessionId != null && session == null) {
            session = new Session(sessionId, profile, timestamp, scope);
            changes |= EventService.SESSION_UPDATED;
            Event event = new Event("sessionCreated", session, profile, scope, null, session, timestamp);

            event.getAttributes().put(Event.HTTP_REQUEST_ATTRIBUTE, request);
            event.getAttributes().put(Event.HTTP_RESPONSE_ATTRIBUTE, response);
            logger.debug("Received event " + event.getEventType() + " for profile=" + profile.getItemId()
                    + " session=" + session.getItemId() + " target=" + event.getTarget() + " timestamp="
                    + timestamp);
            changes |= eventService.send(event);
        }
    }

    if (profileCreated) {
        changes |= EventService.PROFILE_UPDATED;

        Event profileUpdated = new Event("profileUpdated", session, profile, scope, null, profile, timestamp);
        profileUpdated.setPersistent(false);
        profileUpdated.getAttributes().put(Event.HTTP_REQUEST_ATTRIBUTE, request);
        profileUpdated.getAttributes().put(Event.HTTP_RESPONSE_ATTRIBUTE, response);

        logger.debug("Received event {} for profile={} {} target={} timestamp={}",
                profileUpdated.getEventType(), profile.getItemId(),
                session != null ? " session=" + session.getItemId() : "", profileUpdated.getTarget(),
                timestamp);
        changes |= eventService.send(profileUpdated);
    }

    ContextResponse data = new ContextResponse();
    data.setProfileId(profile.isAnonymousProfile() ? cookieProfileId : profile.getItemId());

    if (privacyService.isRequireAnonymousBrowsing(profile.getItemId())) {
        profile = privacyService.getAnonymousProfile();
        session.setProfile(profile);
        changes = EventService.SESSION_UPDATED;
    }

    if (contextRequest != null) {
        changes |= handleRequest(contextRequest, profile, session, data, request, response, timestamp);
    }

    if ((changes & EventService.PROFILE_UPDATED) == EventService.PROFILE_UPDATED && profile != null) {
        profileService.save(profile);
    }
    if ((changes & EventService.SESSION_UPDATED) == EventService.SESSION_UPDATED && session != null) {
        profileService.saveSession(session);
    }

    String extension = httpServletRequest.getRequestURI()
            .substring(httpServletRequest.getRequestURI().lastIndexOf(".") + 1);
    boolean noScript = "json".equals(extension);
    String contextAsJSONString = CustomObjectMapper.getObjectMapper().writeValueAsString(data);
    Writer responseWriter;
    if (noScript) {
        response.setCharacterEncoding("UTF-8");
        responseWriter = response.getWriter();
        response.setContentType("application/json");
        IOUtils.write(contextAsJSONString, responseWriter);
    } else {
        responseWriter = response.getWriter();
        responseWriter.append("window.digitalData = window.digitalData || {};\n").append("var cxs = ")
                .append(contextAsJSONString).append(";\n");

        // now we copy the base script source code
        InputStream baseScriptStream = getServletContext().getResourceAsStream(
                profile instanceof Persona ? IMPERSONATE_BASE_SCRIPT_LOCATION : BASE_SCRIPT_LOCATION);
        IOUtils.copy(baseScriptStream, responseWriter);
    }

    responseWriter.flush();
}

From source file:net.yacy.http.servlets.SolrSelectServlet.java

@Override
public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException {

    HttpServletRequest hrequest = (HttpServletRequest) request;
    HttpServletResponse hresponse = (HttpServletResponse) response;
    SolrQueryRequest req = null;//from  w w  w .ja  v  a 2s .c  om

    final Method reqMethod = Method.getMethod(hrequest.getMethod());

    Writer out = null;
    try {
        // prepare request to solr
        MultiMapSolrParams mmsp = SolrRequestParsers.parseQueryString(hrequest.getQueryString());

        Switchboard sb = Switchboard.getSwitchboard();
        // TODO: isUserInRole needs a login to jetty container (not done automatically on admin from localhost)
        boolean authenticated = hrequest.isUserInRole(UserDB.AccessRight.ADMIN_RIGHT.toString());

        // count remote searches if this was part of a p2p search
        if (mmsp.getMap().containsKey("partitions")) {
            final int partitions = mmsp.getInt("partitions", 30);
            sb.searchQueriesGlobal += 1.0f / partitions; // increase query counter
        }

        // get the ranking profile id
        int profileNr = mmsp.getInt("profileNr", 0);

        // rename post fields according to result style
        String querystring = "";
        if (!mmsp.getMap().containsKey(CommonParams.Q) && mmsp.getMap().containsKey(CommonParams.QUERY)) {
            querystring = mmsp.get(CommonParams.QUERY, "");
            mmsp.getMap().remove(CommonParams.QUERY);
            QueryModifier modifier = new QueryModifier(0);
            querystring = modifier.parse(querystring);
            modifier.apply(mmsp);
            QueryGoal qg = new QueryGoal(querystring);
            StringBuilder solrQ = qg.collectionTextQuery();
            mmsp.getMap().put(CommonParams.Q, new String[] { solrQ.toString() }); // sru patch

            // experimental p2p enrichment if flag to do so is set
            /*
            final String p2pQuery = querystring;
            new Thread() {
            @Override
            public void run() {
                FederateSearchManager.getManager().query(p2pQuery);
            }
            }.start();
            */
        }
        String q = mmsp.get(CommonParams.Q, "");
        if (querystring.length() == 0)
            querystring = q;
        if (!mmsp.getMap().containsKey(CommonParams.START)) {
            int startRecord = mmsp.getFieldInt("startRecord", null, 0);
            mmsp.getMap().remove("startRecord");
            mmsp.getMap().put(CommonParams.START, new String[] { Integer.toString(startRecord) }); // sru patch
        }
        if (!mmsp.getMap().containsKey(CommonParams.ROWS)) {
            int maximumRecords = mmsp.getFieldInt("maximumRecords", null, 10);
            mmsp.getMap().remove("maximumRecords");
            mmsp.getMap().put(CommonParams.ROWS, new String[] { Integer.toString(maximumRecords) }); // sru patch
        }
        mmsp.getMap().put(CommonParams.ROWS, new String[] { Integer
                .toString(Math.min(mmsp.getInt(CommonParams.ROWS, 10), (authenticated) ? 100000000 : 100)) });

        // set ranking according to profile number if ranking attributes are not given in the request
        Ranking ranking = sb.index.fulltext().getDefaultConfiguration().getRanking(profileNr);
        if (!mmsp.getMap().containsKey(CommonParams.SORT) && !mmsp.getMap().containsKey(DisMaxParams.BQ)
                && !mmsp.getMap().containsKey(DisMaxParams.BF) && !mmsp.getMap().containsKey("boost")) {
            if (!mmsp.getMap().containsKey("defType"))
                mmsp.getMap().put("defType", new String[] { "edismax" });
            String fq = ranking.getFilterQuery();
            String bq = ranking.getBoostQuery();
            String bf = ranking.getBoostFunction();
            if (fq.length() > 0)
                mmsp.getMap().put(CommonParams.FQ, new String[] { fq });
            if (bq.length() > 0)
                mmsp.getMap().put(DisMaxParams.BQ, StringUtils.split(bq, "\t\n\r\f")); // bq split into multiple query params, allowing space in single query
            if (bf.length() > 0)
                mmsp.getMap().put("boost", new String[] { bf }); // a boost function extension, see http://wiki.apache.org/solr/ExtendedDisMax#bf_.28Boost_Function.2C_additive.29
        }

        // get a response writer for the result
        String wt = mmsp.get(CommonParams.WT, "xml"); // maybe use /solr/select?q=*:*&start=0&rows=10&wt=exml
        QueryResponseWriter responseWriter = RESPONSE_WRITER.get(wt);
        if (responseWriter == null)
            throw new ServletException("no response writer");
        if (responseWriter instanceof OpensearchResponseWriter) {
            // set the title every time, it is possible that it has changed
            final String promoteSearchPageGreeting = (sb
                    .getConfigBool(SwitchboardConstants.GREETING_NETWORK_NAME, false))
                            ? sb.getConfig("network.unit.description", "")
                            : sb.getConfig(SwitchboardConstants.GREETING, "");
            ((OpensearchResponseWriter) responseWriter).setTitle(promoteSearchPageGreeting);
        }

        // if this is a call to YaCys special search formats, enhance the query with field assignments
        if ((responseWriter instanceof YJsonResponseWriter
                || responseWriter instanceof OpensearchResponseWriter)
                && "true".equals(mmsp.get("hl", "true"))) {
            // add options for snippet generation
            if (!mmsp.getMap().containsKey("hl.q"))
                mmsp.getMap().put("hl.q", new String[] { q });
            if (!mmsp.getMap().containsKey("hl.fl"))
                mmsp.getMap().put("hl.fl",
                        new String[] { CollectionSchema.description_txt.getSolrFieldName() + ","
                                + CollectionSchema.h4_txt.getSolrFieldName() + ","
                                + CollectionSchema.h3_txt.getSolrFieldName() + ","
                                + CollectionSchema.h2_txt.getSolrFieldName() + ","
                                + CollectionSchema.h1_txt.getSolrFieldName() + ","
                                + CollectionSchema.text_t.getSolrFieldName() });
            if (!mmsp.getMap().containsKey("hl.alternateField"))
                mmsp.getMap().put("hl.alternateField",
                        new String[] { CollectionSchema.description_txt.getSolrFieldName() });
            if (!mmsp.getMap().containsKey("hl.simple.pre"))
                mmsp.getMap().put("hl.simple.pre", new String[] { "<b>" });
            if (!mmsp.getMap().containsKey("hl.simple.post"))
                mmsp.getMap().put("hl.simple.post", new String[] { "</b>" });
            if (!mmsp.getMap().containsKey("hl.fragsize"))
                mmsp.getMap().put("hl.fragsize",
                        new String[] { Integer.toString(SearchEvent.SNIPPET_MAX_LENGTH) });
            if (!mmsp.getMap().containsKey("fl"))
                mmsp.getMap().put("fl",
                        new String[] { CollectionSchema.sku.getSolrFieldName() + "," + CollectionSchema.title
                                + "," + CollectionSchema.description_txt.getSolrFieldName() + ","
                                + CollectionSchema.id.getSolrFieldName() + ","
                                + CollectionSchema.url_paths_sxt.getSolrFieldName() + ","
                                + CollectionSchema.last_modified.getSolrFieldName() + ","
                                + CollectionSchema.size_i.getSolrFieldName() + ","
                                + CollectionSchema.url_protocol_s.getSolrFieldName() + ","
                                + CollectionSchema.url_file_ext_s.getSolrFieldName() });
        }

        // get the embedded connector
        String requestURI = hrequest.getRequestURI();
        boolean defaultConnector = (requestURI.startsWith("/solr/" + WebgraphSchema.CORE_NAME)) ? false
                : requestURI.startsWith("/solr/" + CollectionSchema.CORE_NAME)
                        || mmsp.get("core", CollectionSchema.CORE_NAME).equals(CollectionSchema.CORE_NAME);
        mmsp.getMap().remove("core");
        SolrConnector connector = defaultConnector ? sb.index.fulltext().getDefaultEmbeddedConnector()
                : sb.index.fulltext().getEmbeddedConnector(WebgraphSchema.CORE_NAME);
        if (connector == null) {
            connector = defaultConnector ? sb.index.fulltext().getDefaultConnector()
                    : sb.index.fulltext().getConnectorForRead(WebgraphSchema.CORE_NAME);
        }
        if (connector == null)
            throw new ServletException("no core");

        // add default queryfield parameter according to local ranking config (or defaultfield)
        if (ranking != null) { // ranking normally never null
            final String qf = ranking.getQueryFields();
            if (qf.length() > 4) { // make sure qf has content (else use df)
                addParam(DisMaxParams.QF, qf, mmsp.getMap()); // add QF that we set to be best suited for our index
                // TODO: if every peer applies a decent QF itself, this can be reverted to getMap().put()
            } else {
                mmsp.getMap().put(CommonParams.DF, new String[] { CollectionSchema.text_t.getSolrFieldName() });
            }
        } else {
            mmsp.getMap().put(CommonParams.DF, new String[] { CollectionSchema.text_t.getSolrFieldName() });
        }

        // do the solr request, generate facets if we use a special YaCy format
        final SolrQueryResponse rsp;
        if (connector instanceof EmbeddedSolrConnector) {
            req = ((EmbeddedSolrConnector) connector).request(mmsp);
            rsp = ((EmbeddedSolrConnector) connector).query(req);

            // prepare response
            hresponse.setHeader("Cache-Control", "no-cache, no-store");
            HttpCacheHeaderUtil.checkHttpCachingVeto(rsp, hresponse, reqMethod);

            // check error
            if (rsp.getException() != null) {
                AccessTracker.addToDump(querystring, "0", new Date());
                sendError(hresponse, rsp.getException());
                return;
            }

            NamedList<?> values = rsp.getValues();
            DocList r = ((ResultContext) values.get("response")).docs;
            int numFound = r.matches();
            AccessTracker.addToDump(querystring, Integer.toString(numFound), new Date());

            // write response header
            final String contentType = responseWriter.getContentType(req, rsp);
            if (null != contentType)
                response.setContentType(contentType);

            if (Method.HEAD == reqMethod) {
                return;
            }

            // write response body
            if (responseWriter instanceof BinaryResponseWriter) {
                ((BinaryResponseWriter) responseWriter).write(response.getOutputStream(), req, rsp);
            } else {
                out = new FastWriter(
                        new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8));
                responseWriter.write(out, req, rsp);
                out.flush();
            }
        } else {
            // write a 'faked' response using a call to the backend
            SolrDocumentList sdl = connector.getDocumentListByQuery(mmsp.getMap().get(CommonParams.Q)[0],
                    mmsp.getMap().get(CommonParams.SORT) == null ? null
                            : mmsp.getMap().get(CommonParams.SORT)[0],
                    Integer.parseInt(mmsp.getMap().get(CommonParams.START)[0]),
                    Integer.parseInt(mmsp.getMap().get(CommonParams.ROWS)[0]),
                    mmsp.getMap().get(CommonParams.FL));
            OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream());
            EnhancedXMLResponseWriter.write(osw, req, sdl);
            osw.close();
        }
    } catch (final Throwable ex) {
        sendError(hresponse, ex);
    } finally {
        if (req != null) {
            req.close();
        }
        SolrRequestInfo.clearRequestInfo();
        if (out != null)
            try {
                out.close();
            } catch (final IOException e1) {
            }
    }
}