public Representation represent(Variant variant) throws ResourceException { Representation representation; if (variant.getMediaType() == MediaType.APPLICATION_XML) { representation = new WriterRepresentation(MediaType.APPLICATION_XML) { public void write(Writer writer) throws IOException { XmlMarshaller.marshalDocument(writer,"script", makeDataModel()); } }; } else { representation = new WriterRepresentation(MediaType.TEXT_HTML) { public void write(Writer writer) throws IOException { ScriptResource.this.writeHtml(writer); } }; } // TODO: remove if not necessary in future? representation.setCharacterSet(CharacterSet.UTF_8); return representation; }
public Representation represent(Variant variant) throws ResourceException { if (appCtx == null) { throw new ResourceException(404); } Representation representation; if (variant.getMediaType() == MediaType.APPLICATION_XML) { representation = new WriterRepresentation(MediaType.APPLICATION_XML) { public void write(Writer writer) throws IOException { XmlMarshaller.marshalDocument(writer, "beans", makeDataModel()); } }; } else { representation = new WriterRepresentation( MediaType.TEXT_HTML) { public void write(Writer writer) throws IOException { BeanBrowseResource.this.writeHtml(writer); } }; } // TODO: remove if not necessary in future? representation.setCharacterSet(CharacterSet.UTF_8); return representation; }
Form form = new Form(); form.add("x", "foo"); form.add("y", "bar"); ClientResource resource = new ClientResource("http://localhost:8080/someresource"); Response response = resource.post(form.getWebRepresentation()); if (response.getStatus().isSuccess()) { System.out.println("Success! " + response.getStatus()); System.out.println(response.getEntity().getText()); } else { System.out.println("ERROR! " + response.getStatus()); System.out.println(response.getEntity().getText()); }
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)"); } } }
public JobRelatedResource(Context ctx, Request req, Response res) throws ResourceException { super(ctx, req, res); cj = getEngine().getJob((String)req.getAttributes().get("job")); if(cj==null) { throw new ResourceException(404); } }
er = (EditRepresentation) getVariants().get(0); } catch (ClassCastException cce) { throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "File modification should use either PUT or " + "POST with a '?format=textedit' query-string."); File file = er.getFileRepresentation().getFile(); try { FileUtils.writeStringToFile(file, newContents,"UTF-8");
@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); }
public ReportGenResource(Context ctx, Request req, Response res) throws ResourceException { super(ctx, req, res); getVariants().add(new Variant(MediaType.TEXT_PLAIN)); reportClass = (String)req.getAttributes().get("reportClass"); }
pw.println("<!DOCTYPE html>"); pw.println("<html>"); pw.println("<head><title>"+fileRepresentation.getFile().getName()+"</title>"); pw.println("<link rel='stylesheet' href='" + getStaticRef("codemirror/codemirror.css") + "'>"); pw.println("<link rel='stylesheet' href='" + getStaticRef("codemirror/util/dialog.css") + "'>"); pw.println("<form style='position:absolute;top:15px;bottom:15px;left:15px;right:15px;overflow:auto' method='POST'>"); pw.println("<textarea style='width:98%;height:90%;font-family:monospace' name='contents' id='editor'>"); StringEscapeUtils.escapeHtml(pw,fileRepresentation.getText()); pw.println("</textarea>"); pw.println("<div id='savebar'>"); pw.println("<input type='submit' value='save changes' id='savebutton'>"); pw.println(fileRepresentation.getFile()); Reference viewRef = dirResource.getRequest().getOriginalRef().clone(); viewRef.setQuery(null);
Variant v = iter.next(); if(v instanceof FileRepresentation) { File file = ((FileRepresentation)v).getFile(); if(getEnhDirectory().allowsEdit(file)) { iter.remove(); v.setCharacterSet(CharacterSet.UTF_8); iter.add(new EditRepresentation((FileRepresentation)v,this)); }; Variant v = iter.next(); if(v instanceof FileRepresentation) { File file = ((FileRepresentation)v).getFile(); if(getEnhDirectory().allowsPaging(file)) { iter.remove(); while(iter.hasNext()) { Variant v = iter.next(); v.setCharacterSet(CharacterSet.UTF_8);
/** * If client can accept text/html, always prefer it. WebKit-based browsers * claim to want application/xml, but we don't want to give it to them. See * <a href="https://webarchive.jira.com/browse/HER-1603">https://webarchive.jira.com/browse/HER-1603</a> */ public Variant getPreferredVariant() { boolean addExplicitTextHtmlPreference = false; for (Preference<MediaType> mediaTypePreference: getRequest().getClientInfo().getAcceptedMediaTypes()) { if (mediaTypePreference.getMetadata().equals(MediaType.TEXT_HTML)) { mediaTypePreference.setQuality(Float.MAX_VALUE); addExplicitTextHtmlPreference = false; break; } else if (mediaTypePreference.getMetadata().includes(MediaType.TEXT_HTML)) { addExplicitTextHtmlPreference = true; } } if (addExplicitTextHtmlPreference) { List<Preference<MediaType>> acceptedMediaTypes = getRequest().getClientInfo().getAcceptedMediaTypes(); acceptedMediaTypes.add(new Preference<MediaType>(MediaType.TEXT_HTML, Float.MAX_VALUE)); getRequest().getClientInfo().setAcceptedMediaTypes(acceptedMediaTypes); } return super.getPreferredVariant(); }
@Override public Reader getReader() throws IOException { StringWriter writer = new StringWriter((int)fileRepresentation.getSize()+100); write(writer); return new StringReader(writer.toString()); }
/** * Actually read the requested lines, and reverses if appropriate. * * If at file start, refuses to show fewer lines than are possible * ('bounces' against start). * * @throws IOException */ protected void loadLines() throws IOException { this.file = fileRepresentation.getFile(); this.lines = new LinkedList<String>(); this.range = FileUtils.pagedLines(file, position, lineCount, lines, 128); // bounce against the front of the file: don't show runt (fewer // lines than requested) unless absolutely necessary) if(lines.size()<Math.abs(lineCount) && range.getMinimumLong() == 0 && range.getMaximumLong()<file.length()) { this.lines = new LinkedList<String>(); this.range = FileUtils.pagedLines(file, 0, Math.abs(lineCount), lines, 128); } if(reversedOrder) { Collections.reverse(lines); } }
public Representation represent(Variant variant) throws ResourceException { if (cj == null) { throw new ResourceException(404); } Representation representation = null; if (variant.getMediaType() == MediaType.APPLICATION_XML) { representation = new WriterRepresentation(MediaType.APPLICATION_XML) { public void write(Writer writer) throws IOException { CrawlJobModel model = makeDataModel(); model.put("heapReport", getEngine().heapReportData()); XmlMarshaller.marshalDocument(writer, "job", model); } }; } else { representation = new WriterRepresentation(MediaType.TEXT_HTML) { public void write(Writer writer) throws IOException { JobResource.this.writeHtml(writer); } }; } // TODO: remove if not necessary in future? // honor requested charset? representation.setCharacterSet(CharacterSet.UTF_8); return representation; }
public void acceptRepresentation(Representation entity) throws ResourceException { if (appCtx == null) { throw new ResourceException(404); } // copy op? Form form = getRequest().getEntityAsForm(); beanPath = form.getFirstValue("beanPath"); String newVal = form.getFirstValue("newVal"); if(newVal!=null) { int i = beanPath.indexOf("."); String beanName = i<0?beanPath:beanPath.substring(0,i); Object namedBean = appCtx.getBean(beanName); BeanWrapperImpl bwrap = new BeanWrapperImpl(namedBean); String propPath = beanPath.substring(i+1); Object coercedVal = bwrap.convertIfNecessary(newVal, bwrap.getPropertyValue(propPath).getClass()); bwrap.setPropertyValue(propPath, coercedVal); } Reference ref = getRequest().getResourceRef(); ref.setPath(getBeansRefPath()); ref.addSegment(beanPath); getResponse().redirectSeeOther(ref); }
public ScriptResource(Context ctx, Request req, Response res) throws ResourceException { super(ctx, req, res); setModifiable(true); getVariants().add(new Variant(MediaType.TEXT_HTML)); getVariants().add(new Variant(MediaType.APPLICATION_XML)); Configuration tmpltCfg = new Configuration(); tmpltCfg.setClassForTemplateLoading(this.getClass(),""); tmpltCfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); setTemplateConfiguration(tmpltCfg); scriptingConsole = new ScriptingConsole(cj); } public void setTemplateConfiguration(Configuration tmpltCfg) {
public Representation represent(Variant variant) throws ResourceException { Representation representation; if (variant.getMediaType() == MediaType.APPLICATION_XML) { representation = new WriterRepresentation(MediaType.APPLICATION_XML) { public void write(Writer writer) throws IOException { XmlMarshaller.marshalDocument(writer, "engine", makeDataModel()); } }; } else { representation = new WriterRepresentation(MediaType.TEXT_HTML) { public void write(Writer writer) throws IOException { EngineResource.this.writeHtml(writer); } }; } // TODO: remove if not necessary in future? representation.setCharacterSet(CharacterSet.UTF_8); return representation; }
public EngineResource(Context ctx, Request req, Response res) { super(ctx, req, res); setModifiable(true); getVariants().add(new Variant(MediaType.TEXT_HTML)); getVariants().add(new Variant(MediaType.APPLICATION_XML)); Configuration tmpltCfg = new Configuration(); tmpltCfg.setClassForTemplateLoading(this.getClass(),""); tmpltCfg.setObjectWrapper(new DefaultObjectWrapper()); setTemplateConfiguration(tmpltCfg); }
public JobResource(Context ctx, Request req, Response res) throws ResourceException { super(ctx, req, res); setModifiable(true); getVariants().add(new Variant(MediaType.TEXT_HTML)); getVariants().add(new Variant(MediaType.APPLICATION_XML)); cj = getEngine().getJob( TextUtils.urlUnescape((String) req.getAttributes().get("job"))); Configuration tmpltCfg = new Configuration(); tmpltCfg.setClassForTemplateLoading(this.getClass(),""); tmpltCfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); setTemplateConfiguration(tmpltCfg); } public void setTemplateConfiguration(Configuration tmpltCfg) {
public BeanBrowseResource(Context ctx, Request req, Response res) throws ResourceException { super(ctx, req, res); getVariants().add(new Variant(MediaType.TEXT_HTML)); getVariants().add(new Variant(MediaType.APPLICATION_XML)); setModifiable(true); // accept POSTs appCtx = cj.getJobContext(); beanPath = (String)req.getAttributes().get("beanPath"); if (beanPath!=null) { try { beanPath = URLDecoder.decode(beanPath,"UTF-8"); } catch (UnsupportedEncodingException e) { // inconceivable! UTF-8 required all Java impls } } else { beanPath = ""; } Configuration tmpltCfg = new Configuration(); tmpltCfg.setClassForTemplateLoading(this.getClass(),""); tmpltCfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); setTemplateConfiguration(tmpltCfg); } public void setTemplateConfiguration(Configuration tmpltCfg) {