@Override public Representation getRepresentation(MediaType defaultMediaType, int timeToLive) { return new FileRepresentation(getFile(), defaultMediaType, timeToLive); }
/** * Constructor. If a positive "timeToLive" parameter is given, then the * expiration date is set accordingly. If "timeToLive" is equal to zero, * then the expiration date is set to the current date, meaning that it will * immediately expire on the client. If -1 is given, then no expiration date * is set. * * @param file * The represented file. * @param mediaType * The representation's media type. * @param timeToLive * The time to live before it expires (in seconds). */ public FileRepresentation(File file, MediaType mediaType, int timeToLive) { super(mediaType); this.file = file; setModificationDate(new Date(file.lastModified())); if (timeToLive == 0) { setExpirationDate(null); } else if (timeToLive > 0) { setExpirationDate(new Date(System.currentTimeMillis() + (1000L * timeToLive))); } setMediaType(mediaType); Disposition disposition = new Disposition(); disposition.setFilename(file.getName()); this.setDisposition(disposition); }
/** * Constructor. * * @param path * The path name or file URI of the represented file (either in * system format or in 'file:///' format). * @param mediaType * The representation's media type. * @param timeToLive * The time to live before it expires (in seconds). * @see java.io.File#File(String) */ public FileRepresentation(String path, MediaType mediaType, int timeToLive) { this(createFile(path), mediaType, timeToLive); }
FileRepresentation fileRepresentation = new FileRepresentation(file, mediaType); fileRepresentation.setDisposition(disposition);
@Override public ResponseEntity uploadOctetStream(String urn, java.io.File file, MediaType mediaType) throws ServiceException { ResponseEntity response; FileRepresentation fileRepresentation = new FileRepresentation(file, mediaType); fileRepresentation.setMediaType(MediaType.APPLICATION_OCTET_STREAM); ClientResource service = createClient(FileEndpoints.uploadContentsAsOctetStream(urn)); service.accept(MediaType.APPLICATION_JSON); try { Representation result = service.post(fileRepresentation); JsonRepresentation jsonRepresentation = new JsonRepresentation(result); JSONObject jsonResult = jsonRepresentation.getJsonObject(); response = JsonUtil.fromJson(jsonResult, ResponseEntity.class); if (service.getStatus().equals(Status.SUCCESS_OK)) { LOGGER.info("Successfully uploaded file to path {}", FileEndpoints.uploadContentsAsMultipart(urn)); } else { LOGGER.error("Unexpected HTTP status code returned: {}", service.getStatus().getCode()); throw new ServiceException(response); } } catch (JSONException | IOException e) { LOGGER.error("Unexpected Exception", e); throw new ServiceException(e); } return response; }
FileRepresentation FRepre = new FileRepresentation(file, MediaType.TEXT_CSV); FRepre.getDisposition().setType(Disposition.TYPE_ATTACHMENT); FRepre.getDisposition().setFilename(file.getName());
public static FileRepresentation createRepForFile(File file) { MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap(); String mimeTypeStr = mimeTypesMap.getContentType(file); MediaType mt = new MediaType(mimeTypeStr); FileRepresentation fr = new FileRepresentation(file, mt); Disposition disp = new Disposition(Disposition.TYPE_ATTACHMENT); disp.setFilename(file.getName()); disp.setSize(file.length()); fr.setDisposition(disp); return fr; }
/** * Writes the representation to a byte channel. Optimizes using the file * channel transferTo method. * * @param writableChannel * A writable byte channel. */ @Override public void write(WritableByteChannel writableChannel) throws IOException { IoUtils.copy(getChannel(), writableChannel); }
@Get public Representation getKMLLayer() { try { String layerId = ( String ) getRequest().getAttributes().get( "layer" ); int kmlId = Integer.parseInt( ( String) getRequest().getAttributes().get("kmlId")); String fileName = ( String ) getRequest().getAttributes().get("kmlFile"); JSONObject layer = _service.getLayerJSON(layerId); JSONObject kmlDef = layer.getJSONObject("public").getJSONArray("kml").getJSONObject(kmlId); String path = new File(new File(kmlDef.getString("dir")), fileName).getPath(); File kmlFile = new File(getClass().getClassLoader().getResource(path).toURI()); return new FileRepresentation(kmlFile, MediaType.APPLICATION_KML); } catch (Exception e) { throw new ResourceException( Status.SERVER_ERROR_INTERNAL, "Unable to load desired kml file", e ); } } }
/** This resource returns the swagger.json */ @Get("json") public String listResources() { final ServletContext servlet = (ServletContext) getContext().getAttributes().get("org.restlet.ext.servlet.ServletContext"); final String realPath = servlet.getRealPath("/"); final JacksonRepresentation<ApiDeclaration> result = new JacksonRepresentation<ApiDeclaration>( new FileRepresentation(realPath + "swagger.json", MediaType.APPLICATION_JSON), ApiDeclaration.class); try { return result.getText(); } catch (final IOException e) { LOGGER.warn("Error building swagger json", e); } return "Not Found: swagger.json"; } }
@Override protected Representation get() throws ResourceException { // Data String identity = ensureRequestAttribute( PARAM_IDENTITY, String.class, Status.CLIENT_ERROR_BAD_REQUEST ); // Context X509Context x509Context = newRootContext().x509Context( identity ); // Interaction EscrowedKeyPair ekp = x509Context.recover(); // Representation return new FileRepresentation( ekp.attachedFile(), MediaType.TEXT_PLAIN ); }
@Override protected Representation get() throws ResourceException { // Data String identity = ensureRequestAttribute( PARAM_IDENTITY, String.class, Status.CLIENT_ERROR_BAD_REQUEST ); // Context EscrowedKeyPairContext ekpCtx = newRootContext().escrowedKeyPairContext( identity ); // Interaction EscrowedKeyPair ekp = ekpCtx.escrowedKeyPair(); // Representation return new FileRepresentation( ekp.attachedFile(), MediaType.TEXT_PLAIN ); }
if (UmlgProperties.INSTANCE.isLoadUiResourcesFromFile()) { File umlgui2 = new File("./runtime/runtime-ui/src/main/webapp/resources/org/umlg/ui/umlgui2.html"); FileRepresentation fileRepresentation = new FileRepresentation(umlgui2, MediaType.APPLICATION_XHTML); return new TemplateRepresentation(fileRepresentation, dataModel, MediaType.TEXT_HTML); } else {
@Override protected Representation get() throws ResourceException { // Data String caIdentity = ensureRequestAttribute( PARAM_IDENTITY, String.class, Status.CLIENT_ERROR_BAD_REQUEST ); // Context RootContext rootCtx = newRootContext(); CAContext caCtx = rootCtx.caContext( caIdentity ); // Interaction CRL crl = caCtx.ca().crl().get(); // Representation return new FileRepresentation( crl.attachedFile(), MediaType.TEXT_PLAIN ); }
MediaType.TEXT_PLAIN)); } else if (source instanceof File) { result = new FileRepresentation((File) source, MediaType.getMostSpecific(target.getMediaType(), MediaType.APPLICATION_OCTET_STREAM));