下面列出了org.springframework.core.NestedIOException#org.apache.velocity.exception.ResourceNotFoundException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public InputStream getResourceStream(String source) throws ResourceNotFoundException {
if (logger.isDebugEnabled()) {
logger.debug("Looking for Velocity resource with name [" + source + "]");
}
for (String resourceLoaderPath : this.resourceLoaderPaths) {
org.springframework.core.io.Resource resource =
this.resourceLoader.getResource(resourceLoaderPath + source);
try {
return resource.getInputStream();
}
catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not find Velocity resource: " + resource);
}
}
}
throw new ResourceNotFoundException(
"Could not find resource [" + source + "] in Spring resource loader path");
}
@Override
public Reader getResourceReader(String source, String encoding) throws ResourceNotFoundException {
if (logger.isDebugEnabled()) {
logger.debug("Looking for Velocity resource with name [" + source + "]");
}
for (String resourceLoaderPath : this.resourceLoaderPaths) {
org.springframework.core.io.Resource resource =
this.resourceLoader.getResource(resourceLoaderPath + source);
try {
return new InputStreamReader(resource.getInputStream());
} catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not find Velocity resource: " + resource);
}
}
}
throw new ResourceNotFoundException(
"Could not find resource [" + source + "] in Spring resource loader path");
}
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
String value = (String) node.jjtGetChild(0).value(context);
String character = (String) node.jjtGetChild(1).value(context);
int len = value.length();
StringBuilder sb = new StringBuilder(len);
sb.append(value.charAt(0));
for (int i = 1; i < len; i++) {
char c = value.charAt(i);
if (Character.isUpperCase(c)) {
sb.append(character).append(Character.toLowerCase(c));
} else {
sb.append(c);
}
}
writer.write(sb.toString());
return true;
}
@Override
public InputStream getResourceStream(String source) throws ResourceNotFoundException {
if (logger.isDebugEnabled()) {
logger.debug("Looking for Velocity resource with name [" + source + "]");
}
for (String resourceLoaderPath : this.resourceLoaderPaths) {
org.springframework.core.io.Resource resource =
this.resourceLoader.getResource(resourceLoaderPath + source);
try {
return resource.getInputStream();
}
catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not find Velocity resource: " + resource);
}
}
}
throw new ResourceNotFoundException(
"Could not find resource [" + source + "] in Spring resource loader path");
}
@Override
public InputStream getResourceStream(String source) throws ResourceNotFoundException {
if (logger.isDebugEnabled()) {
logger.debug("Looking for Velocity resource with name [" + source + "]");
}
for (String resourceLoaderPath : this.resourceLoaderPaths) {
org.springframework.core.io.Resource resource =
this.resourceLoader.getResource(resourceLoaderPath + source);
try {
return resource.getInputStream();
}
catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not find Velocity resource: " + resource);
}
}
}
throw new ResourceNotFoundException(
"Could not find resource [" + source + "] in Spring resource loader path");
}
@Override
public InputStream getResourceStream(String name) throws ResourceNotFoundException {
try {
Registry registry =
CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_CONFIGURATION);
if (registry == null) {
throw new IllegalStateException("No valid registry instance is attached to the current carbon context");
}
if (!registry.resourceExists(EMAIL_CONFIG_BASE_LOCATION + "/" + name)) {
throw new ResourceNotFoundException("Resource '" + name + "' does not exist");
}
org.wso2.carbon.registry.api.Resource resource =
registry.get(EMAIL_CONFIG_BASE_LOCATION + "/" + name);
resource.setMediaType("text/plain");
return resource.getContentStream();
} catch (RegistryException e) {
throw new ResourceNotFoundException("Error occurred while retrieving resource", e);
}
}
public boolean send(String from, String to, String cc, String title,
String templatePath, VelocityContext context) {
Template t = null;
try {
t = velocityEngine.getTemplate(BASE_PATH + templatePath);
} catch (ResourceNotFoundException ex) {
// ignored
}
if (t == null) {
try {
t = velocityEngine.getTemplate(templatePath);
} catch (ResourceNotFoundException e) {
t = velocityEngine.getTemplate("/" + templatePath);
}
}
final StringWriter writer = new StringWriter();
t.merge(context, writer);
if (LOG.isDebugEnabled()) {
LOG.debug(writer.toString());
}
return this.send(from, to, cc, title, writer.toString());
}
public boolean send(String from, String to, String cc, String title,
String templatePath, VelocityContext context) {
Template t = null;
try {
t = velocityEngine.getTemplate(BASE_PATH + templatePath);
} catch (ResourceNotFoundException ex) {
// ignored
}
if (t == null) {
try {
t = velocityEngine.getTemplate(templatePath);
} catch (ResourceNotFoundException e) {
t = velocityEngine.getTemplate("/" + templatePath);
}
}
final StringWriter writer = new StringWriter();
t.merge(context, writer);
if (LOG.isDebugEnabled()) {
LOG.debug(writer.toString());
}
return this.send(from, to, cc, title, writer.toString());
}
public boolean render(InternalContextAdapter context, Writer writer, Node node)
throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
//render content
StringWriter content = new StringWriter();
node.jjtGetChild(0).render(context, content);
//compress
try {
writer.write(htmlCompressor.compress(content.toString()));
} catch (Exception e) {
writer.write(content.toString());
String msg = "Failed to compress content: "+content.toString();
log.error(msg, e);
throw new RuntimeException(msg, e);
}
return true;
}
/**
* Returns a markdown rendering of provider documentation for the given provider information
* object with the given name.
*/
public String render(String providerName, ProviderInfo providerInfo) throws IOException {
VelocityContext context = new VelocityContext();
context.put("util", new MarkdownUtil());
context.put("providerName", providerName);
context.put("providerInfo", providerInfo);
StringWriter stringWriter = new StringWriter();
Reader reader = readerFromPath(providerTemplateFilename);
try {
velocityEngine.evaluate(context, stringWriter, providerTemplateFilename, reader);
} catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
throw new IOException(e);
}
return stringWriter.toString();
}
/**
* Renders the template and writes the output to the given file.
*
* Strips all trailing whitespace before writing to file.
*/
public void write(File outputFile) throws IOException {
StringWriter stringWriter = new StringWriter();
try {
engine.mergeTemplate(template, "UTF-8", context, stringWriter);
} catch (ResourceNotFoundException|ParseErrorException|MethodInvocationException e) {
throw new IOException(e);
}
stringWriter.close();
String[] lines = stringWriter.toString().split(System.getProperty("line.separator"));
try (Writer fileWriter = Files.newBufferedWriter(outputFile.toPath(), UTF_8)) {
for (String line : lines) {
// Strip trailing whitespace then append newline before writing to file.
fileWriter.write(line.replaceFirst("\\s+$", "") + "\n");
}
}
}
protected Language readTemplateLanguage(final String templateName) {
if (StringUtils.isEmpty(templateName)) {
throw new ResourceNotFoundException("DataSourceResourceLoader: Template name was empty or null");
}
Matcher matcher = SOURCE_NAME_PATTERN.matcher(templateName);
String languageStr = null;
if (matcher.matches()) {
languageStr = matcher.group("language");
} else {
throw new ResourceNotFoundException("Template name not valid. Pattern: templateId(/lang)?");
}
Language language = null;
if (StringUtils.isNotBlank(languageStr)) {
language = Language.valueOf(languageStr);
if (language == null) {
throw new ResourceNotFoundException("Language not supported");
}
}
return language;
}
/**
* Get an InputStream so that the Runtime can build a
* template with it.
*
* @param name name of template to get
* @return InputStream containing the template
* @throws ResourceNotFoundException if template not found
* in classpath.
*/
public synchronized InputStream getResourceStream(String name) throws ResourceNotFoundException
{
if (name == null || name.length() == 0) { return null;}
/* since the paths always ends in '/',
* make sure the name never ends in one */
while (name.startsWith("/"))
{
name = name.substring(1);
}
for (int i = 0; i < paths.length; i++)
{
InputStream result = null;
result = servletContext.getResourceAsStream(paths[i] + name);
if (result != null)
{
/* exit the loop if we found the template */
return result;
}
}
throw new ResourceNotFoundException(String.format("Template '%s' not found from %s", name, Arrays.asList(paths)));
}
/**
* Manages the {@link ResourceNotFoundException} to send an HTTP 404 result
* when needed.
*
* @param request The request object.
* @param response The response object.
* @param e The exception to check.
* @throws IOException If something goes wrong when sending the HTTP error.
*/
protected void manageResourceNotFound(HttpServletRequest request,
HttpServletResponse response, ResourceNotFoundException e)
throws IOException
{
String path = ServletUtils.getPath(request);
getLog().debug("Resource not found for path '{}'", path, e);
String message = e.getMessage();
if (!response.isCommitted() && path != null &&
message != null && message.contains("'" + path + "'"))
{
response.sendError(HttpServletResponse.SC_NOT_FOUND, path);
}
else
{
error(request, response, e);
throw e;
}
}
/**
* Checks to see when a resource was last modified
*
* @param resource Resource the resource to check
* @return long The time when the resource was last modified or 0 if the file can't be reached
*/
public long getLastModified(Resource resource)
{
// get the previously used root
String name = resource.getName();
String root = (String)templateRoots.get(name);
try
{
// get a connection to the URL
URL u = new URL(root + name);
URLConnection conn = u.openConnection();
conn.setConnectTimeout(timeout);
conn.setReadTimeout(timeout);
return conn.getLastModified();
}
catch (IOException ioe)
{
// the file is not reachable at its previous address
String msg = "URLResourceLoader: '"+name+"' is no longer reachable at '"+root+"'";
log.error(msg, ioe);
throw new ResourceNotFoundException(msg, ioe, rsvc.getLogContext().getStackTrace());
}
}
/**
* @see org.apache.velocity.runtime.parser.node.SimpleNode#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
*/
public boolean render( InternalContextAdapter context, Writer writer)
throws IOException, MethodInvocationException,
ResourceNotFoundException, ParseErrorException
{
SpaceGobbling spaceGobbling = rsvc.getSpaceGobbling();
if (spaceGobbling == SpaceGobbling.NONE)
{
writer.write(prefix);
}
int i, k = jjtGetNumChildren();
for (i = 0; i < k; i++)
jjtGetChild(i).render(context, writer);
if (morePostfix.length() > 0 || spaceGobbling.compareTo(SpaceGobbling.LINES) < 0)
{
writer.write(postfix);
}
writer.write(morePostfix);
return true;
}
public InputStream getResourceStream(String source) throws ResourceNotFoundException {
try {
return ConnectorIOUtils.getResourceAsStream(source);
} catch (TechnicalConnectorException var3) {
throw new ResourceNotFoundException(var3);
}
}
public InputStream getResourceStream(String source) throws ResourceNotFoundException {
try {
return ConnectorIOUtils.getResourceAsStream(source);
} catch (TechnicalConnectorException var3) {
throw new ResourceNotFoundException(var3);
}
}
public InputStream getResourceStream(String source) throws ResourceNotFoundException {
try {
return ConnectorIOUtils.getResourceAsStream(source);
} catch (TechnicalConnectorException var3) {
throw new ResourceNotFoundException(var3);
}
}
public InputStream getResourceStream(String source) throws ResourceNotFoundException {
try {
return ConnectorIOUtils.getResourceAsStream(source);
} catch (TechnicalConnectorException var3) {
throw new ResourceNotFoundException(var3);
}
}
public InputStream getResourceStream(String source) throws ResourceNotFoundException {
try {
return ConnectorIOUtils.getResourceAsStream(source);
} catch (TechnicalConnectorException var3) {
throw new ResourceNotFoundException(var3);
}
}
@Override
public Reader getResourceReader(String source, String encoding) throws ResourceNotFoundException {
try {
InputStream stream = servletContext.getResourceAsStream(source);
if (stream != null) {
return new InputStreamReader(stream, encoding);
}
return null;
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}
private void mergeTemplate(String name, String encoding, Map<String, Object> model, StringWriter writer)
throws ResourceNotFoundException, ParseErrorException, Exception {
VelocityContext velocityContext = new VelocityContext(model);
velocityContext.put("dateSymbol", new DateTool());
velocityContext.put("numberSymbol", new NumberTool());
velocityContext.put("mathSymbol", new MathTool());
Template template = engine().getTemplate(name, encoding);
template.merge(velocityContext, writer);
}
/**
* @see org.apache.velocity.runtime.parser.node.SimpleNode#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
*/
public boolean render( InternalContextAdapter context, Writer writer)
throws IOException,MethodInvocationException,
ResourceNotFoundException, ParseErrorException
{
return jjtGetChild(1).render( context, writer );
}
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
String clazz = (String) node.jjtGetChild(0).value(context);
if (context.containsKey(clazz)) {
String packagePath = context.get(clazz).toString();
packagePath = new StringBuilder("import ").append(packagePath).append(";\n").toString();
writer.write(packagePath);
return true;
}
return false;
}
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
String prefix = (String) node.jjtGetChild(0).value(context);
String str = (String) node.jjtGetChild(1).value(context);
String suffix = (String) node.jjtGetChild(2).value(context);
writer.write(String.valueOf(prefix + str + suffix));
return true;
}
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
String value = (String) node.jjtGetChild(0).value(context);
String result = value.replaceFirst(value.substring(0, 1), value.substring(0, 1).toUpperCase());
writer.write(result);
return true;
}
@Override
public Template getTemplate(String name) throws ResourceNotFoundException {
Template template = this.templates.get(name);
if (template == null) {
throw new ResourceNotFoundException("No template registered for name [" + name + "]");
}
return template;
}
@Override
public InputStream getResourceStream(String source) throws ResourceNotFoundException
{
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(_root + "/" + source);
return is;
}
@Override
public Reader getResourceReader(String source, String encoding) throws ResourceNotFoundException {
try {
return buildReader(loader.openResource("velocity/" + source), encoding);
} catch (IOException ioe) {
throw new ResourceNotFoundException(ioe);
}
}