@Override public InputStream getContent() throws IOException, IllegalStateException { return entity.getStream(); }
/** * {@inheritDoc}<br> * * The stream of the underlying representation is wrapped with a new * instance of the {@link DigestInputStream} class, which allows to compute * progressively the digest value. */ @Override public InputStream getStream() throws IOException { return new DigestInputStream(getWrappedRepresentation().getStream(), this.computedDigest); }
/** * Delegates to {@link #read(InputStream)} passing it representation.getStream() */ @Override public final Object toObject(Representation representation) { try { return read( representation.getStream() ); } catch (IOException e) { throw new RuntimeException( e ); } }
/** * This implementation throws IllegalStateException if attempting to read * the underlying stream multiple times. */ public InputStream getInputStream() throws IOException, IllegalStateException { if (this.read && this.representation.isTransient()) { throw new IllegalStateException( "Representation has already been read and is transient."); } this.read = true; return this.representation.getStream(); }
@Override public InputStream getStream() throws IOException { return getWrappedRepresentation().getStream(); }
@Override public Object evaluate(String expression, QName returnType) throws Exception { Object result = null; final XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(this); final Document xmlDocument = getDocumentBuilder().parse( this.xmlRepresentation.getStream()); if (xmlDocument != null) { result = xpath.evaluate(expression, xmlDocument, returnType); } else { throw new Exception( "Unable to obtain a DOM document for the XML representation. " + "XPath evaluation cancelled."); } return result; }
/** * Returns the wrapped schema. * * @return The wrapped schema. * @throws IOException */ private static Schema getSchema(Representation schemaRepresentation) throws Exception { Schema result = null; if (schemaRepresentation != null) { final StreamSource streamSource = new StreamSource( schemaRepresentation.getStream()); result = SchemaFactory.newInstance( getSchemaLanguageUri(schemaRepresentation)).newSchema( streamSource); } return result; }
/** * Exhauts the content of the representation by reading it and silently * discarding anything read. * * @return The number of bytes consumed or -1 if unknown. */ public long exhaust() throws IOException { long result = -1L; if (isAvailable()) { result = ByteUtils.exhaust(getStream()); } return result; }
@Override public InputStream getResourceStream(String name) throws ResourceNotFoundException { InputStream result = null; try { Representation resultRepresentation = getStore().get(name); if (resultRepresentation == null) { resultRepresentation = this.defaultRepresentation; if (resultRepresentation == null) { throw new ResourceNotFoundException( "Could not locate resource '" + name + "'"); } else { result = resultRepresentation.getStream(); } } else { result = resultRepresentation.getStream(); } } catch (IOException ioe) { throw new ResourceNotFoundException(ioe); } return result; }
public InputStream getBaclavaStream() throws NotSuccessException, MediaTypeException, IOException { Response response = getBaclavaResponse(); return response.getEntity().getStream(); }
/** * Returns the wrapped DOM document. If no document is defined yet, it * attempts to parse the XML representation eventually given at construction * time. Otherwise, it just creates a new document. * * @return The wrapped DOM document. */ public Document getDocument() throws IOException { if (this.dom == null) { if (this.xmlRepresentation != null) { try { this.dom = getDocumentBuilder().parse( this.xmlRepresentation.getStream()); } catch (SAXException se) { throw new IOException( "Couldn't read the XML representation. " + se.getMessage()); } } else { this.dom = getDocumentBuilder().newDocument(); } } return this.dom; }
/** * Handles an upload using the EXTERNAL method. * * @param request * @throws IOException */ public static File handleEXTERNALUpload(Request request) throws IOException { //get the URL for this file to upload final InputStream inStream=request.getEntity().getStream(); final String stringURL=IOUtils.getStringFromStream(inStream); final URL fileURL=new URL(stringURL); final File inputFile= IOUtils.URLToFile(fileURL); if(inputFile!=null && inputFile.exists() && inputFile.canRead()) { return inputFile; } return null; }
/** * Constructor reading the object from a serialized representation. This * representation must have the proper media type: * "application/x-java-serialized-object". * * @param serializedRepresentation * The serialized representation. * @throws IOException * @throws ClassNotFoundException * @throws IllegalArgumentException */ @SuppressWarnings("unchecked") public ObjectRepresentation(Representation serializedRepresentation) throws IOException, ClassNotFoundException, IllegalArgumentException { super(MediaType.APPLICATION_JAVA_OBJECT); if (serializedRepresentation.getMediaType().equals( MediaType.APPLICATION_JAVA_OBJECT)) { final ObjectInputStream ois = new ObjectInputStream( serializedRepresentation.getStream()); this.object = (T) ois.readObject(); ois.close(); } else { throw new IllegalArgumentException( "The serialized representation must have this media type: " + MediaType.APPLICATION_JAVA_OBJECT.toString()); } }
public static File downloadFile( URL url, String targetFile ) throws IOException { OutputStream out = null; InputStream in = null; File downloadedFile = new File( targetFile ); try { Response response = sendMessage( url, Method.GET, null ); if ( !response.getStatus().isSuccess() ) { throw new FileNotFoundException( response.getStatus() + " - " + url ); } // if this is null then someone was getting really creative with the tests, but hey, we will let them... if ( downloadedFile.getParentFile() != null ) { downloadedFile.getParentFile().mkdirs(); } in = response.getEntity().getStream(); out = new BufferedOutputStream( new FileOutputStream( downloadedFile ) ); IOUtil.copy( in, out, 1024 ); } finally { IOUtil.close( in ); IOUtil.close( out ); } return downloadedFile; }
try { MessageDigest md = MessageDigest.getInstance(algorithm); DigestInputStream dis = new DigestInputStream(getStream(), md); ByteUtils.exhaust(dis); result = new Digest(algorithm, md.digest());
/** * Returns a SAX source. * * @param xmlRepresentation * The XML representation to wrap. * @return A SAX source. * @throws IOException */ public static SAXSource getSaxSource(Representation xmlRepresentation) throws IOException { SAXSource result = null; if (xmlRepresentation != null) { result = new SAXSource(new InputSource(xmlRepresentation .getStream())); if (xmlRepresentation.getIdentifier() != null) { result.setSystemId(xmlRepresentation.getIdentifier() .getTargetRef().toString()); } } return result; }
protected TileLayer deserializeAndCheckLayer(Request req, Response resp, boolean isPut) throws RestletException, IOException { // TODO UTF-8 may not always be right here String layerName = ServletUtils.URLDecode((String) req.getAttributes().get("layer"), "UTF-8"); String formatExtension = (String) req.getAttributes().get("extension"); InputStream is = req.getEntity().getStream(); // If appropriate, check whether this layer exists if (!isPut) { findTileLayer(layerName, layerDispatcher); } return deserializeAndCheckLayerInternal(layerName, formatExtension, is); }
/** * Constructor. * * @param xmlRepresentation * A source XML representation to parse. * @throws IOException */ public SaxRepresentation(Representation xmlRepresentation) { super(xmlRepresentation.getMediaType()); try { if (xmlRepresentation instanceof XmlRepresentation) { this.source = ((XmlRepresentation) xmlRepresentation) .getSaxSource(); } else { this.source = new SAXSource(new InputSource(xmlRepresentation .getStream())); } if (xmlRepresentation.getIdentifier() != null) { this.source.setSystemId(xmlRepresentation.getIdentifier() .getTargetRef().toString()); } } catch (IOException ioe) { ioe.printStackTrace(); } }
public static SyndFeed getFeed( String feedId ) throws IllegalArgumentException, MalformedURLException, FeedException, IOException { SyndFeedInput input = new SyndFeedInput(); Response response = RequestFacade.sendMessage( FEED_URL_PART + feedId, Method.GET ); Assert.assertTrue( "Expected content", response.getEntity().isAvailable()); SyndFeed feed = input.build( new XmlReader( response.getEntity().getStream() ) ); // sort it by date sortSyndEntryOrderByPublishedDate( feed ); return feed; }
protected Metadata downloadMetadataFromRepository(Gav gav, String repoId) throws IOException, XmlPullParserException { String url = String.format("%s%s%s/%s/%s/maven-metadata.xml", getBaseNexusUrl(), REPOSITORY_RELATIVE_URL, repoId, gav.getGroupId(), gav.getArtifactId()); Response response = null; try { response = RequestFacade.sendMessage(new URL(url), Method.GET, null); if (response.getStatus().isError()) { return null; } try (InputStream stream = response.getEntity().getStream()) { MetadataXpp3Reader metadataReader = new MetadataXpp3Reader(); return metadataReader.read(stream); } } finally { RequestFacade.releaseResponse(response); } }