@Override public Representation getRepresentation(Status status, Request request, Response response) { StringWriter st = new StringWriter(); PrintWriter pw = new PrintWriter(st); if(status.getCode()==404){ pw.append("<h1>Page not found</h1>\n"); pw.append("The page you are looking for does not exist. "+ "You may be able to recover by going " + "<a href='javascript:history.back();void(0);'>back</a>.\n"); } else{ pw.append("<h1>An error occurred</h1>\n"); pw.append( "You may be able to recover and try something " + "else by going " + "<a href='javascript:history.back();void(0);'>back</a>.\n"); if(status.getThrowable()!=null) { pw.append("<h2>Cause: "+ status.getThrowable().toString()+"</h2>\n"); pw.append("<pre>"); status.getThrowable().printStackTrace(pw); pw.append("</pre>"); } } pw.flush(); return new StringRepresentation(st.toString(),MediaType.TEXT_HTML); }
new StringRepresentation( e.getMessage() ).write( resp.getOutputStream() ); resp.getOutputStream().flush();
/** * Constructor. * * @param text * The string value. * @param mediaType * The media type. * @param language * The language. * @param characterSet * The character set. */ public StringRepresentation(CharSequence text, MediaType mediaType, Language language, CharacterSet characterSet) { super(mediaType); this.text = text; setMediaType(mediaType); if (language != null) { getLanguages().add(language); } setCharacterSet(characterSet); updateSize(); }
/** * Updates the expected size according to the current string value. */ protected void updateSize() { if (getText() != null) { try { if (getCharacterSet() != null) { setSize(getText().getBytes(getCharacterSet().getName()).length); } else { setSize(getText().getBytes().length); } } catch (UnsupportedEncodingException e) { Context.getCurrentLogger().log(Level.WARNING, "Unable to update size", e); setSize(UNKNOWN_SIZE); } } else { setSize(UNKNOWN_SIZE); } }
@Override public InputStream getStream() throws IOException { if (getText() != null) { if (getCharacterSet() != null) { return new ByteArrayInputStream(getText().getBytes( getCharacterSet().getName())); } return new ByteArrayInputStream(getText().getBytes()); } return null; }
@Override public void write(OutputStream outputStream) throws IOException { if (getText() != null) { OutputStreamWriter osw = null; if (getCharacterSet() != null) { osw = new OutputStreamWriter(outputStream, getCharacterSet() .getName()); } else { osw = new OutputStreamWriter(outputStream); } osw.write(getText()); osw.flush(); } }
public Representation represent(Variant variant) throws ResourceException { // generate report if (cj == null || cj.getCrawlController() == null) { throw new ResourceException(500); } File f = cj.getCrawlController().getStatisticsTracker().writeReportFile(reportClass); if (f==null) { throw new ResourceException(500); } // redirect String relative = JobResource.getHrefPath(f,cj); if(relative!=null) { getResponse().redirectSeeOther("../"+relative+"?m="+f.lastModified()); return new StringRepresentation(""); } else { return new StringRepresentation( "Report dumped to "+f.getAbsolutePath() +" (outside job directory)"); } } }
@Override public Representation toRepresentation(Object object) { return new StringRepresentation( object.toString(), mediaType ); }
/** * Returns a XMLRepresentation of the layer * * @param layer * @return */ public Representation getXMLRepresentation(TileLayer layer) { XStream xs = xmlConfig.getConfiguredXStream(new XStream()); String xmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xs.toXML(layer); return new StringRepresentation(xmlText, MediaType.TEXT_XML); }
/** * Sets a textual entity. * * @param value * The represented string. * @param mediaType * The representation's media type. */ public void setEntity(String value, MediaType mediaType) { setEntity(new StringRepresentation(value, mediaType)); }
/** * @param s The message to report this error to the user (will report mimetype as text/plain) * @param stat The Status to report to the client * @param e The actual Exception that occurred */ public RestletException(String s, Status stat, Throwable t){ super(t); init(new StringRepresentation(s + ":" + t.getMessage(), MediaType.TEXT_PLAIN), stat); }
/** * @param s The message to report this error to the user (will report mimetype as text/plain) * @param stat The Status to report to the client */ public RestletException(String s, Status stat){ init(new StringRepresentation(s, MediaType.TEXT_PLAIN), stat); }
/** * @param s The message to report this error to the user (will report mimetype as text/plain) * @param stat The Status to report to the client * @param e The actual Exception that occurred */ public RestletException(String s, Status stat, Exception e){ super(e); init(new StringRepresentation(s + ":" + e.getMessage(), MediaType.TEXT_PLAIN), stat); }
/** * Returns the form as a Web representation * (MediaType.APPLICATION_WWW_FORM). * * @param characterSet * The supported character encoding. * @return The form as a Web representation. */ public Representation getWebRepresentation(CharacterSet characterSet) { return new StringRepresentation(getQueryString(characterSet), MediaType.APPLICATION_WWW_FORM, null, characterSet); }
/** * @param s The message to report this error to the user (will report mimetype as text/plain) * @param stat The Status to report to the client */ public RestletException(String s, Status stat){ init(new StringRepresentation(s, MediaType.TEXT_PLAIN), stat); }
private Response request(Method method, Reference uri, String data, MediaType mediaType, MediaType accepts) throws NotSuccessException { return request(method, uri, new StringRepresentation(data, mediaType), accepts, true); }
@Override public void handleGet() { Representation representation = new StringRepresentation(String.valueOf(count), MediaType.TEXT_PLAIN); getResponse().setEntity(representation); } }
/** * Returns a representation of the list in the "text/uri-list" format. * * @return A representation of the list in the "text/uri-list" format. */ public Representation getTextRepresentation() { final StringBuilder sb = new StringBuilder(); if (getIdentifier() != null) { sb.append("# ").append(getIdentifier().toString()).append("\r\n"); } for (final Reference ref : this) { sb.append(ref.toString()).append("\r\n"); } return new StringRepresentation(sb.toString(), MediaType.TEXT_URI_LIST); }
public Object sendCommand( String service, String command ) throws NexusConnectionException { return this.sendMessage( Method.PUT, this.buildUrl( service, "command" ), new StringRepresentation( command, MediaType.TEXT_ALL ) ); }
@Override public Representation getRepresentation(Status status, Request request, Response response) { StringWriter st = new StringWriter(); PrintWriter pw = new PrintWriter(st); if(status.getCode()==404){ pw.append("<h1>Page not found</h1>\n"); pw.append("The page you are looking for does not exist. "+ "You may be able to recover by going " + "<a href='javascript:history.back();void(0);'>back</a>.\n"); } else{ pw.append("<h1>An error occurred</h1>\n"); pw.append( "You may be able to recover and try something " + "else by going " + "<a href='javascript:history.back();void(0);'>back</a>.\n"); if(status.getThrowable()!=null) { pw.append("<h2>Cause: "+ status.getThrowable().toString()+"</h2>\n"); pw.append("<pre>"); status.getThrowable().printStackTrace(pw); pw.append("</pre>"); } } pw.flush(); return new StringRepresentation(st.toString(),MediaType.TEXT_HTML); }