I have a JEE rest app for consume and build web service. When I use url of app in ibm bluemix I have an error in local server webSphere and also in Bluemix:
SRVE0777E: Exception thrown by application class
'tn.hunterViews.services.OfferService.afficherOffer:31'
java.lang.NullPointerException:
at tn.hunterViews.services.OfferService.afficherOffer(OfferService.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-2)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.ibm.ws.jaxrs20.server.LibertyJaxRsServerFactoryBean.performInvocation(LibertyJaxRsServerFactoryBean.java:674)
at [internal classes]
OffersService.java
package tn.hunterViews.services;
import java.util.List;
import javax.ejb.EJB;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import tn.hunterViews.business.OfferBusiness;
import tn.hunterViews.business.OfferBusinessRemote;
import tn.hunterViews.domain.Offer;
#Path("/Offers")
public class OfferService {
#EJB
OfferBusinessRemote ofR;
#GET
#Path("")
#Produces(MediaType.APPLICATION_JSON)
public Response afficherOffer(){
return Response.status(Status.OK).entity(ofR.getOffer()).build();
}
}
offersBuissness.java
package tn.hunterViews.business;
import java.util.List;
import javax.ejb.Stateless;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import tn.hunterViews.domain.Offer;
/**
* Session Bean implementation class OfferBusiness
*/
#Stateless
public class OfferBusiness implements OfferBusinessRemote {
#Override
public List<Offer> getOffer() {
Client cl = ClientBuilder.newClient();
WebTarget target = cl.target("https://pihunterviewsdotnet.mybluemix.net/api");
WebTarget off = target.path("offerApi");
List<Offer> offers = off.request(MediaType.APPLICATION_JSON).get(new GenericType<List<Offer>>(){}) ;
cl.close();
return offers;
}
#Override
public boolean createOffer(Offer offer) {
Client cl = ClientBuilder.newClient();
WebTarget target = cl.target("https://pihunterviewsdotnet.mybluemix.net/api");
WebTarget off = target.path("offerApi");
Response resp=target.request().post(Entity.entity(offer, MediaType.APPLICATION_JSON));
if(resp.getStatus()!=201)
return false;
return true;
}
#Override
public boolean updateOffer(int id, Offer offer) {
if (id+""!=null && id!=0 && offer.getId()!=0)
{
Client cl = ClientBuilder.newClient();
WebTarget target = cl.target("https://pihunterviewsdotnet.mybluemix.net/api/offerApi"+ id);
target.request().build("PUT", Entity.entity(offer, MediaType.APPLICATION_JSON))
.invoke();
return true;
}
return false;
}
#Override
public boolean deleteOffer(int id) {
if (id+""!=null && id!=0)
{
Client cl = ClientBuilder.newClient();
WebTarget target = cl.target("https://pihunterviewsdotnet.mybluemix.net/api/offerApi"+ id);
target.request().delete();
return true;}
return false;
}
#Override
public Offer findOfferById(int id) {
Client cl = ClientBuilder.newClient();
WebTarget baseUrl = cl.target("https://pihunterviewsdotnet.mybluemix.net/api/offerApi");
WebTarget getPostURL=baseUrl.path(""+id);
Response response = getPostURL.request(MediaType.APPLICATION_JSON).get();
Offer offer=response.readEntity(Offer.class);
response.close();
cl.close();
return offer;
}
}
Please help me with this problem. Thanks.
If the #EJB annotation cannot be resolved to an EJB, then you would
have received a failure when the server created an instance of the
class containing the #EJB annotation. Since that does not appear to be
happening for you, and instead the instance is created fine, just
without that field being set, then the server is not scanning your
class for annotations or you have the javax.ejb.EJB annotation class
packaged as part of your application.
I would recommend checking the following:
Make sure the javax.ejb.EJB class is not being packaged as part of your annotation
Check that the web.xml for your WAR module has a version > 2.4. WAR modules with a version of 2.4 (or earlier) will not be scanned for annotations.
Check that the web.xml does not contain the setting metadata-complete="true". This setting turns off annotation scanning.
Source: http://www.developersite.org/102-95523-websphere-liberty
Related
I have a maven java web application developed using Netbeans. I have figured out to run a parameter based Restful service successfully.
The URL contains three names in separated by slashes before providing the parameter.
http://localhost:8080/chims/api/data/list?name=district_list
Can I have a URL with less slashes like
http://localhost:8080/chims/data?name=district_list
This is the applicatoin config file.
package org.netbeans.rest.application.config;
import javax.ws.rs.core.Application;
#javax.ws.rs.ApplicationPath("api")
public class ApplicationConfig extends Application {
}
This is the service file.
package lk.gov.health.phsp.ws;
import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import lk.gov.health.phsp.bean.AreaApplicationController;
import lk.gov.health.phsp.entity.Area;
import lk.gov.health.phsp.enums.AreaType;
import org.json.JSONArray;
import org.json.JSONObject;
#Path("data")
#RequestScoped
public class ApiResource {
#Context
private UriInfo context;
#Inject
AreaApplicationController areaApplicationController;
/**
* Creates a new instance of GenericResource
*/
public ApiResource() {
}
#GET
#Path("list")
#Produces(MediaType.APPLICATION_JSON)
public String getJson(#QueryParam("name") String name) {
JSONObject jSONObjectOut;
if (name == null || name.trim().equals("")) {
jSONObjectOut = errorMessageInstruction();
} else {
switch (name) {
case "district_list":
jSONObjectOut = districtList();
break;
default:
jSONObjectOut = errorMessage();
}
}
String json = jSONObjectOut.toString();
return json;
}
private JSONObject districtList() {
JSONObject jSONObjectOut = new JSONObject();
JSONArray array = new JSONArray();
List<Area> ds = areaApplicationController.getAllAreas(AreaType.District);
for (Area a : ds) {
JSONObject ja = new JSONObject();
ja.put("district_id", a.getCode());
ja.put("district_name", a.getName());
array.put(ja);
}
jSONObjectOut.put("data", array);
jSONObjectOut.put("status", successMessage());
return jSONObjectOut;
}
private JSONObject successMessage() {
JSONObject jSONObjectOut = new JSONObject();
jSONObjectOut.put("code", 200);
jSONObjectOut.put("type", "success");
return jSONObjectOut;
}
private JSONObject errorMessage() {
JSONObject jSONObjectOut = new JSONObject();
jSONObjectOut.put("code", 400);
jSONObjectOut.put("type", "error");
jSONObjectOut.put("message", "Parameter name is not recognized.");
return jSONObjectOut;
}
private JSONObject errorMessageInstruction() {
JSONObject jSONObjectOut = new JSONObject();
jSONObjectOut.put("code", 400);
jSONObjectOut.put("type", "error");
jSONObjectOut.put("message", "You must provide a value for the parameter name.");
return jSONObjectOut;
}
}
I have not done any changes to the web.xml file. All the tutorials I went through did not give me a clear picture as to how and why I have to change it. Even without changing it, the web service works as expected.
How can I reduce the slashes in the URL?
The first thing you can do is remove the #Path("list"). A GET to /data will automatically go to the getJson method.
The next thing you can do is remove the api. You can do this by changing the "api" to "", "/", or "/*". All three will result in the same "/*". What happens when you do this is that Jersey will now take all requests that come to the server (and the same context). Any other servlets or static content will not be reachable.
To get around this, you can configure Jersey to run as a servlet filter instead of a servlet. Then configure it to forward unknown requests. See this post for how to configure this.
Please note: This issue has also been posted to the official Apache HttpClient Users mailing list.
Question
How should Java-based servers built on Apache HttpComponents assemble and send large SOAP with attachments (SWA) responses?
Issue details
The Apache HttpClient Java API does not support chunking multipart HttpEntity instances. Clients making SOAP requests to a mock server are failing (specifically, Apache-based Java clients are throwing org.apache.http.NoHttpResponseExceptions) when the attachment in the SWA response is large (>2 MiB). Small attachments are transmitted without throwing any exceptions. HTTP Content-Length header is correct.
Use case
Multithreaded SOAP server capable of transmitting SWA responses of arbitrary content length
Platform
Java 8 64b
Required Libraries
HttpCore v4.4.6 + HttpMime v4.5.3
Error replication
Build the example source with HttpCore v4.4.6 and HttpMime v4.5.3 libraries (HttpMime is part of the HttpClient project).
Run the program with a sufficiently large (>2 MiB) "random.png.gz" binary file.
Send the server an arbitrary HTTP POST request via some third-party client. As it's an Apache-based Java client with precision logging, SoapUI is recommended. NB: the failure to receive the large server-generated multipart result.
Example server source
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.ExceptionLogger;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import static org.apache.http.HttpStatus.SC_OK;
import org.apache.http.MethodNotSupportedException;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.nio.bootstrap.HttpServer;
import org.apache.http.impl.nio.bootstrap.ServerBootstrap;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.nio.protocol.BasicAsyncRequestConsumer;
import org.apache.http.nio.protocol.BasicAsyncResponseProducer;
import org.apache.http.nio.protocol.HttpAsyncExchange;
import org.apache.http.nio.protocol.HttpAsyncRequestConsumer;
import org.apache.http.nio.protocol.HttpAsyncRequestHandler;
import org.apache.http.protocol.HttpContext;
public class HttpComponentsConciseTest {
/*
* The random.png.gz file must be large (> 2 MiB)
*/
private static final String LARGE_COMPRESSED_FILE_NAME = "random.png.gz";
private static final File LARGE_TEST_GZIP_FILE = new File(LARGE_COMPRESSED_FILE_NAME);
private static final String SOAP_RESPONSE_MESSAGE_XML
= "<soapenv:Envelope "
+ "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
+ " <soapenv:Header/>\n"
+ " <soapenv:Body>\n"
+ " <AResponse>\n"
+ " <ResponseFile>" + LARGE_COMPRESSED_FILE_NAME + "</ResponseFile>\n"
+ " </AResponse>\n"
+ " </soapenv:Body>\n"
+ "</soapenv:Envelope>";
private static final int PORT_NUMBER = 8080;
public static void main(String[] args) {
startHttpServer();
}
private static void startHttpServer() {
IOReactorConfig config = IOReactorConfig.custom()
.setIoThreadCount(1)
.setSoTimeout(15000)
.setTcpNoDelay(true)
.build();
final HttpServer server = ServerBootstrap.bootstrap()
.setListenerPort(PORT_NUMBER)
.setServerInfo("Test/1.1")
.setIOReactorConfig(config)
.setExceptionLogger(ExceptionLogger.STD_ERR)
.registerHandler("*", new PrimaryRequestHandler())
.create();
Runtime.getRuntime().addShutdownHook(new Thread(() -> server.shutdown(5,
TimeUnit.MILLISECONDS)));
Thread serverOwner = new Thread(() -> {
try {
server.start();
} catch (IOException ex) {
Logger.getLogger(HttpComponentsConciseTest.class.getName()).log(Level.SEVERE, null,
ex);
}
}, "ServerOwner");
serverOwner.start();
}
private static class PrimaryRequestHandler implements HttpAsyncRequestHandler<HttpRequest> {
#Override
public HttpAsyncRequestConsumer<HttpRequest> processRequest(
final HttpRequest request,
final HttpContext context) {
return new BasicAsyncRequestConsumer();
}
#Override
public void handle(
final HttpRequest request,
final HttpAsyncExchange httpexchange,
final HttpContext context) throws HttpException, IOException {
HttpResponse response = httpexchange.getResponse();
String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
if (!method.equals("POST")) {
throw new MethodNotSupportedException(method + " method not supported");
}
StringBody soapResponseStringBody = new StringBody(SOAP_RESPONSE_MESSAGE_XML,
ContentType.APPLICATION_XML);
FileBody soapAttachment = new FileBody(LARGE_TEST_GZIP_FILE);
HttpEntity responseEntity = MultipartEntityBuilder.create()
.addPart("SOAP Envelope", soapResponseStringBody)
.addPart(LARGE_COMPRESSED_FILE_NAME, soapAttachment)
.build();
response.setStatusCode(SC_OK);
response.setEntity(responseEntity);
httpexchange.submitResponse(new BasicAsyncResponseProducer(response));
}
}
}
Pastebin link to source
https://pastebin.com/2Hzdhpt3
I am using Tomcat7, Sprng framework for restfull web services. I am trying to call an http web service that have basic authentication using Spring RestTemplate.
I couldn't get it to work. Can anybody please tell me based on the code below what do I need to change to make it able to call the http restfull web service that have basic authentication.. Also can anybody tell me or provide me with the pom.xml file which java libraries would I need?
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.journaldev.spring.controller.EmpRestURIConstants;
import com.journaldev.spring.model.CostControlPost;
import com.journaldev.spring.model.Employee;
import com.journaldev.spring.model.RfxForUpdate;
import static org.junit.Assert.*;
import org.apache.commons.codec.binary.Base64;
import javax.net.ssl.*;
import java.io.*;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class TestExample2 {
public static final String SERVER_LIST="http://abc/sourcing/testServices";
#Test
public void testGetListOfServiceNames()
{
try
{
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(SERVER_LIST,HttpMethod.GET,null,String.class);
assertNotNull(response);
}
catch(Exception e)
{
System.out.println("e:"+e.getMessage());
}
}
}
In simplest form:
DefaultHttpClient httpClient = new DefaultHttpClient();
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password);
httpClient.setCredentialsProvider(credentialsProvider);
ClientHttpRequestFactory rf = new HttpComponentsClientHttpRequestFactory(httpClient);
template = new RestTemplate(rf);
Spring Automatic Management:
Create HTTP context for RestTemplate:
private HttpContext createHttpContext() {
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(host, basicAuth);
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
return localcontext;
}
Add the interceptors:
restTemplate.getInterceptors().add(
new BasicAuthorizationInterceptor("username", "password"));
Call:
restTemplate.exchange(
"http://abc/sourcing/testServices",
HttpMethod.GET, null, String.class);
Refer this post.
The question is basically the title of the post.
Is there a possibility to hide the hidden files that appear on the CRXDE Lite?
I have a mac and in my CRXDE Lite i can see the .DS_Store files and i don't want to see them.
Why hide them when you can delete them? This simple example is a Servlet. you could run this nightly with an OSGi scheduler.
package com.foo.bar;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import javax.jcr.query.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
#SlingServlet(paths={"/bin/deletedsstore"})
public class DeleteDSStoreServlet extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory.getLogger(DeleteDSStoreServlet.class);
private static final String SQL2_QUERY = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/content]) and NAME() = '.DS_Store'";
private static final int SAVE_THRESHOLD = 100;
#Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
ResourceResolver resolver = request.getResourceResolver();
Iterator<Resource> resources = resolver.findResources(SQL2_QUERY, Query.JCR_SQL2);
int deleted = 0;
while (resources.hasNext()) {
Resource resource = resources.next();
String path = resource.getPath();
resolver.delete(resource);
log.info("Deleted node: " + path);
deleted++;
if (deleted % SAVE_THRESHOLD == 0) {
resolver.commit();
}
}
if (resolver.hasChanges()) {
resolver.commit();
}
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter out = response.getWriter();
out.write("Deleted " + deleted + " .DS_Store nodes");
}
}
I'm trying to create a RESTful web service in java that will allow authentication with a Yubikey.
I'm modifying an existing tutorial that I completed while trying to learn about REST.
I'm trying to call the validation function from within the javax.ws.rs.core.Response function but keep getting an error with a package from the imported yubikey jars.
I imported these jars from build path -> libraries --> Add external Jars
Error as follows when I post my form to the RESTful url:
java.lang.NoClassDefFoundError: com/yubico/client/v2/exceptions/YubicoValidationException
package de.vogella.jersey.todo.resources;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import com.yubico.client.v2.exceptions.YubicoValidationException;
import com.yubico.client.v2.exceptions.YubicoValidationFailure;
import de.vogella.jersey.todo.resources.Validate;
#Path("/test")
public class Test {
#POST
public Response testCred(#FormParam("username") String username,
#FormParam("password") String password,
#FormParam("otp") String otp) throws YubicoValidationException, YubicoValidationFailure {
int client_id = 11095;
boolean status;
status = Validate.validate(otp, client_id);
return Response.status(200)
.entity("validation status: : " + status + ", for client " + otp)
.build();
}
}
the validate class is as follows:
package de.vogella.jersey.todo.resources;
import com.yubico.client.v2.YubicoClient;
import com.yubico.client.v2.YubicoResponse;
import com.yubico.client.v2.YubicoResponseStatus;
import com.yubico.client.v2.exceptions.YubicoValidationException;
import com.yubico.client.v2.exceptions.YubicoValidationFailure;
public class Validate {
public static boolean validate(String otp, int client_id) throws YubicoValidationException, YubicoValidationFailure {
YubicoClient client = YubicoClient.getClient(client_id);
YubicoResponse response = client.verify(otp);
if(response != null && response.getStatus() == YubicoResponseStatus.OK) {
return true;
}
return false;
}
}