Self Signed Applet Can it access Local File Systems - applet

Hi I have created a Self Signed Applet , but not able to access local files system .What have i to do ?

you need to wrap your IO code inside PrivilegedAction.
Generally, you need to sign your applet with your test certificate, the user will see a warning and will have to accept the certificate when it loads the applet.
then you need to wrap your code inside a PriviligedAction. see this for some examples.

The below code is use to Add a Bouncy Castle Jar, the same way you can use it for accessing the file. AccessController java api is used.
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try{
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); // Here you can write the code for File Accesss
}catch (Exception e) {
return "";
}
return "";
}
});

Related

How to download pdf file using Feign Client?

In our project we are using feign client to make a call to third party service. For content type application/json it’s working fine. But we have a requirement where a third party service URL return pdf file and that time we are getting exception.
Due to security reason I can not paste the logs and code but if any one share me the code to download a pdf file from feign client that would be very helpful to me.
Thanks in advance!!
You could use byte[] as return type.
#FeignClient(url = "url", name = "name")
public interface SomeFeignClient {
#GetMapping("/give-me-a-pdf")
byte[] getPDF();
}
Your service would simply call
public byte[] getPDF() {
return SomeFeignClient.getPDF();
}
Now with the bytes array you could perform any operation you want, for example saving the file using
FileUtils.writeByteArrayToFile(new File("pathname"), resource);
or provide an endpoint to download the file (Spring boot can return pretty much anything without use of any external library)
#GetMapping("/pdf")
ResponseEntity getPDF() {
byte[] resource = SomeFeignClient.getPDF();
return ResponseEntity.ok()
.contentLength(resource.length)
.contentType(MediaType.APPLICATION_PDF)
.body(resource);
}

Send localized email in GWT [duplicate]

I have an interface that extends the com.google.gwt.i18n.client.Messages class, which I use for retrieving i18n messages in my GWT application. It looks like this:
public interface MyMessages extends com.google.gwt.i18n.client.Messages {
#DefaultMessage("Hello world")
#Key("message1")
String message1();
#DefaultMessage("Hello again")
#Key("message2")
String message2();
//...
}
Normally, I create an instance of it using GWT.create() like so:
private MyMessages messages = GWT.create(MyMessages.class);
However, this does not work with server-side code, only client-side code (it throws an error saying that GWT.create() is only usable in client-side code).
The answer to a similar question points to a separate library that you can download which will let you access the i18n messages on the server, but I don't want to download any extra libraries (this seems like a simple problem, there must be a simple solution).
In summary: How can I access my i18n messages in server-side code? Thanks.
On the server side you can use the standard Java localization tools like ResourceBundle.
Look here for a tutorial how to use it.
// Create a ResourceBundle out of your property files
ResourceBundle labels =
ResourceBundle.getBundle("LabelsBundle", currentLocale);
// Get localized value
String value = labels.getString(key);
The GWT specific way of creating an interface out of your property files and providing implementations via deferred binding can not be used on sever side Java.
If you are fearless and willing to spend the time, you can implement a code generation step to read your property files and generate implementation classes for your message interface. That's exactly what the Google GWT compiler does behind the scene.
I agree with Michael.. I was having this problem of trying to "localize" messages generated on the server.... but I decided to instead just throw an Exception on the server (because it is an error message which should only happen exceptionally) which contains the message code, which the client code can then look up and show the correct localized message to the user.
There's a great library for GWT internationalization gwt-dmesg. It allows you to 'share' .properties files between clent and server. However, project looks to be abandoned by author and you must recompile it manually for use with GWT versio >= 2.1.0.
GWT.create() can only be used in client-side code.
The good thing to do is that you provide your own I18NProvider class/interface, from which then you can extend to server side I18N factory and client side I18N factory read the same resource bundle.
After that you can simply use it all over your system, unify your code.
Hope that helps.
Following vanje's answer, and considering the encoding used for the properties files (which can be troublesome as ResourceBundle uses by default "ISO-8859-1", here is the solution I came up with:
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;
public class MyResourceBundle {
// feature variables
private ResourceBundle bundle;
private String fileEncoding;
public MyResourceBundle(Locale locale, String fileEncoding){
this.bundle = ResourceBundle.getBundle("com.app.Bundle", locale);
this.fileEncoding = fileEncoding;
}
public MyResourceBundle(Locale locale){
this(locale, "UTF-8");
}
public String getString(String key){
String value = bundle.getString(key);
try {
return new String(value.getBytes("ISO-8859-1"), fileEncoding);
} catch (UnsupportedEncodingException e) {
return value;
}
}
}
The way to use this would be very similar than the regular ResourceBundle usage:
private MyResourceBundle labels = new MyResourceBundle("es", "UTF-8");
String label = labels.getString(key)
Or you can use the alternate constructor which uses UTF-8 by default:
private MyResourceBundle labels = new MyResourceBundle("es");

