Java tutorial
/* * Copyright (C) 2012 Dario Scoppelletti, <http://www.scoppelletti.it/>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package it.scoppelletti.wui; import java.io.*; import java.net.*; import java.util.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import org.restlet.data.*; import org.restlet.representation.*; import org.restlet.resource.*; import org.slf4j.*; import org.springframework.context.*; import org.springframework.web.context.support.*; import org.springframework.web.util.*; import it.scoppelletti.programmerpower.io.*; import it.scoppelletti.programmerpower.web.rest.*; import it.scoppelletti.programmerpower.wui.*; /** * Emette il contributo di tutte le applicazioni all’elemento * <CODE><head></CODE> delle pagine. */ public final class HeadTag extends SimpleTagSupport { private static final Logger myLogger = LoggerFactory.getLogger(HeadTag.class); /** * Percorso della risorsa REST che rappresenta il contributo di * un’applicazione all’elemento <CODE><head></CODE> delle * pagine. Il valore della costante è <CODE>{@value}</CODE>. */ public static final String HEAD_PATH = "/rest/head"; /** * Percorso della risorsa incorporata che rappresenta il contributo di * un’applicazione all’elemento <CODE><head></CODE> delle * pagine. Il valore della costante è <CODE>{@value}</CODE>. */ public static final String HEAD_RES = "WEB-INF/it-scoppelletti-head.html"; /** * Costruttore. */ public HeadTag() { } /** * Implementazione. */ @Override public void doTag() throws IOException, JspException { URI uri; UriComponentsBuilder uriBuilder; WebApplicationManager applMgr; List<String> applList; ApplicationContext applCtx; PageContext pageCtx = (PageContext) getJspContext(); applCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(pageCtx.getServletContext()); applMgr = applCtx.getBean(WebApplicationManager.BEAN_NAME, WebApplicationManager.class); applList = applMgr.listRunningApplications(); for (String ctxPath : applList) { uriBuilder = UriComponentsBuilder.fromHttpUrl(applMgr.getBaseUrl()); uriBuilder.path(ctxPath).path(HeadTag.HEAD_PATH); uriBuilder.queryParam(AbstractServerResource.QUERY_LOCALE, pageCtx.getRequest().getLocale().toString()); uri = uriBuilder.build().toUri(); try { head(uri, pageCtx.getOut()); } catch (Exception ex) { myLogger.error(String.format("Failed to get %1$s.", uri), ex); } } } /** * Emette il contributo di un’applicazione all’elemento * <CODE><head></CODE>. * * @param uri Risorsa REST. * @param writer Flusso di scrittura. */ private void head(URI uri, JspWriter writer) throws IOException { String line; ClientResource clientRes; Representation rep; InputStream in = null; Reader reader = null; BufferedReader lineReader = null; clientRes = new ClientResource(uri); rep = clientRes.get(MediaType.TEXT_HTML); if (rep == null) { return; } try { in = rep.getStream(); reader = new InputStreamReader(in); in = null; lineReader = new BufferedReader(reader); reader = null; line = lineReader.readLine(); while (line != null) { writer.println(line); line = lineReader.readLine(); } } finally { if (in != null) { IOUtils.close(in); in = null; } if (reader != null) { IOUtils.close(reader); reader = null; } if (lineReader != null) { IOUtils.close(lineReader); lineReader = null; } } } }