I'm trying to access a mule flowVar from within a Java class:
In the mule processor:
flowVars.rootFilePath="c:\test"
From within the mule processor, I'm calling the java method renameFile(oldFile, newFile) :
package com.rename;
import java.io.File;
import org.mule.api.MuleMessage;
public class FileRename {
public String renameFile(String oldFile, String newFile) {
File file1 = new File(message.getInvocationProperty("rootFilePath") + oldFile);
File file2 = new File(message.getInvocationProperty("rootFilePath") + newFile);
file1.renameTo(file2);
return "Renaming " + oldFile + " to: " + newFile;
}
}
However, I'm receiving the error "message cannot be resolved". What am I missing here? Your help is very much appreciated!
Why can't you use onCall Method to do this?
You can use below code as a sample to access message.
public class MyComponent implements Callable {
#Override
public Object onCall(MuleEventContext eventContext) throws Exception {
String oldFile = eventContext.getMessage().getProperty('');
return "Renaming " + oldFile + " to: " + newFile;";
}
}
Related
I am able to access http://localhost/manage/welcome page. But not able to get the html. it just returns the string.
#Endpoint(id="welcome")
#Component
public class UtilClass {
#ReadOperation
public String logger() {
return "<html><table>sampletable</table></html>";
}
}
Please suggest any way to load html content without using #Controller/ #RestController or thyleaf
I don't understand why you don't want to use #Controller
#ReadOperation has a parameter to set output content type in spring boot 2.xx.
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
#Endpoint(id = "welcome")
#Component
public class UtilClass {
#ReadOperation(produces = MediaType.TEXT_HTML_VALUE)
public String logger() {
return "<html>" +
"<table>" +
" <thead><tr>" +
" <th>ID</th><th>Name</th></tr>" +
"</thead>" +
" <tbody><tr>" +
" <td>1</td><td>Arfat</td></tr>" +
"</tbody>" +
"</table>" +
"</html>";
}
}
When i try to compile the following code using javac or gradlew
I got the error method does not override or implement a method from a supertype #Override
The error happened to the two #Override functions
How to solve that ?
code.java:
package com.android.commands.locksettings;
import android.os.ResultReceiver;
import android.os.ServiceManager;
import android.os.ShellCallback;
import com.android.internal.os.BaseCommand;
import com.android.internal.widget.ILockSettings;
import java.io.FileDescriptor;
import java.io.PrintStream;
public final class LockSettingsCmd extends BaseCommand {
private static final String USAGE =
"usage: locksettings set-pattern [--old OLD_CREDENTIAL] NEW_PATTERN\n" +
" locksettings set-pin [--old OLD_CREDENTIAL] NEW_PIN\n" +
" locksettings set-password [--old OLD_CREDENTIAL] NEW_PASSWORD\n" +
" locksettings clear [--old OLD_CREDENTIAL]\n" +
"\n" +
"locksettings set-pattern: sets a pattern\n" +
" A pattern is specified by a non-separated list of numbers that index the cell\n" +
" on the pattern in a 1-based manner in left to right and top to bottom order,\n" +
" i.e. the top-left cell is indexed with 1, whereas the bottom-right cell\n" +
" is indexed with 9. Example: 1234\n" +
"\n" +
"locksettings set-pin: sets a PIN\n" +
"\n" +
"locksettings set-password: sets a password\n" +
"\n" +
"locksettings clear: clears the unlock credential\n";
public static void main(String[] args) {
(new LockSettingsCmd()).run(args);
}
#Override
public void onShowUsage(PrintStream out) {
out.println(USAGE);
}
#Override
public void onRun() throws Exception {
ILockSettings lockSettings = ILockSettings.Stub.asInterface(ServiceManager.getService("lock_settings"));
lockSettings.asBinder().shellCommand(FileDescriptor.in, FileDescriptor.out,
FileDescriptor.err, getRawArgs(), new ShellCallback(), new ResultReceiver(null) {});
}
}
Solved by restoring original BaseCommand file, Thanks to #deHaar
below is my java file, and i'm exporting the same an separate jar file ,name of the jar is "Softassert"
package com.annuity_payer;
import java.util.Arrays;
import java.util.Map;
import org.testng.Assert;
import org.testng.Reporter;
import com.thoughtworks.selenium.SeleneseTestBase;
public class Softassert extends SeleneseTestBase {
private StringBuffer verificationErrors;
private StringBuffer verificationSuccess;
public Softassert() {
verificationErrors = new StringBuffer();
verificationSuccess = new StringBuffer();
}
public void verifyEquals(String actual, String expected, String msg) {
try {
Assert.assertEquals(actual, expected, msg);
verificationSuccess.append(msg + ":" + " " + "Actual Result:" + " "
+ actual + " " + "Expected Result:" + " " + expected
+ " - Condition PASSED" + "\n");
} catch (AssertionError e) {
verificationErrors.append(e + "-Condition FAILED" + "\n");
}
}
}
so then i'm creating an new project and the jar file is mapped with build path to retrieve an method from the jar file
below are the code
import com.annuity_payer.Softassert.*;
public class testJar {
Softassert prabu = new Softassert();
prabu.verifyEquals("test","test","verification");
from that above i'm creating an object name of "prabu", then try to call desire method (i.e verifyEquals method)
when i try the same its shown an error "syntax error on tokens" Please clarify / help how to call the method from my jar file
You don't need the star in the import if you only want to import "Softassert", use...
import com.annuity_payer.Softassert;
If you want all classes in the annuity_payer package, you can use
import com.annuity_payer.*;
You might be getting confused with "import static" statements
Does anyone know how to get the same information, about what paths are used, like at the start of dw application. I mean the output after this line:
io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:
GET /path/of/res/test (this.is.the.class.package.info.MyRessource)
POST /path/of/res/test2 (this.is.the.class.package.info.MyRessource2)
I have to check if specific path exists.
You'll have to do this on your own. Take a look at the logEndpoints method (which is what actually logs this information - with private methods). You should be able to adapt this method to handle the resources from your environment.jersey().getResourceConfig() after you configure your resources in your run method.
Something like:
final ImmutableList.Builder<Class<?>> builder = ImmutableList.builder();
for (Object o : environment.jersey().getResourceConfig().getSingletons()) {
if (o.getClass().isAnnotationPresent(Path.class)) {
builder.add(o.getClass());
}
}
for (Class<?> klass : environment.jersey().getResourceConfig().getClasses()) {
if (klass.isAnnotationPresent(Path.class)) {
builder.add(klass);
}
}
final List<String> endpoints = Lists.newArrayList();
for (Class<?> klass : builder.build()) {
AbstractResource resource = IntrospectionModeller.createResource(klass);
endpoints.add(resource.getPath().getValue());
}
Note that what's in master is slightly ahead of what's in Maven - the above example shows how to get the AbstractResource which will work with 0.7.1. You'll have to be sure to adapt your method as dropwizard evolves. This example also doesn't normalize the path but I you can easily add that based on logEndpoints.
This solution works for me (DW 0.7.1):
private Multimap<String, String> getEndpoints(Environment environment)
{
Multimap<String, String> resources = ArrayListMultimap.create();
ResourceConfig jrConfig = environment.jersey().getResourceConfig();
Set<Object> dwSingletons = jrConfig.getSingletons();
for (Object singletons : dwSingletons) {
if (singletons.getClass().isAnnotationPresent(Path.class)) {
AbstractResource resource = IntrospectionModeller.createResource(singletons.getClass());
AbstractResource superResource = IntrospectionModeller.createResource(singletons.getClass().getSuperclass());
String uriPrefix = getStringWithoutStartingSlash(resource.getPath().getValue());
for (AbstractResourceMethod srm :resource.getResourceMethods())
{
String uri = uriPrefix;
resources.put(uri,srm.getHttpMethod());
LOG.info("Found http method " +srm.getHttpMethod() + " for the path " + uri + " returning (class) " + srm.getReturnType().getName());
}
for (AbstractSubResourceMethod srm :resource.getSubResourceMethods())
{
//extended resources methods will be added by hand
if(superResource != null){
for (AbstractSubResourceMethod superSrm : superResource.getSubResourceMethods())
{
String srmPath = getStringWithoutStartingSlash(srm.getPath().getValue());
String superSrmPath = getStringWithoutStartingSlash(superSrm.getPath().getValue());
Class<?> srmClass = srm.getDeclaringResource().getResourceClass();
Class<?> superSrmClass = superSrm.getDeclaringResource().getResourceClass();
//add superclass method if methodName is not equal superMethodName
if(srmClass.getSuperclass().equals(superSrmClass) && !srm.getMethod().getName().equals(superSrm.getMethod().getName())){
String uri = uriPrefix + "/" + srmPath + "/" + superSrmPath ;
resources.put(uri,superSrm.getHttpMethod());
LOG.info("Found http method " +superSrm.getHttpMethod() + " for the path " + uri + " returning (class) " + superSrm.getReturnType().getName());
}
}
}
String uri = uriPrefix + "/" + getStringWithoutStartingSlash(srm.getPath().getValue());
resources.put(uri,srm.getHttpMethod());
LOG.info("Found http method " +srm.getHttpMethod() + " for the path " + uri + " returning (class) " + srm.getReturnType().getName());
}
}
}
return resources;
}
But #PathParam annoations are also plain, e.g. if #Path("/{id}") then sth. like '.../{id}' will be used!!!
If you extend your resources and super class does also have path annotation, then this method will produce also informations and even more than the default DW logEndpoints() method!
FYI: The imports used in class
import java.util.Set;
import javax.ws.rs.Path;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.api.model.AbstractResource;
import com.sun.jersey.api.model.AbstractResourceMethod;
import com.sun.jersey.api.model.AbstractSubResourceMethod;
import com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.dropwizard.setup.Environment;
I used a simpler approach for getting the same data. All the resources here are jersey resources.
Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(Path.class);
Collection<Object> values = beansWithAnnotation.values();
for (Object next : values) {
ResourceUtil.getResourceUrls(next);
}
public static List<String> getResourceUrls(Object obj)
{
Resource resource = Resource.from(obj.getClass());
String uriPrefix = resource.getPath();
List<String> urls = new ArrayList<>();
for (Resource res :resource.getChildResources())
{
String uri = uriPrefix + res.getPath();
urls.add(uri);
}
return urls;
}
In my application we are uploading image/audio/video files. I have allowed some fixed set of allowed extensions of each types. If the extension does not match the list of provided extensions then it won't upload, but if the user renames the .exe, .pdf file to .jpg or .png the it would clear the UI validations and servlet will be called and upload will happen. But later that would cause a problem. Is there any way to check the same at backend and then throw an Exception accordingly.
I have tried using :
import java.net.*;
public class FileUtils{
public static String getMimeType(String fileUrl) throws java.io.IOException {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String type = fileNameMap.getContentTypeFor(fileUrl);
return type;
}
public static void main(String args[]) throws Exception {
System.out.println(FileUtils.getMimeType("file:/home/kavinder/file.pdf"));
// this is a .jpg file renamed to .pdf
}
}
This is returning according to extension only. And
import javax.activation.MimetypesFileTypeMap;
import java.io.File;
class GetMimeType {
public static void main(String args[]) {
File f = new File("/home/kavinder/file.jpg");
System.out.println("Mime Type of " + f.getName() + " is " + new MimetypesFileTypeMap().getContentType(f));
}
}
This is always returning the mime type as: application/octet-streamThank you in advance