Example usage for javax.servlet.http HttpServletResponse setHeader

List of usage examples for javax.servlet.http HttpServletResponse setHeader

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse setHeader.

Prototype

public void setHeader(String name, String value);

Source Link

Document

Sets a response header with the given name and value.

Usage

From source file:com.thinkberg.moxo.dav.LockHandler.java

private void sendLockAcquiredResponse(HttpServletResponse response, Lock lock) throws IOException {
    response.setContentType("text/xml");
    response.setCharacterEncoding("UTF-8");
    response.setHeader(HEADER_LOCK_TOKEN, "<" + lock.getToken() + ">");

    Document propDoc = DocumentHelper.createDocument();
    Element propEl = propDoc.addElement(TAG_PROP, "DAV:");
    Element lockdiscoveryEl = propEl.addElement(TAG_LOCKDISCOVERY);

    lock.serializeToXml(lockdiscoveryEl);

    XMLWriter xmlWriter = new XMLWriter(response.getWriter());
    xmlWriter.write(propDoc);/*w w  w .j  a  v a  2  s .  c om*/
    log(propDoc);
}

From source file:technology.tikal.gae.service.template.RestControllerTemplate.java

@ExceptionHandler(NotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)//from   w ww.j  a v a2  s .  com
public BasicErrorMessage handleException(NotValidException ex, HttpServletRequest request,
        HttpServletResponse response) {
    response.setHeader("Content-Type", "application/json;charset=UTF-8");
    BasicErrorMessage result = new BasicErrorMessage();
    BindingResult detail = ex.getDetails();
    String[] msg = new String[detail.getAllErrors().size()];
    String[][] code = new String[detail.getAllErrors().size()][];
    String[][] args = new String[detail.getAllErrors().size()][];
    int index = 0;
    Locale locale = LocaleContextHolder.getLocale();
    for (ObjectError x : detail.getAllErrors()) {
        Object[] tmpArgs = resolveArgumentMessage(x.getCodes(), x.getArguments());
        msg[index] = getValidationMessage(x.getDefaultMessage(), x.getCodes(), tmpArgs, locale);
        code[index] = x.getCodes();
        String[] tmp = new String[tmpArgs.length];
        args[index] = tmp;
        int j = 0;
        for (Object y : tmpArgs) {
            args[index][j] = y.toString();
            j = j + 1;
        }
        index = index + 1;
    }
    result.setType(ex.getClass().getSimpleName());
    result.setMessage(msg);
    result.setCode(code);
    result.setArguments(args);
    return result;
}

From source file:com.cloudbees.demo.beesshop.service.MongoDbFileStorageService.java

public void serveFile(String photoId, String id, HttpServletResponse response) throws IOException {

    GridFSDBFile file = gridFS.findOne(new ObjectId(id));
    response.setContentType(file.getContentType());
    response.setHeader("cache-control", "public, max-age=" + TimeUnit.SECONDS.convert(365, TimeUnit.DAYS));
    try (InputStream in = file.getInputStream()) {
        ByteStreams.copy(in, response.getOutputStream());
    }/* w w  w  . jav  a 2 s  .co  m*/

}

From source file:inti.ws.spring.resource.ByteWebResource.java

/**
 * Sets the MIME-Type as contentType./*from   w  w w . ja  v a  2 s .  co  m*/
 */
@Override
public void processResponse(HttpServletRequest request, HttpServletResponse response,
        ConfigParserSettings settings) {
    super.processResponse(request, response, settings);

    if (contentType != null) {
        response.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
    }
}

From source file:de.science.hack.jetstream.web.controller.DownloadController.java

private OutputStream prepareResponse(HttpServletResponse response) throws IOException {
    response.setContentType(MIME);// ww  w.  j  a  va  2s. c  o m
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", GLOBE);
    response.setHeader(headerKey, headerValue);
    return response.getOutputStream();
}

From source file:com.indeed.imhotep.web.QueryMetadata.java

public void setPendingHeaders(HttpServletResponse resp) {
    for (QueryMetadataItem queryMetadataItem : items) {
        if (queryMetadataItem.sendPending) {
            resp.setHeader(queryMetadataItem.name, queryMetadataItem.value);
            queryMetadataItem.markSent();
        }/*from www.ja v  a 2  s .  c  o  m*/
    }
}

From source file:ee.sk.hwcrypto.demo.controller.ViewController.java

@RequestMapping("/downloadContainer")
public void downloadContainer(HttpServletResponse response) {
    Container container = session.getContainer();
    String fileName = container.getDataFiles().get(0).getName() + ".bdoc";
    response.setContentType(CONTAINER_MIME_TYPE);
    response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
    try {/*from  ww  w  . j  a  va  2s  .  co  m*/
        InputStream containerStream = container.saveAsStream();
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copy(containerStream, outputStream);
        response.flushBuffer();
    } catch (IOException e) {
        log.error("Error Writing file content to output stream", e);
    }
}

From source file:no.dusken.aranea.web.control.BannerController.java

/**
 * Handles load requests for ads//from   www  . j  a va 2 s  .c o  m
 *
 * @param request  The HttpServletRequest
 * @param response The HttpServletResponse
 * @return ModelAndView with a map containing the retrieved ads
 */
public ModelAndView handleBannerLoad(HttpServletRequest request, HttpServletResponse response) {
    Map<String, Object> map = new HashMap<String, Object>();

    // banners does change often. Allow three hours caching
    response.setHeader("Cache-Control", "max-age=10800");

    // get the banner locations
    List<BannerLocation> bls = bannerLocationService.getBannerLocations();
    Set<Banner> ads = new HashSet<Banner>();
    for (BannerLocation bl : bls) {
        ads.add(bl.getBanner());
    }
    //the fishes are watching
    if (!ads.isEmpty()) {
        map.put("ads", ads);
    } else {
        log.info("No ads to put in map");
    }

    return new ModelAndView("no/dusken/aranea/base/web/ad/view", map);
}

From source file:com.happyuno.controller.EventController.java

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 *///from  w  w  w  .j  a  v a2  s. c  o m
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
    request.setCharacterEncoding("utf-8");
    response.setHeader("Cache-Control", "no-cache");

    int playerId = Integer.parseInt(request.getParameter("playerId").trim());
    int gameId = Integer.parseInt(request.getParameter("tableId").trim());

    ServletContext application = request.getSession().getServletContext();
    HashMap<Integer, GameTable> gameTableMap = (HashMap<Integer, GameTable>) application
            .getAttribute("gameTableMap");
    //?
    Game game = gameTableMap.get(gameId).getCurrentGame();

    ArrayList<JSONObject> eventsAL = game.getEventCenter().getEvents(game.getPlayerFromId(playerId));

    JSONArray events = new JSONArray();
    for (int i = 0; i < eventsAL.size(); ++i) {
        events.put(eventsAL.get(i));
    }

    response.getWriter().write(events.toString());
}

From source file:de.undercouch.gradle.tasks.download.ContentEncodingTest.java

@Override
protected Handler[] makeHandlers() throws IOException {
    ContextHandler contentEncodingHandler = new ContextHandler("/" + CONTENT_ENCODING) {
        @Override/*from   ww  w.  ja v  a 2 s .c om*/
        public void handle(String target, HttpServletRequest request, HttpServletResponse response,
                int dispatch) throws IOException, ServletException {
            response.setStatus(200);
            if (contentEncoding != null) {
                response.setHeader("Content-Encoding", contentEncoding);
            }
            PrintWriter rw = response.getWriter();
            rw.write("ce: " + String.valueOf(contentEncoding));
            rw.close();
        }
    };
    return new Handler[] { contentEncodingHandler };
}