i have like this below code in order to start to publish wsdl
package my.mimos.stp.MelodyWS.webservice;
import javax.xml.ws.Endpoint;
public class Server {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8081/Melody/MelodyService", new MelodyWS());
System.out.println("Melody service is ready");
}
}
what should i do if i want to stop that service? i have a changes in MelodyWS and would like to republish it.
You have to keep a reference on the Endpoint object and call stop() method on it:
Endpoint ep = Endpoint.create(new MelodyWS());
ep.publish("http://localhost:8081/Melody/MelodyService");
..
ep.stop();
Related
I have a rest api using apache camel. When I hit a post request on a route, it gets a file from S3. Here is the code for that ->
public static class HelloRoute extends RouteBuilder {
#Override
public void configure() {
rest("/")
.post("file-from-s3")
.route()
.setHeader(AWS2S3Constants.KEY, constant("filename"))
.to("aws2-s3://bucketname?accessKey=INSERT&secretKey=INSERT®ion=INSERT&operation=getObject")
.endRest();
}
}
This gives the content of the file in Postman. I want the response in a json format where the contents of the file will be in the content key of json. How to do this?
Make sure you have binding mode enabled (auto|json) in your REST configuration and the consumes/produces set on your route. Now write your processor to build your response object and set it in the body. Camel will handle the rest for you.
public static class HelloRoute extends RouteBuilder {
restConfiguration().component("netty-http").host("localhost").port(portNum).bindingMode(RestBindingMode.auto);
#Override
public void configure() {
rest("/")
.post("file-from-s3")
.consumes("application/json").type(YourRequest.class)
.produces("application/json").outType(YourResponse.class)
.route()
.setHeader(AWS2S3Constants.KEY, constant("filename"))
.to("aws2-s3://bucketname?accessKey=INSERT&secretKey=INSERT®ion=INSERT&operation=getObject")
//.process("responseBuilderProcessor")
.endRest();
}
}
I have a resource method which produces a streaming download:
#GET
#Path("/{assetId}")
#Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response download(#PathParam("assetId") String assetId) {
StreamingOutput stream = os -> service.download(assetId, os);
return Response.ok(stream).build();
}
I want to unit test this with a mock service object. I already have:
private static AssetsService service = Mockito.mock(AssetsService.class);
#ClassRule
public final static ResourceTestRule resource = ResourceTestRule.builder()
.addResource(new AssetsResource(service))
.addProvider(MultiPartFeature.class)
.build();
#Test
public void testDownload() {
reset(service);
// how to get an output stream from this?
resource.client().target("/assets/123").request().get();
}
Per my comment in the test, what do I need to do in order to get an outputstream from the response? I find the jersey client API pretty confusing.
Once I have this, I'll stub the service call so that it writes a known file, and test that it's received correctly.
Try this:
Response response = resource.client().target("/assets/123").request().get();
InputStream is = response.readEntity(InputStream.class);
How to publish a web service class with #WebServiceProvider?What is the endpoint URL in this case?
Could we generate wsdl with #WebServiceProvider as in the case with
#WebService?What does the "wsdlLocation" attribute mean in #WebServiceProvider?
For instance
#ServiceMode(value = Service.Mode.MESSAGE)
#WebServiceProvider(portName = "ProviderPort",serviceName = "ProviderService",
targetNamespace = "http://bean/")
public class WebServiceProviderImpl implements Provider<SOAPMessage>
Simplest way is-
package server;
import javax.xml.ws.Endpoint;
public class Server {
protected Server() throws Exception {
System.out.println("Starting Server");
System.out.println("Starting SoapService1");
Object implementor = new WebServiceProviderImpl();
String address = "http://localhost:8123/SoapContext/SoapPort1";
Endpoint.publish(address, implementor);
}
public static void main(String args[]) throws Exception {
new Server();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
The URL is "address". As far as I understand you can specify it as you like, as long as the port is free.
Alternatively, you could use JAXWsServerFactoryBean which is a part of CXF.
You would do the same thing as you would with an SEI.
And yes, it does generate a WSDL for you.
You can create your client stubs from it using wsimport just like an SEI
Hi I am trying to automate AdvancedRestClient extension for Chrome to test webservice.
I am able to start the Extension and send request. But I am not able to get any response.
public class WebServices {
public static void main(String[] args) {
//Start the driver
System.setProperty("webdriver.chrome.driver","$PATH_TO_DRIVER");
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=C:/Users/$username/AppData/Local/Google/Chrome/User Data/Default/Extensions/hgmloofddffdnphfgcellkdfbfbjeloo/3.1.7_0");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
//Start the extension
driver.get("chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Get the authentication field and set authentication
WebElement authField = driver.findElement(By.xpath("//*[#id='appContainer']/div/div/div/div[4]/div[2]/section[1]/textarea"));
authField.sendKeys("$SET_AUTORIZATION")
//Get the reqestURL field and enter request
WebElement requestField = driver.findElement(By.xpath("//*[#id='appContainer']/div/div/div/div[2]/input"));
requestField.clear();
requestField.sendKeys("$REQUEST");
authField.click();
//Click on send button
WebElement sendButton = driver.findElement(By.xpath("//*[#id='appContainer']/div/div/div/div[7]/div/button[2]"));
sendButton.click();
}
}
The above steps work fine when I do it manually. But script does not generate any response.
Please help.
Automating a browser to test a web service is not the most reliable or efficient way.
You should instantiate a HttpClient in your test instead a Webdriver instance. This will allow you to make REST calls directly and interogate the response in the same way you would assert via WebDriver.
This approach will take milliseconds rather than seconds to run a test. Also, it can run anywhere without the need to install Chrome or Webdriver
I'm trying to access a query string parameter and save it to a Session variable. Since the solution I'm working on has several base layouts, the simplest approach would be to add this to a pipeline handler. However, my code is failing because args.Context.Session is null:
public class SaveQueryStringToSession : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
Assert.ArgumentNotNull((object)args, "args");
string queryString = WebUtil.GetQueryString("parm1");
if (queryString.Length <= 0)
return;
args.Context.Session["parm1"] = queryString;
}
}
This occurs when this method is inserted into either the HttpRequestBegin or HttpRequestEnd pipeline. Curious to know why, and if there is a standard workaround or pattern to use here. (Yes, I will add a null check. No need to point that out.)
I'm running Sitecore Sitecore.NET 6.4.1 (rev. 110720) on IIS 7.5 (Integrated .Net 2.0)
Possibly relevant links:
What is the first global.asax event that Session is accessible assuming the context handler is IRequiresSessionState or IReadOnlySessionState?
http://intothecore.cassidy.dk/2009/02/session-state-and-integrated-pipeline.html
The HttpRequestBegin pipeline is wired up to the HttpApplication.BeginRequest event, and this event is fired before the HttpSession object has been instantiated. Using the HttpRequestEnd pipeline does not work because the HttpSession object has already been disposed by the time the HttpApplication.EndRequest event is fired.
The session becomes available after the PostAcquireRequestState event is fired. To intercept this, create a class that implements IHttpModule, and add it to the <httpModules> element in Web.config. The HttpModule code will need to check if the request requires session state, as attempting to read the session for a static resource request will throw an exception.
Here is HttpModule code that accesses the Session and QueryString:
public class MyHttpModule :IHttpModule
{
public void Init(HttpApplication context)
{
context.PostAcquireRequestState += RequestHandler;
}
public void Dispose()
{
//
}
public void RequestHandler(object sender, EventArgs e)
{
var app = (HttpApplication) sender;
if (app.Context.Handler is IRequiresSessionState)
{
var session = app.Session;
var queryString = app.Request.QueryString["test"];
session["test"] = queryString;
}
}
}
It is worth noting that Sitecore's HttpRequestBegin and HttpRequestEnd pipelines are wired to ASP.NET via an HttpModule:
<add type="Sitecore.Nexus.Web.HttpModule,Sitecore.Nexus"
name="SitecoreHttpModule" />
Thanks to #ddysart for pointing me in the right direction, and to this answer for the correct event to listen for.
Actually instead of httpRequestBegin or HttpRequestEnd you can use httpRequestProcessed during which sitecore process the HttpRequest so you can access the Session.
You will be able to use the same code you have provided earlier.
public class SaveQueryStringToSession : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
Assert.ArgumentNotNull((object)args, "args");
string queryString = WebUtil.GetQueryString("parm1");
if (queryString.Length <= 0)
return;
args.Context.Session["parm1"] = queryString;
}
}