private void handleOpenArticle( Request request, HttpServletResponse httpServletResponse, String target) throws Exception { try { int k1 = target.indexOf('/', 1); int k2 = target.indexOf('/', k1 + 1); String feedId = target.substring(k1 + 1, k2); String strSeq = target.substring(k2 + 1); int seq = Integer.parseInt(strSeq); Article article = articleDb.get(feedId, seq); LoginInfo loginInfo = userHelpers.getLoginInfo(request); // ttt2 using the link from a non-authenticated browser causes a NPE; maybe do something // better, e.g. sign up ReadArticlesColl readArticlesColl = readArticlesCollDb.get(loginInfo.userId, feedId); if (readArticlesColl == null) { readArticlesColl = new ReadArticlesColl(loginInfo.userId, feedId); } if (!readArticlesColl.isRead(seq)) { readArticlesColl.markRead(seq, Config.getConfig().maxSizeForReadArticles); readArticlesCollDb.add(readArticlesColl); } String s = URIUtil.encodePath(article.url) .replace("%3F", "?") .replace("%23", "#"); // ttt2 see how to do this right httpServletResponse.sendRedirect(s); } catch (Exception e) { WebUtils.showResult( String.format("Failed to get article for path %s. %s", target, e), "/", request, httpServletResponse); } }
/** * Get the resource list as a HTML directory listing. * * @param base The base URL * @param parent True if the parent directory should be included * @return String of HTML */ public String getListHTML(String base, boolean parent) throws IOException { base = URIUtil.canonicalPath(base); if (base == null || !isDirectory()) return null; String[] ls = list(); if (ls == null) return null; Arrays.sort(ls); String decodedBase = URIUtil.decodePath(base); String title = "Directory: " + deTag(decodedBase); StringBuilder buf = new StringBuilder(4096); buf.append("<HTML><HEAD>"); buf.append("<LINK HREF=\"") .append("jetty-dir.css") .append("\" REL=\"stylesheet\" TYPE=\"text/css\"/><TITLE>"); buf.append(title); buf.append("</TITLE></HEAD><BODY>\n<H1>"); buf.append(title); buf.append("</H1>\n<TABLE BORDER=0>\n"); if (parent) { buf.append("<TR><TD><A HREF=\""); buf.append(URIUtil.addPaths(base, "../")); buf.append("\">Parent Directory</A></TD><TD></TD><TD></TD></TR>\n"); } String encodedBase = hrefEncodeURI(base); DateFormat dfmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); for (int i = 0; i < ls.length; i++) { Resource item = addPath(ls[i]); buf.append("\n<TR><TD><A HREF=\""); String path = URIUtil.addPaths(encodedBase, URIUtil.encodePath(ls[i])); buf.append(path); if (item.isDirectory() && !path.endsWith("/")) buf.append(URIUtil.SLASH); // URIUtil.encodePath(buf,path); buf.append("\">"); buf.append(deTag(ls[i])); buf.append(" "); buf.append("</A></TD><TD ALIGN=right>"); buf.append(item.length()); buf.append(" bytes </TD><TD>"); buf.append(dfmt.format(new Date(item.lastModified()))); buf.append("</TD></TR>"); } buf.append("</TABLE>\n"); buf.append("</BODY></HTML>\n"); return buf.toString(); }
@Override public void sendRedirect(String location) throws IOException { if (isIncluding()) return; if (location == null) throw new IllegalArgumentException(); if (!URIUtil.hasScheme(location)) { StringBuilder buf = _channel.getRequest().getRootURL(); if (location.startsWith("/")) buf.append(location); else { String path = _channel.getRequest().getRequestURI(); String parent = (path.endsWith("/")) ? path : URIUtil.parentPath(path); location = URIUtil.addPaths(parent, location); if (location == null) throw new IllegalStateException("path cannot be above root"); if (!location.startsWith("/")) buf.append('/'); buf.append(location); } location = buf.toString(); HttpURI uri = new HttpURI(location); String path = uri.getDecodedPath(); String canonical = URIUtil.canonicalPath(path); if (canonical == null) throw new IllegalArgumentException(); if (!canonical.equals(path)) { buf = _channel.getRequest().getRootURL(); buf.append(URIUtil.encodePath(canonical)); String param = uri.getParam(); if (param != null) { buf.append(';'); buf.append(param); } String query = uri.getQuery(); if (query != null) { buf.append('?'); buf.append(query); } String fragment = uri.getFragment(); if (fragment != null) { buf.append('#'); buf.append(fragment); } location = buf.toString(); } } resetBuffer(); setHeader(HttpHeader.LOCATION, location); setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); complete(); }
/** @param url The url with a war scheme */ @Override public URLConnection openConnection(URL url) throws IOException { // remove the war scheme. URL actual = new URL(url.toString().substring("war:".length())); // let's do some basic tests: see if this is a folder or not. // if it is a folder. we will try to support it. if (actual.getProtocol().equals("file")) { File file = new File(URIUtil.encodePath(actual.getPath())); if (file.exists()) { if (file.isDirectory()) { // TODO (not mandatory for rfc66 though) } } } // if (actual.toString().startsWith("file:/") && ! actual.to) URLConnection ori = (URLConnection) actual.openConnection(); ori.setDefaultUseCaches(Resource.getDefaultUseCaches()); JarURLConnection jarOri = null; try { if (ori instanceof JarURLConnection) { jarOri = (JarURLConnection) ori; } else { jarOri = (JarURLConnection) new URL("jar:" + actual.toString() + "!/").openConnection(); jarOri.setDefaultUseCaches(Resource.getDefaultUseCaches()); } Manifest mf = WarBundleManifestGenerator.createBundleManifest( jarOri.getManifest(), url, jarOri.getJarFile()); try { jarOri.getJarFile().close(); jarOri = null; } catch (Throwable t) { } return new WarURLConnection(actual, mf); } finally { if (jarOri != null) try { jarOri.getJarFile().close(); } catch (Throwable t) { } } }
public void stop(String test, int count, int of) throws IOException { System.err.println("Stop " + test + " " + count + " of " + of); String stop = "GET " + URIUtil.encodePath("/benchmark/stop/ " + test + " " + count + " of " + of) + " HTTP/1.1\r\n" + "Host: benchmarkControl:8080\r\n" + "Connection: close\r\n" + "\r\n"; SocketChannel control = SocketChannel.open(new InetSocketAddress("localhost", 8080)); control.write(BufferUtil.toBuffer(stop)); while (control.isOpen()) { BufferUtil.clear(responseBuf); int pos = BufferUtil.flipToFill(responseBuf); if (control.read(responseBuf) == -1) control.close(); BufferUtil.flipToFlush(responseBuf, pos); } for (int i = 0; i < client.length; i++) if (client[i] != null && client[i].isOpen()) client[i].close(); client = null; }