How to mkdirs on gwt when fileupload on client

i mode development in eclipse. the fileupload works just fine. but i will make directory to /var/wms/year/month/file.jpg on linux. this my source code from client:
add component to form
fileUpload = new SingleUploader(FileInputType.LABEL);
fileUpload.setFileInputPrefix("PJ");
fileUpload.addOnFinishUploadHandler(onFinishUploaderHandler);
layoutContainerItemRight.add(fileUpload, formData);
method is addOnFinishUploadHandler
private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
public void onFinish(IUploader uploader) {
if (uploader.getStatus() == gwtupload.client.IUploadStatus.Status.SUBMITING) {
String month = VisionProperties.getBulan();
String year = DateTimeFormat.getFormat( "d-M-yyyy" ).format( new Date() ).split( "-")[2];
String strDirectoy = "/var/wms/" + year + "/" + month + "/";
File file = new File(strDirectoy);
if (!file.exists()) {
file.mkdirs();
}
}
if (uploader.getStatus() == gwtupload.client.IUploadStatus.Status.SUCCESS) {
String msg = uploader.getServerInfo().message;
fileName = msg.toString();
if(selectWindow != 2){
img.setUrl("servlet.gupld?show=&fieldname=" + fileName);
itemPanel.render(img.getElement());
}else{
tb.setVisible(true);
tb.setText("Download File "+uploader.getFileName());
}
}
}
};
how to make directory file when upload file process?
You are trying to use to use java.io.File in the client side which is not supported by the set of packages in the GWT jre emulation.
If you want to do this in client side you have to use the javascript File Api which is not supported by old browsers, and is not implemented in gwt-core. Using elemental you could use the Api only with Chrome, but I'm not positive. So it is better to wrap it via jsni, it is planned in gwtupload, but there is no a timeframe yet. Be aware that using js File Api, you dont have access to your real filesystem, but a virtual one inside your browser. To save created files in the local filesystem you have to download it using and iframe so as it asks the user where to save it.
Otherwise, If you wanted to do this work at server side, do it overriding the executeAction in your servlet if you are extending UploadAction.
You cannot do this on client side. You can perform this on server side in the following ways
before you upload the files to server by another rpc/http call.
after you upload the files to server when the file upload servlet is being executed on the srever side.
HTML5 FILE API are restricted to readonly behavior in even modern browser.
Reference -
1. Basic File upload in GWT
2. How to retrieve file from GWT FileUpload component?

Not allowed to use reflection in asp.net mvc2 running on a secure server?

I'm trying to deploy a web site on a secure server, but I'm having problems with reflection.
I guess this have something to do with security, but I'm not quite sure.
The error occurs when doing Assembly.GetTypes()
ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.]
System.Reflection.Module._GetTypesInternal(StackCrawlMark& stackMark) +0
System.Reflection.Assembly.GetTypes() +105
If I just ignore these exceptions and continue to load from assemblies, nothing is loaded. Seems like I'm not allowed to do this for any assemblies at all. When I run in a non secure webserver, everything works ok. I don't know much about IIS, mvc or .net security policies, so any help in the right direction would be greatly appreciated.
Heres the code causing the exception:
private static IEnumerable<IModule> GetModules()
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var type in assembly.GetTypes()) // <--- This one throws
{
var moduleType = type.GetInterface(typeof(IModule).Name);
if (moduleType != null)
{
IModule module = null;
try
{
module = (IModule)Activator.CreateInstance(type, null);
}
catch (ReflectionTypeLoadException ex)
{
// GetInterface() get's all interfaces with the same
// name, so we'll just skip those who isn't ours
logger.Warn("Could not load module", ex);
}
if( module != null)
yield return module;
}
}
}
}
I found the error. It's not related to HTTP/HTTPS at all.
This post helped me on track http://forums.asp.net/t/1196710.aspx
Some assemblies in a dependant module didn't get copied to the output folder. My development machine and the test server has these in GAC, so everything works there.
I added these components explicitly, and now everything works.
Thanks for your time

how can I check for an existing web folder

I work as software tester entry level and I was given a task to save my log files to the specific folder on my company website and this website only can be accessed internally by the company employees. So far I know how to save file onto the site, but how would I check which specific folder is already there before I save the file to it?
private void SaveLogsTogWeb(string file)
{
try
{
//create WebClient object
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
client.UploadFile(#"http://myCompnay/MyProjects/TestLogs/" + file, "PUT", file);
client.Dispose();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
Thanks in advance for the helps
Use this code:
if(!Directory.Exists({path}))
{
//create the directory
}
It checks to see if the directory doesn't exist. And if it doesn't then you can create it!
One way would be to put a dummy file in that folder (dummy.txt) and do an HTTP GET of the file. If you can successfully do that, you can then assume the folder exists (barring any virtual folders, etc.)