Embedding outofbox portlets in Custom portlet - embed

I m trying to embed calender portlet in my custom portlet. But not able to accomplish this. I am getting below error:
javax.servlet.ServletException: javax.servlet.jsp.JspException:
ServletException in '/html/common/themes/portlet.jsp':
File "/html/common/themes/portlet.jsp" not found
at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
This is my class:
public class Hello extends MVCPortlet {
public void doView(RenderRequest request, RenderResponse response)
throws IOException, PortletException {
final String portletId = "47"; //hello world portlet id
final ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
HttpServletRequest req = PortalUtil.getHttpServletRequest(request);
HttpServletResponse res = (HttpServletResponse) request.getAttribute(PortletServlet.PORTLET_SERVLET_RESPONSE);
ServletContext servletContext = req.getSession().getServletContext();
PortletPreferences prefs = null;
try {
// get the portlet preferences
prefs = PortletPreferencesFactoryUtil.getPortletPreferences(req, portletId);
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("portlet preferences are " + prefs.toString());
//method 3
PortletPreferences originalPrefs = null;
try {
originalPrefs = PortletPreferencesFactoryUtil.getStrictLayoutPortletSetup(
themeDisplay.getLayout(), portletId);
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PortletPreferences targetPrefs = null;
try {
targetPrefs = PortletPreferencesFactoryUtil.getStrictLayoutPortletSetup(
themeDisplay.getLayout(), portletId);
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (java.util.Map.Entry<String, String[]> entry : targetPrefs.getMap().entrySet()) {
targetPrefs.reset(entry.getKey());
}
for (java.util.Map.Entry<String, String[]> entry : originalPrefs.getMap().entrySet()) {
targetPrefs.setValues(entry.getKey(), entry.getValue());
}
// Store preferencec.
targetPrefs.store();
try {
//finally render the portlet
final String portletHtml = RuntimePortletEmbedUtil.renderPortlet(request, response, portletId, "");
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Any insights to portlet embedding, I have already tried working on suggestions from this blog but no help.
Any suggestions or a way to accomplish this?
I guess there are issues to get the correct portlet preferences and authority to add a portlet over a targeted portlet.
Please update if you found some solutions to the problems.

Related

Transform java object to JSONObject return null on live server

I have a web application (Maven project, JSP/SERVLET, TomCat 8.5.20). The application run perfectly in localhost (same TomCat version), but when i deploy to a live server, the following code doesn't work, the 'x01Json' (JSONObject) variable value be 'null' after i cal the transfromGameToJson() method.
Game init servlet, where i set JSON in request
X01Game x01Game = gameController.initX01Game(type, legsNumber, setsNumber, users, doubleIn, doubleOut, x01,
randomOrder, startingPoint);
JSONObject x01Json = gameController.transfromGameToJson(x01Game);
session.setAttribute("x01Game", x01Game);
request.setAttribute("x01Json", x01Json);
request.getRequestDispatcher("/darts/x01game.jsp").forward(request, response);
the method
public JSONObject transfromGameToJson(X01Game x01Game) {
ObjectMapper mapper = new ObjectMapper();
try {
mapper.writeValue(new File("x01Game.json"), x01Game);
JSONObject object = new JSONObject(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(x01Game));
return object;
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
If i delete this line from my method, it's working:
mapper.writeValue(new File("x01Game.json"), x01Game);

java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference

Every time I open my app it crashes because of this error I dont know what should I do please help me... I have also attached logcat screen shot below and the java class in which the error is comming.
DataParser.java:
public class DataParser {
private HashMap<String,String> getPlace(JSONObject googlePlacesJson){
HashMap<String,String> googlePlaceMap=new HashMap<>();
String placeName="-NA-";
String vicinity="-NA-";
String latitude="";
String logitude="";
String reference="";
List<HashMap<String,String>> placesList=new ArrayList<>();
try {
if(!googlePlacesJson.isNull("name")){
placeName=googlePlacesJson.getString("name");
}
if (!googlePlacesJson.isNull("vicinity")){
vicinity=googlePlacesJson.getString("vicinity");
}
latitude=googlePlacesJson.getJSONObject("geometry").getJSONObject("location").getString("lat");
logitude=googlePlacesJson.getJSONObject("geometry").getJSONObject("location").getString("lng");
reference=googlePlacesJson.getString("reference");
googlePlaceMap.put("name",placeName);
googlePlaceMap.put("vicinity",vicinity);
googlePlaceMap.put("lat",latitude);
googlePlaceMap.put("lng",logitude);
googlePlaceMap.put("reference",reference);
} catch (JSONException e) {
e.printStackTrace();
}
return googlePlaceMap;
}
private List<HashMap<String,String>> getPlaces(JSONArray jsonArray){
long count;
count=jsonArray.length(); // error comes on this line
List<HashMap<String,String>> placesList=new ArrayList<>();
HashMap<String,String> placeMap=null;
for(int i=0;i<count;i++){
try {
placeMap=getPlace((JSONObject) jsonArray.get(i));
placesList.add(placeMap);
} catch (JSONException e) {
e.printStackTrace();
}
}
return placesList;
}
public List<HashMap<String,String>> parse(String jsonData){
JSONArray jsonArray=null;
JSONObject jsonObject;
try {
jsonObject=new JSONObject(jsonData);
jsonArray=jsonObject.getJSONArray("results");
} catch (JSONException e) {
e.printStackTrace();
}
return getPlaces(jsonArray);
}
}
this is the screen shot my my log cat:
The problem is somewhere in here:
public List<HashMap<String,String>> parse(String jsonData){
JSONArray jsonArray=null;
JSONObject jsonObject;
try {
jsonObject=new JSONObject(jsonData);
jsonArray=jsonObject.getJSONArray("results"); // !!! this line here !!!
} catch (JSONException e) {
e.printStackTrace();
}
return getPlaces(jsonArray);
}
getJSONArray("results"); returns null and that's causing problems later (NullPointerException).
What you should do:
Check your JSON for syntax errors.
Place a breakpoint at the getJSONArray() line and take a look at jsonData and jsonObject if everything is, as it should be.
The array you want to retrieve is probably not valid or not in the jsonData at all.
i would start by validating if the jsonArray is null before running the entire method. or:
if (jsonArray != null)
count=jsonArray.length();
else count = 0;
then debug to find out why you are calling that method with a null object.

Android studio: Can't find obvious file location in c:?

Every time I run this code, I get the message "File not found" in my exception however, I don't understand why is that when I literally have the file I'm looking for sitting at the root of my c:?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView testMEtext = (TextView) findViewById(R.id.testMEtext);
//
JSONParser parser = new JSONParser();
try {
//File temp = new File("C:/Windows/outdoorWeather.json");
//String path = temp.getAbsolutePath();
//testMEtext.setText(path);
**Object obj = parser.parse(new FileReader("/C:/outdoorWeather.json"));**
JSONObject jsonObject = (JSONObject) obj;
String oTemp = (String) jsonObject.get("Fahrenheit temperature");
testMEtext.setText(oTemp);
}
catch (FileNotFoundException e) {
e.printStackTrace();
testMEtext.setText("File not found");
}
catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
I don't think you can interact with objects outside of your emulator. To solve it try referring to these.
Load a simple text file in android studio
How can I read a text file in android
Reading a simple text file

Parameter passing via httpgetClient

I need to hit the URL and get the response, but through this method I can get only one message whereas I want to pass multiple values as a response.
Here is the method:
public String getOutputFromUrl(String url)
{
Log.d("in getOutputFromUrl", "getOutputFromUrl");
String[] output = null;
try {
httpClient = new DefaultHttpClient();
httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
output = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
Log.d("in UnsupportedEncodingException", e.toString());
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.d("in geClientProtocolExceptiontOutputFromUrl", e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.d("in getOutputFromUrl", e.toString());
e.printStackTrace();
}
Log.d("in getOutputFromUrl:output===>>", output);
return output;
}
I want the return type of the method to be String[] or object of any class ,but
output = EntityUtils.toString(httpEntity)
This line accepts only a string; no other object or string array, so I can't keep multiple values in this output variable passed as response from the URL link

JSON Parsing on BlackBerry

I'm working on parsing JSON on BlackBerry using org.json.me, but I can't parsing the result. Simulator Console says: No Stack Trace
Here's my code to parsing JSON after receiving JSON string from my restclient
try {
JSONObject outer=new JSONObject(data);
JSONArray ja = outer.getJSONArray("status");
JSONArray arr=ja.getJSONArray(0);
System.out.println(arr);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And here's the piece code to get JSON from the server
public PromoThread(final String url, final ResponseCallback callback){
Thread t = new Thread(new Runnable(){
public void run() {
waitScreen = new WaitPopupScreen();
System.out.println("Log >> Promo thread run...");
synchronized (UiApplication.getEventLock()){
UiApplication.getUiApplication().pushScreen(waitScreen);
}
//network call
try {
conn = (HttpConnection) new ConnectionFactory().getConnection(url).getConnection();
conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
in = conn.openInputStream();
// parser.parse(in, handler);
//buff.append(IOUtilities.streamToBytes(in));
//result = buff.toString();
results = new String(IOUtilities.streamToBytes(in));
//System.out.println("Log >> Result: " + results);
UiApplication.getUiApplication().invokeLater(
new Runnable() {
public void run() {
//UiApplication.getUiApplication().popScreen(waitScreen);
callback.callback(results, waitScreen);
}
});
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
conn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
//start thread
t.start();
}
Thanks for your help