Access / Read resource file from REST/SERVELET

Posted: September 29, 2014 in web

String path = request.getServletContext().getRealPath(PDF_PATH);

It will search from standalone/deployments of your server. I’m using jboss so I will have this valuse below.

C:/server/jboss-as-7.1.1.Final/standalone/deployments/carApp.war/resources/template/sample.pdf

Dont forget to add the HttpServletRequest attribute.

 


@Path("/rest/")
public class DocumentRestService {

private static final String PDF_PATH = "/resources/template/sample.pdf";

@GET
 @Path("/generate/{carId}")
 @Produces("application/pdf")
 public Response getDocument(@PathParam("carId") String carId, @Context HttpServletRequest request)
 throws IOException, DocumentException {

String path = request.getServletContext().getRealPath(PDF_PATH);

PdfReader reader = new PdfReader(path);

ByteArrayOutputStream out = new ByteArrayOutputStream();

PdfStamper pdfStamper = new PdfStamper(reader, out);
 AcroFields acroFields = pdfStamper.getAcroFields();
 for (String field : acroFields.getFields().keySet()) {
 acroFields.setField(field, field);
 }
 pdfStamper.close();
 reader.close();
 ResponseBuilder response = Response.ok(out.toByteArray());
 return response.build();
 }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s