I have one unique instance with three war :
Aapp.war
Bapp.war
Capp.war
The Capp need a wsdl endpoint exposed by the Bapp.
I know it's not great, but I need to find a temporary solution without a big refactoring in existing architecture.
The problem is even if the Aapp.war and Bapp.war are deployed, when tomcat begin to deploy the Capp, it lock everything because the bean which instanciate the Bapp Wsdl client does not respond :
#Bean
public WsStorageService getCustomerOfferStorageWSClient(){
WsStorageService wsClient = null;
String wsdlUrlProperty = prop.getCosWSApplicationServicesUrl();
URL url = null;
try {
url = new URL(wsdlUrlProperty);
} catch (MalformedURLException e) {
e.printStackTrace();
}
wsClient = new WsService_Service(url).getWsServicePort();
return wsClient ;
}
I cannot call http://myserver/Aapp or http://myserver/Bapp until the Capp is deployed successfully.
Why ?
Related
I have a requirement to write xml file to a sftp server in a Spring Batch application. Currently below code writes xml file to local file system using StaxEventItemWriter. I need to write directly to remote server instead of writing it to local and then moving to the sftp server. Referred this link (Writing to a remote file using Spring Integrations Sftp Streaming java configuration) but not sure how to write using StaxEventItemWriter/setup Resource object with remote file
public void write(List<? extends UserDTO> items) throws Exception {
for(UserDTO item : items) {
StaxEventItemWriter<UserDTO> staxWriter = getStaxEventItemWriter(item);
staxWriter.write(Arrays.asList(item));
}
}
private StaxEventItemWriter<UserDTO> getStaxEventItemWriter(UserDTO user) {
String key = user.getDomain();
StaxEventItemWriter<UserDTO> writer = writers.get(key);
if (writer == null) {enter code here
writer = new StaxEventItemWriter<>();
try {
UrlResource resource = new UrlResource("file:"+outputDir+"/"+key+"_"+fileName+".xml");
writer.setResource(resource);
writer.setRootTagName("customerSet");
Jaxb2Marshaller UserMarshaller = new Jaxb2Marshaller();
UserMarshaller.setClassesToBeBound(UserDTO.class);
writer.setMarshaller(UserMarshaller);
writer.setOverwriteOutput(Boolean.TRUE);
writer.open(executionContext);
} catch (MalformedURLException e) {
e.printStackTrace();
}
writers.put(key, writer);
}
return writer;
}
You can probably try to use SftpResource which is based on Spring Integration (similar to the solution in the link you shared) and use it in your StaxEventItemWriter.
I want to connect to elastic search from Java. Elastic search domain is configured in AWS. I am using Jest library for this. Currently i have added my system ip in the elastic search configure access section. So i can access ES endpoint. But this is not the right way of doing it. What are the approches to it ? i know about signing the request but could not find any good reference of how to do it in java. Can anyone give some thoughts ?
This is how my code looks like
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig.Builder(elasticSearchserverUrl).connTimeout(10000).readTimeout(10000)
.multiThreaded(true).build());
JestClient client = factory.getObject();
Search.Builder searchBuilder = new Search.Builder(query).addIndices(indices).addType(type);
try {
SearchResult result = client.execute(searchBuilder.build());
List<Hit<String, Void>> hits = result.getHits(String.class);
for (Hit<String, Void> hit : hits) {
String log = hit.source;
System.out.println(log);
}
} catch (IOException e) {
}
I have packaged and deployed my TestRestController.java(PFB code) in a JAR(testrest.jar) in an EAR in JBOSS EAP-6.2,
How can I access my REST-API, i tried hitting the http://{WEB-SERVER-IP}:8080/testrest/test/execute URL from a REST client? But I get HTTP 404.
Is it even possible?
TestRestController.java:
#Path("/test")
public class TestRestController
{
#POST
#Path("/execute")
#Consumes(MediaType.APPLICATION_JSON)
public Response executeRestApi(TestControllerDTO testControllerDto)
{
try
{
if (validateRequestParams(testControllerDto))
{
System.out.println("Validation success.");
response = Response.status(Status.OK).entity("Validation success.").build();
}
else
{
System.out.println("Validation failed.");
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Validation failed.").build();
}
}
catch (Exception e)
{
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Validation failed.").build();
}
return response;
}
private boolean validateRequestParams(TestControllerDTO testControllerDto)
{
boolean areParamsValid = false;
if (null != testControllerDto)
{
areParamsValid = true;
}
return areParamsValid;
}
}
Please help me.
P.S. : I am a newbie to Java and REST.
Thanks in advance.
You need to initiate the rest servlet somehow. The easiest way is to just add an javax.ws.rs.core.Application with an #ApplicationPath annotation to your application.
#ApplicationPath("/rest")
public class JaxRSApplication extends Application {
}
It can be left empty. It can packaged either in your .jar or in the .war (keep in mind, the .jar should also be included in the war in the WEB-INF/lib). With this the rest servlet will get initialized automatically and the classpath will be scanned for your resource classes annotated with #Path
You can see other deployment options here and for more detailed information, you can see the spec.
With the above Application class, you should be able to access
http://localhost:8080/my-app/rest/test/execute
Right now I am working on a android app and I am totally new to this.
I want to make sure my web-service is only accessible via my app.
My background is PHP. In PHP I don't need to worry about anything like that, because everything runs on a server.
In case of Java and especially Android programming things are different. Even with encryption. Everybody can just open an APK and see how the web service gets accessed. So is there a way to hide or to obfuscate the access to a web service, so only my app will be able to use it?
For test purposes I didn't add any security or encryption. This is the basic call to a web server I am doing right now:
String url = "http://thisismyurl.com/a.php?action=get";
String result = Web.executeWeb(url);
public class Web {
public static String executeWeb(final String url) {
final StringBuilder sb = new StringBuilder();
Thread thread = new Thread(new Runnable() {
public void run()
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String result, line = reader.readLine();
result = line;
while((line=reader.readLine())!=null){
result+=line;
}
sb.append(result);
//System.out.println(result);
//Log.i("My Response :: ", result);
} catch (Exception e)
{
// TODO: handle exception
}
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
}
How would I hide this from the prying eyes of hackers? ;-) Is that even possible?
Thanks in advance!
Deploy client authentication using (self signed) certificates within TLS.
This kind of configuration can be enabled on most web servers and Java application servers, and you can normally also configure the web or application server in such a way that you can retrieve the certificate of the private key that the client used to authenticate itself.
Note that HTTPS uses SSL (or now TLS) before any web trafic, so you cannot program this in your application, it does require server configuration.
Check this link on how to configure for Apache 2.
Use your Application's ID ( like IMEI ) as parameter in your webservice call. You need to make a table in database at server side which will store all registered device. Now only these registered device can access your webservice. This is my idea, there should be other idea as well.
i want to open a file and return its content. Although it is in the same directory like the class that wants to open the file, the file can't be found. Would be cool if you could help me solving the problem.
Here is the code:
#GET #Produces("text/html") #Path("/{partNO}/") #Consumes("text/html")
public String getPartNoResponseHTML(#PathParam("partNO") String parID) throws WebApplicationException {
PartNoTemplate partNo = getPartNoResponse(parID);
String result = "";
try {
result = readFile(PART_NO_TEMPLATE_FILE);
} catch (FileNotFoundException e) {
e.printStackTrace(System.out);
return e.getMessage() + e.toString();
// throw new WebApplicationException(Response.Status.NOT_FOUND);
} finally {
result = result.replace("{partNO}", parID);
result = result.replace("{inputFormat}", partNo.getFormat().toString());
}
return result;
}
I guess it can't find the file, because its running on tomcat. I'm also using Jersey and JAX-RS. Thank you for your help,
Maxi
If the file is inside the application WAR (or in a jar) you can try by using
InputStream input = servletContext.getClass().getClassLoader().getResourceAsStream("my_filename.txt");
Your problem is similar (I think) with How can I read file from classes directory in my WAR?
Try to get the path of the file from ServletContext.
ServletContext context = //Get the servlet context
In JAX-RS to get servlet context use this:
#javax.ws.rs.core.Context
ServletContext context;
Then get the file from your web application:
File file = new File(context.getRealPath("/someFolder/myFile.txt"));
You don't post the code that actually tries to read the file, but assuming the file is in the classpath (as you mention it's in the same directory as the class) then you can do:
InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt");
See here